Skip to content

fix: stop completions at the earliest matching sequence - #2460

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

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

Conversation

@remote-controlled-man

Copy link
Copy Markdown
Contributor

What changed

When multiple stop strings become visible in one generated token, non-streaming completions use the first string in the input list rather than the earliest match in the generated text. This can leak part of an earlier stop string and disagree with streaming.

Choose the minimum matched byte offset, using the same rule as the streaming flush. No buffering, tokenization, or sampler behavior changes.

The regression uses the existing Qwen fixture and public logit_bias to hold the sampled token constant. It checks both stop-list orders, overlapping matches, a single effective match, and a nonmatching control, with and without streaming/logprobs.

Reproducer

This uses a published checkpoint and its natural greedy output; no logit bias is needed.

from llama_cpp import Llama

llm = Llama.from_pretrained(
    repo_id="lmstudio-community/Qwen3.5-0.8B-GGUF",
    filename="Qwen3.5-0.8B-Q8_0.gguf",
    n_ctx=256, n_batch=64, n_ubatch=64, n_gpu_layers=0, verbose=False,
)
try:
    kwargs = dict(
        prompt="Once upon a time, there was a",
        max_tokens=6, temperature=0, seed=1234, stop=["ry", "er"],
    )
    plain = llm.create_completion(**kwargs)["choices"][0]["text"]
    streamed = "".join(
        chunk["choices"][0]["text"]
        for chunk in llm.create_completion(stream=True, **kwargs)
    )
    assert plain == streamed == " v"
finally:
    llm.close()

On main (77fbb5d), plain is " ve" and streamed is " v". With this fix both are " v".

Validation

On macOS arm64; the regression and reproducer use CPU (n_gpu_layers=0).

  • make test: 30 passed, no skips, including the four parametrized regression tests covering 24 combinations.
  • make lint: Ruff lint and formatting checks passed for all 29 source/test files.
  • git diff --check 77fbb5d HEAD: passed.
  • The regression fails on the original implementation and passes with the fix. The public reproducer above also passes.

Other platforms and GPU inference were not separately tested.

AI-assisted implementation.

Copilot AI balanced review requested due to automatic review settings November 27, 2026 16:14

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