From dcbdac85a8757cb1381887996a84c0c0ab415f3f Mon Sep 17 00:00:00 2001 From: Surya Chandrakasan <65883542+suryacks@users.noreply.github.com> Date: Fri, 4 Dec 2026 19:16:58 -0600 Subject: [PATCH 1/2] fix: reject logit_bias token ids outside the vocabulary 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. --- CHANGELOG.md | 1 + llama_cpp/llama.py | 6 ++++++ tests/test_llama.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa7e917b..7babc9c70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- fix: reject logit_bias token ids outside the vocabulary by @suryacks in #XXXX - fix: load cached pretrained models in offline mode by @emme1t in #2396 - fix(ci): install matching NVML headers for CUDA wheel builds diff --git a/llama_cpp/llama.py b/llama_cpp/llama.py index 1b4daf6b9..c04312ae9 100644 --- a/llama_cpp/llama.py +++ b/llama_cpp/llama.py @@ -1321,6 +1321,12 @@ def _create_completion( # because of the extra space added to the start of the prompt_tokens if logit_bias is not None: logit_bias_map = {int(k): float(v) for k, v in logit_bias.items()} + for input_id in logit_bias_map: + if not 0 <= input_id < self._n_vocab: + raise ValueError( + f"logit_bias token id {input_id} is out of range for a " + f"vocabulary of {self._n_vocab} tokens" + ) def logit_bias_processor( input_ids: npt.NDArray[np.intc], diff --git a/tests/test_llama.py b/tests/test_llama.py index 8c1512d49..99b37ea7e 100644 --- a/tests/test_llama.py +++ b/tests/test_llama.py @@ -662,3 +662,35 @@ def test_real_llama_embeddings(llama_cpp_embedding_model_path): repeated_embeddings = model.embed(list(reversed(prompts))) assert len(repeated_embeddings) == len(prompts) assert all(len(repeated) == len(embedding) for repeated in repeated_embeddings) + + +def test_logit_bias_rejects_out_of_vocab_token_ids(llama_cpp_transformer_model_path): + model = llama_cpp.Llama( + llama_cpp_transformer_model_path, + n_ctx=32, + n_batch=32, + n_ubatch=32, + logits_all=False, + verbose=False, + ) + n_vocab = model.n_vocab() + + for token_id in (n_vocab, -1): + with pytest.raises(ValueError, match="logit_bias token id"): + model.create_completion( + "Once upon a time", + max_tokens=1, + temperature=0.0, + logit_bias={str(token_id): 100.0}, + ) + + target = model.tokenize(b" dragon", add_bos=False)[0] + output = model.create_completion( + "Once upon a time", + max_tokens=1, + temperature=0.0, + logit_bias={str(target): 100.0}, + ) + assert output["choices"][0]["text"] == model.detokenize([target]).decode() + + model.close() From 46d181905ad6c0239a7978108078c80fbee55a3e Mon Sep 17 00:00:00 2001 From: Surya Chandrakasan <65883542+suryacks@users.noreply.github.com> Date: Fri, 4 Dec 2026 19:27:43 -0600 Subject: [PATCH 2/2] docs: add PR number to changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7babc9c70..cf393dbd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- fix: reject logit_bias token ids outside the vocabulary by @suryacks in #XXXX +- fix: reject logit_bias token ids outside the vocabulary by @suryacks in #2480 - fix: load cached pretrained models in offline mode by @emme1t in #2396 - fix(ci): install matching NVML headers for CUDA wheel builds