Skip to content

fix(server): stream no trailing whitespace before a tool call - #5

Merged
bertholomus merged 1 commit into
bertholomus:deepseek-v41-tp2from
willjriley:fix/stream-tool-call-separator
Oct 12, 2026
Merged

bertholomus merged 1 commit into
bertholomus:deepseek-v41-tp2from
willjriley:fix/stream-tool-call-separator

Conversation

@willjriley

Copy link
Copy Markdown

With tools in a request, the newlines the model writes between its text and a DSML tool-call block are streamed as
content before the block is recognised. The final (non-streamed) content drops them (parse_tool_calls strips the
residue), so a streaming client ends up with different content than a non-streaming one: "... for you.\n\n"
instead of "... for you.".

Clients send that content back in the next request, and the chat template adds its own separator before the tool calls
(as DeepSeek's reference encoder does: content + "\n\n" + tool calls). The history then holds four newlines where the
model generated two:

  • the next prompt no longer matches the tokens the model generated, from the end of its text on (\n\n, id 339,
    becomes \n\n\n\n, id 56630);
  • the model sees a format it wasn't trained on, and over a session it starts writing the longer separator itself.

Fix: with tools in the request, streamed content holds trailing whitespace back until more text follows. The end of
the reply already emits whatever the final content keeps beyond what was streamed (content[sent["content"]:]), so a
reply that ends in text still gets its whitespace, and a reply that ends in a tool call doesn't. Streamed and
non-streamed content are now the same by construction. Requests without tools are unchanged, and the generated tokens are
unchanged (only what is streamed as content changes), so cached == fresh is unaffected.

Before / after (same request: a system prompt, "What's the weather in Lisbon right now?", one get_weather tool,
thinking off, temperature 0, seed 5; the assistant message re-sent as streamed, plus a tool result):

before after
streamed content "I'll check the current weather in Lisbon for you.\n\n" "I'll check the current weather in Lisbon for you."
streamed == non-streamed no yes
generated tokens (token_sha) a8f192daaa6c a8f192daaa6c
next prompt matches the generated tokens no (differs at the separator) yes

In one streaming run of an agent benchmark (tool-eval-bench), we logged every place where a continued prompt stopped
matching the tokens the model had generated: 115 of the 129 were this doubled separator.

src/tensorfold/cuda/server.py +6/-3.

With tools in a request, the newlines the model writes between its text and a tool-call block were
streamed as content before the block was recognised, while the final (non-streamed) content drops
them (parse_tool_calls). A streaming client keeps them and sends them back, and the chat template
adds its own separator, so the history shows four newlines where the model wrote two: the prompt
stops matching the tokens the model generated (a kept prompt can no longer be continued past that
reply) and the model sees a format it was not trained on (it starts writing four itself). Streamed
content now holds trailing whitespace back until more text follows; the end of the reply emits
whatever the final content keeps, so streamed and non-streamed content are the same. Requests
without tools are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bertholomus

Copy link
Copy Markdown
Owner

Thank you @willjriley — merged. ELI5: with tools in the request, the two newlines before a tool call were streamed as reply text; the client echoes them back, the template adds its own two again, and the next prompt no longer matches what the model generated. Now trailing whitespace waits until more text follows: a reply ending in text keeps its whitespace, a reply ending in a tool call doesn't, and streamed equals non-streamed by construction. Generated tokens are unchanged (your token_sha rows), and the change compiles clean on our side. Thanks for the before/after table — and for finding that 115 of 129 mismatches were exactly this.

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.

2 participants