Skip to content

llm-server: parse tool calls a reasoning model writes before closing </think> - #580

Open
Lewis300 wants to merge 3 commits into
apple:mainfrom
Lewis300:tool-call-in-reasoning
Open

Lewis300 wants to merge 3 commits into
apple:mainfrom
Lewis300:tool-call-in-reasoning

Conversation

@Lewis300

@Lewis300 Lewis300 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Adding New Models

N/A: no new model.

Type of Change

  • Bug fix
  • Small feature (agreed in an issue)
  • New model (export recipe only)
  • New model (export recipe and new runtime support)
  • Documentation update

Motivation and Context

In long agent sessions, models sometimes write a tool call before they close their reasoning. llm-server then returns the call as text in reasoning_content, with finish_reason: stop and no tool_calls, so the agent has nothing to run and the user
has to prompt again:

  ...
  </parameter>
  </function>
  </tool_call>

Why it happens: reasoning and tool calls are parsed in series, and only text outside reasoning reaches the tool call parser.

  • For example, Qwen3's chat template ends the prompt with <think>\n, so the whole reply counts as reasoning until the model writes </think>.
  • ThinkTagParser only knows <think> and </think>, so a <tool_call> before the close is just more reasoning, and ToolCallParser never sees it.
  • CoreAILanguageModel chains the same two parsers the same way.

The fix: when a request offers tools, a tool call's open marker also ends the reasoning.

  • ThinkTagParser takes exitMarkers. They end a reasoning block as the close does, but stay in the text, so ToolCallParser gets the whole call.
  • A marker split across two deltas (<tool_ then call>) is held back, not emitted as reasoning.
  • A </think> written after the call closes nothing, so it's dropped rather than shown as content.
  • llm-server (streaming and non-streaming) and CoreAILanguageModel pass the tool format's open marker only when the request has tools. Without tools, nothing changes.
  • A streamed reply to a request without tools is no longer parsed for tool calls, matching the non-streaming path.

Testing

  • Tested locally

  • Added/updated tests

  • Added/updated docs

  • New ThinkTagParserTests suite. It feeds the model's output through both parsers in the server's order:

    • without exit markers, the call stays reasoning, as before;
    • with them, it parses as one bash call with the right command;
    • a split marker is held back;
    • a late </think> is dropped and the text after it kept;
    • a turn that closes its reasoning first parses exactly as before;
    • an exit marker outside reasoning changes nothing.
  • New ReasoningExitMarkerTests (LLMServerTests): only a request with tools gets the marker.

  • Existing suites: the ThinkTagParser and ToolCallParser tests pass.

…as a call

A reasoning model can write its tool call before it closes its reasoning
block, which Qwen3 models do in long, multi-turn contexts. Only text outside
reasoning reached the tool call parser, so the call was returned as
reasoning_content, with finish_reason stop and no tool_calls, and nothing ran.

ThinkTagParser takes exit markers: markers that end a tag-pair reasoning block
as the close does but stay in the text that follows, so the tool call parser
sees the call whole. A partial exit marker at the end of a delta is held back,
and a close the model writes after the call is dropped rather than shown.

llm-server passes its tool call format's open marker when a request offers
tools, in both the streaming and non-streaming paths. CoreAILanguageModel does
the same when the request enables tools. A request without tools keeps a
<tool_call> in its reasoning as reasoning.
@Lewis300
Lewis300 requested a review from a team as a code owner December 2, 2026 22:14
… arguments is kept

The exit marker ended the reasoning wherever the open marker appeared, so a
model that only mentioned `<tool_call>` while thinking lost the rest of its
reasoning and its answer: the tool call parser held everything after the
unclosed marker. And the late close was dropped anywhere after the exit,
including inside the call's arguments.

An exit marker now names the call's close as well as its open. The parser holds
the text from the open marker until it can tell: the call is real when it
closes and no close of the reasoning comes before another open marker. A close
that comes first means a mention, and the reasoning goes on to it. A call the
stream cuts off before it closes is reasoning, as it was. After a real call, a
late close is dropped only outside a call, so the calls' arguments pass
through untouched.
…h tests through the server

- ServerState.parsesToolCalls(for:) decides whether a reply is parsed for tool
  calls: the request carries tools and the model has a format. The streaming
  path parsed every reply and the non-streaming one only a request with tools;
  both now follow it, and the reasoning's exit markers follow it too, so a
  reply splits the same way streamed or not.
- ThinkTagParser.ExitMarker.forToolCalls(_:offered:) is the one place the
  markers come from, for the server and CoreAILanguageModel alike.
- The server tests run a reply that writes its call before </think> through
  runChatCompletion and runStreamingLoop, and check the call comes back as a
  tool call with none of it in the reasoning.
@tjia1818
tjia1818 requested a review from stikves December 4, 2026 19:10
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