Skip to content

fix: reject logit_bias token ids outside the vocabulary - #2480

Open
suryacks wants to merge 2 commits into
abetlen:mainfrom
suryacks:fix/logit-bias-out-of-vocab
Open

suryacks wants to merge 2 commits into
abetlen:mainfrom
suryacks:fix/logit-bias-out-of-vocab

Conversation

@suryacks

@suryacks suryacks commented Dec 5, 2026

Copy link
Copy Markdown

What broke

A logit_bias entry whose token id is outside the vocabulary silently turns off the whole logit_bias for that request, including the valid entries.
On the released 0.3.38 server with Qwen3-0.6B (vocab 151936), temperature=0, prompt "The capital of France is":

logit_bias={"43096": 100}               -> " banana banana banana"
logit_bias={"43096": 100, "999999": 0}  -> " Paris. The"   (bias on 43096 ignored, HTTP 200)
logit_bias={"-1": 100}                  -> ""              (biases token 151935, HTTP 200)

The server log gets one traceback per generated token:

Exception ignored on calling ctypes callback function: <function CustomSampler.__init__.<locals>.apply_wrapper ...>
IndexError: index 999999 is out of bounds for axis 0 with size 151936

Why

logit_bias_processor indexes the scores array with each id.
It runs inside the custom sampler's ctypes callback, and ctypes prints and swallows exceptions raised there, so the IndexError never reaches the caller and the biased copy of the scores is thrown away.
A negative id is a valid numpy index, so -1 biases the last vocabulary entry instead of failing.

Fix

_create_completion checks every logit_bias id against 0 <= id < n_vocab when it builds the map and raises ValueError naming the bad id.
The chat path goes through the same code, so /v1/chat/completions gets the same error.

Tests

  • test_logit_bias_rejects_out_of_vocab_token_ids (stories15M): n_vocab and -1 raise ValueError, and a valid id still forces its token. Fails on main with DID NOT RAISE, passes with the fix.
  • pytest tests: 26 passed. test_llama_cpp_tokenization fails with and without the change because the vendor/llama.cpp vocab file wasn't checked out locally.
  • ruff check and ruff format --check clean.
  • Patched the installed 0.3.38 package and reran the requests above: the out-of-range ones return an error naming the id, the valid bias still gives " banana banana banana", and later requests are fine.

An out-of-range id made the logit bias callback raise IndexError inside the ctypes sampler callback, where the exception is swallowed, so every bias in the request was silently dropped for every token. Negative ids indexed from the end of the vocabulary instead of failing.

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.

1 participant