Repository navigation
Fix Qwen reasoning history reconstruction - #663
Merged
Merged
Conversation
Preserve prompt-priming provenance and pass completed reasoning through reasoning_content while retaining answer text and tool metadata. Add pinned Qwen 3.8 regression and Qwen 3.6 compatibility tests covering exact cache reuse and safe fallback.
Use the concrete class name for the static helper call inside Task. AI assistance: OpenAI Codex applied the fix and ran focused verification.
davidkoski
requested changes
Oct 7, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Change looks good. Would it make sense to move the MessageGenerator to MLXLMCommon so that Qwen3 and Qwen3Next could also use it (I think that they probably need the same thing)
Contributor
Author
|
@davidkoski Moving shared Qwen reasoning normalization to |
abra-code
added a commit
to abra-code/mlx-swift-lm
that referenced
this pull request
Oct 13, 2026
… end A <think> or <|channel> typed in a user turn or returned by a tool primed the reasoning scanner, because the prompt check searched for the last opener anywhere in the tail. Models that carry a reasoning config but do not think then delivered their whole answer as reasoning. The prompt now primes only when it ends with the opener, or for a labeled channel with the opener and a one-word label. A typed opener encodes to the same token id as the template's, so only position can tell them apart. When both delimiters are single tokens, the stream decoder and ReasoningTokenCollector match them by id and report them to the emitter as boundaries. The same characters spelled out as text are content. Other tokenizers keep the text scan. The id lookup is shared with HarmonyFrameParser. ChatSession keeps finished reasoning beside the assistant message, and Qwen3VLMessageGenerator replays it as reasoning_content, so the history fix from ml-explore#663 keeps working now that reasoning is no longer part of the message text. ChatSession reads priming from the whole prompt, not the part the cache left to prefill. Added tests for markers in the middle of a prompt, a thought, and an answer, including the Harmony and Onyx decoders. Co-Authored-By: Claude Opus 5.5
abra-code
added a commit
to abra-code/mlx-swift-lm
that referenced
this pull request
Oct 16, 2026
… end A <think> or <|channel> typed in a user turn or returned by a tool primed the reasoning scanner, because the prompt check searched for the last opener anywhere in the tail. Models that carry a reasoning config but do not think then delivered their whole answer as reasoning. The prompt now primes only when it ends with the opener, or for a labeled channel with the opener and a one-word label. A typed opener encodes to the same token id as the template's, so only position can tell them apart. When both delimiters are single tokens, the stream decoder and ReasoningTokenCollector match them by id and report them to the emitter as boundaries. The same characters spelled out as text are content. Other tokenizers keep the text scan. The id lookup is shared with HarmonyFrameParser. ChatSession keeps finished reasoning beside the assistant message, and Qwen3VLMessageGenerator replays it as reasoning_content, so the history fix from ml-explore#663 keeps working now that reasoning is no longer part of the message text. ChatSession reads priming from the whole prompt, not the part the cache left to prefill. Added tests for markers in the middle of a prompt, a thought, and an answer, including the Harmony and Onyx decoders. Co-Authored-By: Claude Opus 5.5
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.
Proposed changes
Fix Qwen assistant-history reconstruction when the chat template expects reasoning in
reasoning_content.The tested Qwen 3.8 template lacks Qwen 3.6’s fallback for extracting reasoning from
content. WhenChatSessionretains reasoning inline, the next rendered prompt inserts an empty reasoning block before that text. This changes the token prefix and prevents exact cache reuse.This PR:
Qwen3VLMessageGeneratorintoreasoning_content, retaining the visible answer incontentand preserving whitespace and tool metadata.QwenReasoningProtocoldelimiters.Relationship to other work
This addresses one source of prompt divergence related to [#660](#660).
Validation
ChatSessiontests passed.pre-commit run --all-filespassed with swift-format 603.0.0.TokenIteratorClearCacheTests.testFirstTokenClearsBufferCachecache-seeding assertion. It passed in isolation; all other executed tests passed.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesNo public documentation changes are required.
AI usage