Repository navigation
fix(server): stream no trailing whitespace before a tool call - #5
Merged
bertholomus merged 1 commit intoOct 12, 2026
Conversation
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]>
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 |
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.
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_callsstrips theresidue), 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 themodel generated two:
\n\n, id 339,becomes
\n\n\n\n, id 56630);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 areply 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_weathertool,thinking off, temperature 0, seed 5; the assistant message re-sent as streamed, plus a tool result):
"I'll check the current weather in Lisbon for you.\n\n""I'll check the current weather in Lisbon for you."token_sha)a8f192daaa6ca8f192daaa6cIn 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.