Skip to content

fix: honor max_tokens when completions end with partial UTF-8 - #2481

Open
remote-controlled-man wants to merge 2 commits into
abetlen:mainfrom
remote-controlled-man:codex/prflow-8e4cff80d8a5
Open

remote-controlled-man wants to merge 2 commits into
abetlen:mainfrom
remote-controlled-man:codex/prflow-8e4cff80d8a5

Conversation

@remote-controlled-man

Copy link
Copy Markdown
Contributor

Summary

Llama.create_completion() could sample past max_tokens when the last allowed token ended inside a UTF-8 character.
With a three-token budget for a, b, and the first byte of é, the previous implementation sampled four tokens and returned abé.

Stop sampling at the configured budget and decode the pending streaming bytes together at that limit.
This preserves complete characters even if they had been buffered as part of a possible stop string.
For example, a five-token budget with stop="éé" returns abé with finish_reason="length".
With six tokens the full stop string is recognized and the completion returns ab with finish_reason="stop".

Validation

  • Deterministic inference using the existing stories15M-q4_0.gguf fixture in both response modes. The eight cases exercise truncated and complete UTF-8, a partial stop prefix, and a complete stop match.
  • The same regression cases produce four failures and four passing controls on the original implementation and all eight pass with the fix.
  • make test and make lint on macOS arm64 with Python 3.12.14 and CPU inference.

Copilot AI balanced review requested due to automatic review settings December 5, 2026 02:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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