Skip to content

[Engine] serve: keep the tool name on tool-result messages (#7051) - #7073

Open
subourbonite wants to merge 1 commit into
Niko1221:mainfrom
subourbonite:fix/tool-result-name-7051
Open

subourbonite wants to merge 1 commit into
Niko1221:mainfrom
subourbonite:fix/tool-result-name-7051

Conversation

@subourbonite

Copy link
Copy Markdown

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's name or from the earlier assistant call whose id matches tool_call_id. The server drops both before rendering: openai_to_messages does not copy name, and _order_chat_tool_results removes 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/responses and the server-side MCP loop.

With this change each tool result keeps a name the 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, then Service.encode_prompt on an Inkling Service). The template and tokenizer were exported from the ggml-org Inkling-2-Mini IQ4_NL GGUF:

Request main: tokens / empty tool headers This PR: tokens / empty tool headers llama.cpp /apply-template, same GGUF
Single get_weather round trip (the #7051 case) 725 / 1 of 1 728 / 0 of 1 728 tokens, byte-equal to this PR's text
Single create_calendar_event round trip 785 / 1 of 1 790 / 0 of 1 790 tokens, byte-equal to this PR's text
A 34-message Pi 1.6.0 coding session, request 15 (18 tool results, 4 tools declared) 24,220 / 18 of 18 24,238 / 0 of 18 24,238 tokens

The 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 the main text. 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.py hunks in _order_chat_tool_results and openai_to_messages do much the same thing, gated on _FAMILY == "inkling2". It does not cover /v1/responses or the MCP loop, and it sits inside a much larger feature. The two PRs will conflict on those lines. #6520 also adds test_tool_result_named_from_its_call, which asserts that a result is not named when the family is None. 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

  • A helper, _name_tool_results in serve/frontend.py, walks the messages while the private _call_ids are still there. A tool message with no name takes the name of the latest earlier call with its id. It runs from _order_chat_tool_results (chat completions and /v1/messages) and from responses._order_tool_results (/v1/responses, where the name is the flattened namespace.name the template already sees for the call).
  • openai_to_messages keeps a tool message's name when it is a non-empty string.
  • The MCP loop (server.py) builds its tool messages itself, with no ids. It now sets name from the call each result belongs to. results is aligned with calls, since the loop stops when a call does not finish.
  • A call id must be a non-empty string. A non-string or empty value is not used as a name, whether it comes from the client or from the call.

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_1 used by read_file and then write_file, both results are headed read_filewrite_file. This change takes the nearest earlier call instead (read_file, then write_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_results already 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_names renders a tool history through serve/chat_template.jinja (Qwen), tests/data/hy4/chat_template.jinja and tools/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 every docs/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_identical in test_chat_tool_results.py and in test_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_ids and test_ids_never_reach_the_template checked for leaks through the exact key set {"role", "content"}. They now allow name on 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-only tool_result check in test_server.py's test_anthropic_tool_result_image. New tests cover:

  • a name resolved from its call, rendered through the real Inkling template;
  • a client name kept (and a non-string one ignored);
  • an unmatched, empty or missing id, and a call with no name;
  • parallel calls of different tools answered out of order, including an incomplete run that is not reordered and is still named by id;
  • an id reused across turns (the nearest earlier call), and an id used twice in one turn (unnamed, also asserted in the existing duplicate_call case);
  • the Anthropic path through the Inkling template;
  • the MCP loop's next-round messages;
  • the other templates unchanged.

Validation

Revision: main at d46f5f268 (equal to v0.2.21), plus this change. macOS 27.2 on an Apple M4 Pro, Python 3.11.16. The venv was made with uv venv --python 3.11 .venv && uv pip install --python .venv/bin/python -r terminal/requirements.txt, the same pinned runtime requirements as the terminal-minimal-server workflow.

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 with frontend.py, responses.py and server.py reset to main gives FAILED (failures=6, errors=5, skipped=1). The 11 failures are:

    • 7 of the 8 new tests. The eighth, the unmatched-id test, guards against naming too much and passes on main by design.
    • The 4 updated assertions that now expect names.

    The /v1/responses and MCP parts are shown by two of these failing on main. test_responses.Parsing.test_codex_history_becomes_one_assistant_turn_per_answer expects the names, including the namespaced multi_agent_v1.spawn_agent. test_mcp.ToolLoop.test_results_are_named_after_their_calls fails on main with KeyError: 'name'. The two tightened leak tests pass on main and with this change.

  • .venv/bin/python -m unittest discover -s terminal/serve -p 'test_*.py', the CI command, from the repository root. On a clean main checkout: 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 Service on MockEngine (inkling_engine = True, set_family("inkling2")). It sends each chat request through openai_to_messages and Service.encode_prompt, the server's _openai path, without the engine. The tokenizer and template come from strata-inkling --export-tokenizer on the IQ4_NL GGUF, converted with terminal/tools/inkling_tokenizer.py from main. The exported chat_template.jinja is byte-identical to terminal/tests/data/inkling/chat_template.jinja. The llama.cpp column was rendered earlier with llama.cpp 239242322's /apply-template on the same GGUF, the same messages and the same tools, with reasoning_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:

  • The full serve/ suite has 7 failures on this machine, the same ones on main without this change. None of them involves tool results:
    • test_gpu_roles_server.Roles.test_every_card_off
    • test_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_words
    • test_vision_cpu_fallback.Fallback.test_the_card_in_that_sentence_is_the_first_one_the_reader_is_shown
    • test_vision_elastic.OnDemandByDefault.test_the_amd_reader_with_its_cpu_reader_is_on_demand_when_asked
    • test_vision_elastic.OnDemandByDefault.test_the_plain_nvidia_config_is_on_demand (error)
    • test_vision_elastic.OnDemandByDefault.test_what_stays_as_it_was
  • The first full run with this change also failed test_server.ImageMarkers.test_anthropic_tool_result_image. It asserted the exact message of a text-only tool_result and now expects "name": "Read" (see Tests). The run above is the one after that test was updated.

Not run:

  • No live generation with an engine or a model. The table compares rendered prompts only.
  • No CUDA, HIP, SYCL or Metal build. Only Python under terminal/serve/ changed.
  • Linux and Windows.
  • Templates installed with other families' model packs. The server loads the pack's own chat_template.jinja, and only the in-repo copies were checked for reading a tool message's name.

Limits

  • For Inkling, a conversation that was running before the update has new tool headers in its history, so its prompt cache stops matching at the first tool result and is read once more.
  • Anthropic tool_result blocks and Responses *_output items have no name field, so on those paths the name always comes from the matching call. A Responses client-side tool_search_output is headed tool_search, the name its call already has in the template.
  • With reused ids the header can differ from what llama.cpp renders for the same messages (see Approach).
  • Single-turn answers were correct before this change in the [Engine] serve: tool-result messages lose the tool name, so the Inkling header renders <|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

…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>
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.

[Engine] serve: tool-result messages lose the tool name, so the Inkling header renders <|message_tool|><|content_text|> (0.2.21)

1 participant