Repository navigation
feat(core): structured tool calls with call ids - #666
Merged
Merged
Conversation
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.
9 of 11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Message.toolCallhas been one opaque string.InferenceChatstored 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.toolCallsandMessage.toolCallId, covered bycopyWith,==andtoString.Message.toolCall({String? text, List<FunctionCallResponse> calls}):textis what text engines replay — the model's output as the engine returned it, or a converter's JSON — kept as it is;textis 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:).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:
InferenceChatstores the calls at all five places it records a tool turn. The text is unchanged, so prompts are byte-identical.call.idastoolCallId, including the balancing paths.callId→id/toolCallId, andid ?? toolName→callId. AcallIdequal to the tool name is the placeholder for "no id", so parallel calls to one tool don't collide.ToolRequest.ref→ call id,ToolResponse.ref→toolCallId, and the engine's id →ref(call_Nonly 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 throughtoolCallsOf. Before it merges it should readtoolCallsOf/toolCallIdinstead of its own parser andLlamaToolResponseMessage, yield coreFunctionCallResponse(id:), and check id uniqueness against unanswered calls only (Genkit refs restart atcall_0each 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_ai0.10.0.Not in this PR: LiteRT-LM replaying a toolCall as a real assistant
tool_callsturn instead of user text. That is a behaviour change and needs a device run, so it comes as its own PR.Type of Change
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_testcovers each of the five writers.flutter analyze packages/(0 errors/warnings),check_skills,skills_lint; core +1334, genkit +229, genkit_hybrid +70, agent +231, litertlm +567.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,isUserand media, and LiteRT-LM at our pin gives calls no id. Fixed on this branch, each with a mutation-checked test:'';SdkResponseParserdropped call ids, and threw aTypeErroron atool_callsthat is not a list or a name that is not a string;toolCallsOfreturned part of a turn, read string arguments as{}, ignoredargs, and lost a FunctionGemma call with no arguments;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 atool_callslist, whole or not at all;Pre-existing defects the review found outside this diff (Gemma 4 raw-token arguments, three tool-loop paths,
Messagemedia hashing) are recorded for follow-ups.