From 713233477d4087cf97d2f04978f9e627ce992b66 Mon Sep 17 00:00:00 2001
From: sohailshk <122166523+sohailshk@users.noreply.github.com>
Date: Sat, 5 Dec 2026 01:20:30 +0530
Subject: [PATCH] fix: return token logprobs for chat completions that set
logprobs without top_logprobs
The chat handlers passed top_logprobs straight to create_completion as logprobs, so logprobs=true with top_logprobs unset became logprobs=None and the response had logprobs: null. Request zero extra candidates in that case so the sampled token's logprob is returned, as the OpenAI API does.
---
CHANGELOG.md | 1 +
llama_cpp/llama_chat_format.py | 10 ++++----
tests/test_llama_chat_format.py | 45 +++++++++++++++++++++++++++++++++
3 files changed, 51 insertions(+), 5 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bfa7e917bd..e6f5294a65 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- fix: load cached pretrained models in offline mode by @emme1t in #2396
- fix(ci): install matching NVML headers for CUDA wheel builds
+- fix: return token logprobs for chat completions that set logprobs without top_logprobs
## [0.3.38]
diff --git a/llama_cpp/llama_chat_format.py b/llama_cpp/llama_chat_format.py
index 62e2364157..1621c2e12b 100644
--- a/llama_cpp/llama_chat_format.py
+++ b/llama_cpp/llama_chat_format.py
@@ -728,7 +728,7 @@ def chat_completion_handler(
top_k=top_k,
min_p=min_p,
typical_p=typical_p,
- logprobs=top_logprobs if logprobs else None,
+ logprobs=(top_logprobs or 0) if logprobs else None,
stream=stream,
stop=stop,
seed=seed,
@@ -3096,7 +3096,7 @@ def __call__(
top_k=top_k,
min_p=min_p,
typical_p=typical_p,
- logprobs=top_logprobs if logprobs else None,
+ logprobs=(top_logprobs or 0) if logprobs else None,
stream=stream,
stop=stop,
seed=seed,
@@ -3631,7 +3631,7 @@ def raise_exception(message: str):
top_k=top_k,
min_p=min_p,
typical_p=typical_p,
- logprobs=top_logprobs if logprobs else None,
+ logprobs=(top_logprobs or 0) if logprobs else None,
stream=stream,
stop=stop,
seed=seed,
@@ -4288,7 +4288,7 @@ def chatml_function_calling(
model=model,
logits_processor=logits_processor,
grammar=grammar,
- logprobs=top_logprobs if logprobs else None,
+ logprobs=(top_logprobs or 0) if logprobs else None,
),
stream=stream,
)
@@ -4401,7 +4401,7 @@ def chatml_function_calling(
typical_p=typical_p,
stream=stream,
stop=["<|im_end|>"],
- logprobs=top_logprobs if logprobs else None,
+ logprobs=(top_logprobs or 0) if logprobs else None,
max_tokens=None,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty,
diff --git a/tests/test_llama_chat_format.py b/tests/test_llama_chat_format.py
index 18c7279cf0..32d244d7e0 100644
--- a/tests/test_llama_chat_format.py
+++ b/tests/test_llama_chat_format.py
@@ -92,3 +92,48 @@ def test_hf_tokenizer_config_str_to_chat_formatter():
)
assert chat_formatter_respoonse.prompt == ("[INST] Hello, world! [/INST]")
+
+
+class _RecordingLlama:
+ verbose = False
+
+ def __init__(self):
+ self.kwargs = {}
+
+ def tokenize(self, text, add_bos=True, special=False):
+ return [1]
+
+ def create_completion(self, **kwargs):
+ self.kwargs = kwargs
+ return {
+ "id": "cmpl-1",
+ "object": "text_completion",
+ "created": 0,
+ "model": "m",
+ "choices": [
+ {"text": "a", "index": 0, "logprobs": None, "finish_reason": "stop"}
+ ],
+ "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
+ }
+
+
+def test_chat_logprobs_without_top_logprobs_requests_token_logprobs():
+ handler = llama_chat_format.Jinja2ChatFormatter(
+ template="{% for m in messages %}{{ m['content'] }}{% endfor %}",
+ eos_token="",
+ bos_token="",
+ ).to_chat_handler()
+ messages = [ChatCompletionRequestUserMessage(role="user", content="hi")]
+
+ llama = _RecordingLlama()
+ handler(llama=llama, messages=messages, logprobs=True)
+ assert llama.kwargs["logprobs"] == 0
+
+ handler(llama=llama, messages=messages, logprobs=True, top_logprobs=3)
+ assert llama.kwargs["logprobs"] == 3
+
+ handler(llama=llama, messages=messages, logprobs=False, top_logprobs=3)
+ assert llama.kwargs["logprobs"] is None
+
+ handler(llama=llama, messages=messages)
+ assert llama.kwargs["logprobs"] is None