Skip to content

feat(core): structured tool calls with call ids - #666

Merged
DenisovAV merged 12 commits into
mainfrom
feat/structured-tool-call
Oct 15, 2026
Merged

DenisovAV merged 12 commits into
mainfrom
feat/structured-tool-call

Conversation

@DenisovAV

Copy link
Copy Markdown
Owner

Description

Message.toolCall has been one opaque string. InferenceChat stored the raw LiteRT-LM response or the model's own text in it, while the genai and Genkit converters stored {"name","parameters"}. No engine read it back: MediaPipe and the text engines replay it verbatim, LiteRT-LM replays it as user text, and the llama.cpp connector (#628) was the first to parse it. Call ids arrive from genai (ToolPart.callId) and Genkit (ToolRequest.ref), but were dropped at the boundary, so a result could only be paired with its call by name.

This PR makes the call structured, with ids, without changing what any engine replays today.

Core 2.4.0, additions only:

  • FunctionCallResponse.id (nullable), plus value ==/hashCode.
  • Message.toolCalls and Message.toolCallId, covered by copyWith, == and toString.
  • Message.toolCall({String? text, List<FunctionCallResponse> calls}):
    • text is what text engines replay — the model's output as the engine returned it, or a converter's JSON — kept as it is;
    • when text is null, it is {"tool_calls":[{"id","name","arguments"}]}, the format feat: add llama.cpp inference through llamadart #628 already persists;
    • text: alone still works.
  • Message.toolResponse(..., toolCallId:).
  • Engine SPI toolCallsOf(Message): the stored calls, or a parse of every text a toolCall has been written with. Histories saved as text therefore still replay, except that a web Gemma 4 call's text no longer marks its string arguments (documented). The text formats also read the canonical text back.

Writers:

  • InferenceChat stores the calls at all five places it records a tool turn. The text is unchanged, so prompts are byte-identical.
  • The tool loop passes call.id as toolCallId, including the balancing paths.
  • genai: callId → id/toolCallId, and id ?? toolName → callId. A callId equal to the tool name is the placeholder for "no id", so parallel calls to one tool don't collide.
  • Genkit 0.10.0: ToolRequest.ref → call id, ToolResponse.ref → toolCallId, and the engine's id → ref (call_N only for calls without one).

