ToolCall

Represents a tool/function call made by the model in a chat response.

Access via response["message"]["tool_calls"] when the model calls a tool.

Members

Functions

toJson
JSONValue toJson()

Converts to a JSON object matching the Ollama API tool call format.

Variables

arguments
JSONValue arguments;

Arguments passed to the function (JSON object).

id
string id;

Optional tool call identifier.

name
string name;

Name of the function called.

Examples

// ToolFunction serialization
auto tf = ToolFunction("get_weather", "Fetch weather data",
    parseJSON(`{"type":"object","properties":{"city":{"type":"string"}}}`));
auto jtf = tf.toJson();
assert(jtf["name"].str == "get_weather");
assert(jtf["description"].str == "Fetch weather data");
assert(jtf["parameters"]["type"].str == "object");

// Tool serialization — JSON key must be "function" (not "function_")
auto tool = Tool("function", tf);
auto jt = tool.toJson();
assert(jt["type"].str == "function");
assert("function" in jt);
assert(jt["function"]["name"].str == "get_weather");

// ToolCall with id and arguments
auto tc = ToolCall("call-1", "get_weather", parseJSON(`{"city":"Paris"}`));
auto jtc = tc.toJson();
assert(jtc["id"].str == "call-1");
assert(jtc["function"]["name"].str == "get_weather");
assert(jtc["function"]["arguments"]["city"].str == "Paris");

// ToolCall without id — id key must be absent
auto tc2 = ToolCall("", "sum", parseJSON(`{"a":1,"b":2}`));
auto jtc2 = tc2.toJson();
assert("id" !in jtc2);
assert(jtc2["function"]["name"].str == "sum");

Meta