Skip to content

🐞 Tokenizer: Flush held text when streaming generation ends - #670

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/streaming-detokenizer-finalization
Oct 6, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
aleroot:alessio/streaming-detokenizer-finalization

Conversation

@aleroot

@aleroot aleroot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Proposed changes

Fixes #636

NaiveStreamingDetokenizer holds decoded text ending in a replacement character while waiting for more tokens. When generation ends, that buffered text is never emitted, potentially dropping both the replacement character and valid preceding text.

Add an idempotent finish() method and flush pending text through the existing generation, reasoning, tool, and framed-output pipelines. Final text passes through the usual stop-string filtering and parsers, while preserving consumer cancellation and frame boundaries.

Related to #636 and #619. This addresses dropped text at stream completion; arbitrary cleanup-induced prefix rewrites still require tokenizer-aware buffered decoding.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure:

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good, thank you!

@davidkoski
davidkoski merged commit 4ea4225 into ml-explore:main Oct 6, 2026
5 checks passed
wballard added a commit to swissarmyhammer/mlx-swift-lm-old that referenced this pull request Oct 7, 2026
Apply the upstream commit ecd8e88..4ea4225 as a 3-way patch. CLAUDE.md is
excluded.

- 4ea4225 Tokenizer: Flush held text when streaming generation ends (ml-explore#670)

The patch applies with no conflicts. Each stable user of
NaiveStreamingDetokenizer and TokenLoopHandler.onGenerationEnd is the same
as upstream, thus no stable-only path needs the new finish() flush.

Tests (xcrun xctest): MLXLMTests 1351 Swift Testing and 716 XCTest tests
pass. MLXGuidedGenerationTests 70, MLXFoundationModelsTests 349,
CXGrammarTests 7, MLXHuggingFaceMacrosTests 5 pass.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] NaiveStreamingDetokenizer drops characters when decode() output shrinks

3 participants