Connector (flutter_edge_ai_llama, #628): not in this PR. #628 already persists the canonical {"tool_calls":[{"id","name","arguments"}]} text, so its saved histories parse through toolCallsOf. Before it merges it should read toolCallsOf/toolCallId instead of its own parser and LlamaToolResponseMessage, yield core FunctionCallResponse(id:), and check id uniqueness against unanswered calls only (Genkit refs restart at call_0 each turn).

What ids do today: no bundled engine gives calls ids or reads toolCallId; results still pair with calls by name and order. Ids come from genai and Genkit and stay in the history for engines that read them (#628 next).

Versions: core 2.4.0 shares the release with #665 (already on main, one CHANGELOG section) and #660; genkit_flutter_edge_ai 0.10.0.

Not in this PR: LiteRT-LM replaying a toolCall as a real assistant tool_calls turn instead of user text. That is a behaviour change and needs a device run, so it comes as its own PR.

Type of Change

  • New feature
  • Documentation update
  • Test addition

Testing

  • message_tool_call_test, tool_calls_of_test (every legacy shape: canonical, raw SDK, back-to-back, multimodal content, genai/Genkit object and list, OpenAI string arguments, blank arguments, Gemma 4 tokens, FunctionGemma, Qwen, JSON in markdown, Phi, DeepSeek, Llama).
  • chat_tool_call_history_test covers each of the five writers.
  • Tool-loop ids, including the throw and not-run paths.
  • genai and Genkit converters both ways.
  • Every production line was mutated and its test went red.
  • flutter analyze packages/ (0 errors/warnings), check_skills, skills_lint; core +1334, genkit +229, genkit_hybrid +70, agent +231, litertlm +567.
  • Public-surface golden: one line, toolCallsOf function.

Review

A Codex pass and six review agents (architecture, Dart quality, guidelines and docs, type design, silent failures, engine replay). The engine-replay pass confirmed that nothing sent to any model changes: every engine reads only text, type, toolName, isUser and media, and LiteRT-LM at our pin gives calls no id. Fixed on this branch, each with a mutation-checked test:

  • a single Genkit call lost its id; numbered Genkit refs could repeat a real id, in either order; an empty ref became the id '';
  • SdkResponseParser dropped call ids, and threw a TypeError on a tool_calls that is not a list or a name that is not a string;
  • toolCallsOf returned part of a turn, read string arguments as {}, ignored args, and lost a FunctionGemma call with no arguments;
  • the canonical text Message.toolCall(calls:) writes was unreadable by every text format, so a model answering in that shape had its call shown as text — the JSON fallback the text formats share now reads a tool_calls list, whole or not at all;
  • a history's calls shared their argument maps with the calls the app receives; they are a frozen copy now;
  • the docs promised pairing by id, which no engine does yet.

Pre-existing defects the review found outside this diff (Gemma 4 raw-token arguments, three tool-loop paths, Message media hashing) are recorded for follow-ups.

Message.toolCall(calls:) stores the calls beside the model's text; with
text omitted it writes {"tool_calls":[{"id","name","arguments"}]}.
Message.toolResponse(toolCallId:) names the call a result answers, and
FunctionCallResponse gains an id and value equality. Engines read calls
through toolCallsOf (engine SPI), which also parses every text a
toolCall has been written with, so saved histories still replay.

InferenceChat stores the calls at all five places it records a turn and
passes call.id on in the tool loop. The genai converters map callId to
the id (a callId equal to the tool name stays "no id"); Genkit maps
ToolRequest.ref / ToolResponse.ref both ways.

core 2.4.0, genkit_flutter_edge_ai 0.10.0.
The blocking and streaming paths rebuilt the call from name and args,
so the id was lost and the converter fell back to call_0.
[id: call_1, no id] gave two call_1 refs. toolCallRefs numbers a call
without an id past every ref used before it, and a streamed chunk is
numbered against the calls of earlier chunks, so chunk and final
response agree.
SdkResponseParser read only the name and arguments, so a raw response
whose call carried an id lost it in the response and in the recorded
toolCalls.
A Genkit turn kept as text because one request's input was not an
object parsed to its other calls, so a replay answered part of a turn;
a canonical call with string arguments fell through to the SDK parser,
which read it with {} arguments. A JSON document of calls is now final:
all of its calls, or none.
… throwing

SdkResponseParser cast tool_calls to a list and the name to a string, so
{"tool_calls":"x"} or a numeric name threw a TypeError out of
toolCallsOf and the live SDK path.
The JSON format has always read parameters, args or arguments; a call
stored as {"name","args"} replayed with {} arguments.
The {} of call:get_time{} is valid JSON, so the text was taken for an
SDK response with no content and the wire-format parse saw nothing.
Message.toolCall(calls:) writes {"tool_calls":[...]}, which text engines
replay as the model's own turn, but no text parser read it: a model
answering in that shape had its call shown as text. The JSON fallback
every text format shares now reads a tool_calls list, whole or not at
all.
Message.toolCall copied the list of calls but not their argument maps,
which InferenceChat also hands to the app: an app changing call.args
rewrote the stored call. It now stores a frozen deep copy.
A real id an earlier call already took as its ref ([no id, id: call_0])
gave two call_0 refs; it is numbered now. An empty ToolRequest or
ToolResponse ref became the call id '' instead of no id, unlike genai
and the engines' parsers.
No bundled engine gives calls ids or reads toolCallId; results pair by
name and order. The stored text is what text engines replay, not always
the model's words. toolCallsOf reads one JSON document whole, the model
formats call by call, returns [] for an unreadable call, and loses a web
Gemma 4 call's string arguments from text alone. Consecutive toolCall
messages may be one turn.
@DenisovAV
DenisovAV merged commit 6f41abb into main Oct 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant