Repository navigation
[Engine] serve: keep the tool name on tool-result messages (#7051) - #7073
Open
subourbonite wants to merge 1 commit into
Open
subourbonite wants to merge 1 commit into
subourbonite wants to merge 1 commit into
Conversation
…7051) Tool results reached the chat template without a name, so Inkling's tool-result header rendered empty. Resolve each result's name from the matching assistant call before call ids are dropped, and keep a client-sent name, on chat completions, /v1/messages, /v1/responses and the MCP loop. Ids repeated within one turn leave the result unnamed. Fixes Niko1221#7051 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem and result
Tool-result messages reach the chat template without the tool's name. Inkling-2-Mini's template heads every result with that name (
<|message_tool|>get_weather<|content_text|>…), taken from the message'snameor from the earlier assistant call whoseidmatchestool_call_id. The server drops both before rendering:openai_to_messagesdoes not copyname, and_order_chat_tool_resultsremoves the private call ids. So every tool result renders as<|message_tool|><|content_text|>…, and the model is never told which tool produced it. The same happens on/v1/messages,/v1/responsesand the server-side MCP loop.With this change each tool result keeps a
namethe client sends. Otherwise it gets the function name of the nearest earlier call with the same id, worked out before the ids are dropped. The MCP loop names its results from the calls it ran. The ids still never reach the template.Rendered offline with Strata's own front end on
main(openai_to_messages, thenService.encode_prompton an InklingService). The template and tokenizer were exported from the ggml-org Inkling-2-Mini IQ4_NL GGUF:main: tokens / empty tool headers/apply-template, same GGUFget_weatherround trip (the #7051 case)create_calendar_eventround tripThe only difference between the two renders is the 18 header names (
read,bash). If each<|message_tool|>NAME<|content_text|>in the new text is put back to<|message_tool|><|content_text|>, it is byte-identical to themaintext. The names appear in the same order as the calls.Related discussion
Fixes #7051.
The OpenAI chat and Anthropic parts overlap with #6520 (Inkling-2-Mini from the official NVFP4/MXFP8 checkpoint). That PR's
frontend.pyhunks in_order_chat_tool_resultsandopenai_to_messagesdo much the same thing, gated on_FAMILY == "inkling2". It does not cover/v1/responsesor the MCP loop, and it sits inside a much larger feature. The two PRs will conflict on those lines. #6520 also addstest_tool_result_named_from_its_call, which asserts that a result is not named when the family isNone. If both land, one side decides on gating, and either that test or this PR's other-templates test changes. This one is the focused fix for #7051. If #6520 lands first, this one can be rebased to drop the duplicate part, and the reverse works too. #6774 also changes the Inkling prompt path, but it does not touch these lines.Approach
_name_tool_resultsinserve/frontend.py, walks the messages while the private_call_ids are still there. A tool message with nonametakes the name of the latest earlier call with its id. It runs from_order_chat_tool_results(chat completions and/v1/messages) and fromresponses._order_tool_results(/v1/responses, where the name is the flattenednamespace.namethe template already sees for the call).openai_to_messageskeeps a tool message'snamewhen it is a non-empty string.server.py) builds its tool messages itself, with no ids. It now setsnamefrom the call each result belongs to.resultsis aligned withcalls, since the loop stops when a call does not finish.Reused ids. The template looks at every assistant call in the conversation, later ones included, and appends every matching name. With unique ids, which is what clients send, the result is identical to this change. With an id reused across turns it writes a name no tool has. For
call_1used byread_fileand thenwrite_file, both results are headedread_filewrite_file. This change takes the nearest earlier call instead (read_file, thenwrite_file), because a result answers the most recent call with that id. An id used twice inside one assistant turn is ambiguous, so its results are left unnamed._order_chat_tool_resultsalready treats that case as ambiguous and keeps the received order.Compatibility. AGENTS.md asks that the default path stay byte-identical to the last release. It does: the default (Qwen) template and the other non-Inkling templates never read a tool message's
name.test_the_other_templates_render_the_same_with_namesrenders a tool history throughserve/chat_template.jinja(Qwen),tests/data/hy4/chat_template.jinjaandtools/qwen4_intake_fixtures/qwen3-0.6b.chat_template.jinja, with and without the names, and checks that the renders are identical. The repository holds 19 chat templates, counting everydocs/bench-results/**/provenance/copy. They are byte copies of 4 files: those three and the Inkling one. For Inkling the prompt changes on purpose: the tool headers now carry their names, as the template intends.The fix is not gated by model family, because no other template reads the name. If you would rather keep it to Inkling, it can be gated on
_FAMILY == "inkling2", as #6520 does.Tests. Two existing tests encoded the missing name:
test_already_ordered_and_legacy_prompts_are_identicalintest_chat_tool_results.pyand intest_anthropic_tool_results.py. They expected a history with ids to normalize exactly like one without ids. A history with ids now also names its results, so each test checks those names and then compares the rest as before. The chat test still checks that the Qwen render is unchanged.test_reversed_results_of_the_same_function_follow_call_idsandtest_ids_never_reach_the_templatechecked for leaks through the exact key set{"role", "content"}. They now allownameon a tool message (set(m) <= {"role", "content", "name"}), and still require tool calls to be exactly{"function"}. Two more tests asserted exact messages and now expect the names:test_responses.py's Codex history test, and the text-onlytool_resultcheck intest_server.py'stest_anthropic_tool_result_image. New tests cover:duplicate_callcase);Validation
Revision:
mainatd46f5f268(equal tov0.2.21), plus this change. macOS 27.2 on an Apple M4 Pro, Python 3.11.16. The venv was made withuv venv --python 3.11 .venv && uv pip install --python .venv/bin/python -r terminal/requirements.txt, the same pinned runtime requirements as theterminal-minimal-serverworkflow.Passed:
cd terminal && ../.venv/bin/python -m unittest serve.test_chat_tool_results serve.test_anthropic_tool_results serve.test_responses serve.test_mcp serve.test_server.ImageMarkers:Ran 136 tests,OK (skipped=1). The same command withfrontend.py,responses.pyandserver.pyreset tomaingivesFAILED (failures=6, errors=5, skipped=1). The 11 failures are:mainby design.The
/v1/responsesand MCP parts are shown by two of these failing onmain.test_responses.Parsing.test_codex_history_becomes_one_assistant_turn_per_answerexpects the names, including the namespacedmulti_agent_v1.spawn_agent.test_mcp.ToolLoop.test_results_are_named_after_their_callsfails onmainwithKeyError: 'name'. The two tightened leak tests pass onmainand with this change..venv/bin/python -m unittest discover -s terminal/serve -p 'test_*.py', the CI command, from the repository root. On a cleanmaincheckout:Ran 2346 tests in 876.445s,FAILED (failures=5, errors=2, skipped=65). With this change:Ran 2354 tests in 873.414s,FAILED (failures=5, errors=2, skipped=65). The failing tests are the same 7 in both runs (listed under Failed), and no other test fails.The offline renders in the table above. A small script, not part of this PR, builds an Inkling
ServiceonMockEngine(inkling_engine = True,set_family("inkling2")). It sends each chat request throughopenai_to_messagesandService.encode_prompt, the server's_openaipath, without the engine. The tokenizer and template come fromstrata-inkling --export-tokenizeron the IQ4_NL GGUF, converted withterminal/tools/inkling_tokenizer.pyfrommain. The exportedchat_template.jinjais byte-identical toterminal/tests/data/inkling/chat_template.jinja. The llama.cpp column was rendered earlier with llama.cpp239242322's/apply-templateon the same GGUF, the same messages and the same tools, withreasoning_effort: medium. For the two round trips its saved text was compared byte for byte. For the agent session only its prompt token count was saved.Failed:
serve/suite has 7 failures on this machine, the same ones onmainwithout this change. None of them involves tool results:test_gpu_roles_server.Roles.test_every_card_offtest_serve_robustness.EngineOutputDecoding.test_a_non_utf8_byte_before_ready_and_in_a_reply(error)test_vision_cpu_fallback.Fallback.test_another_cause_falls_back_too_without_the_full_card_wordstest_vision_cpu_fallback.Fallback.test_the_card_in_that_sentence_is_the_first_one_the_reader_is_showntest_vision_elastic.OnDemandByDefault.test_the_amd_reader_with_its_cpu_reader_is_on_demand_when_askedtest_vision_elastic.OnDemandByDefault.test_the_plain_nvidia_config_is_on_demand(error)test_vision_elastic.OnDemandByDefault.test_what_stays_as_it_wastest_server.ImageMarkers.test_anthropic_tool_result_image. It asserted the exact message of a text-onlytool_resultand now expects"name": "Read"(see Tests). The run above is the one after that test was updated.Not run:
terminal/serve/changed.chat_template.jinja, and only the in-repo copies were checked for reading a tool message'sname.Limits
tool_resultblocks and Responses*_outputitems have no name field, so on those paths the name always comes from the matching call. A Responses client-sidetool_search_outputis headedtool_search, the name its call already has in the template.<|message_tool|><|content_text|>(0.2.21) #7051 report. This PR does not measure how the names affect answer quality in long tool chains.🤖 Generated with Claude Code