fix(types): pass nonconforming non-streaming logprobs through instead of failing the response

This commit is contained in:
mateo-berri 2026-10-29 17:48:11 -07:00
parent f38dfce30e
commit 6e4d63b9aa
2 changed files with 40 additions and 1 deletions

View file

@ -1785,7 +1785,7 @@ class Choices(SafeAttributeModel, OpenAIObject):
params["message"] = Message(**dump)
if logprobs is not None:
if isinstance(logprobs, dict):
params["logprobs"] = ChoiceLogprobs(**logprobs)
params["logprobs"] = CHOICE_LOGPROBS_OR_JSON.validate_python(logprobs)
else:
params["logprobs"] = logprobs
else:

View file

@ -1,6 +1,7 @@
import asyncio
import importlib
import json
from collections.abc import Mapping
from datetime import datetime, timedelta
from typing import Final
@ -1111,6 +1112,44 @@ def test_convert_to_model_response_object_with_logprobs():
assert result.system_fingerprint is None
assert result._hidden_params is not None
@pytest.mark.parametrize(
"upstream_logprobs",
[
pytest.param({"content": [{"token": "Hi", "logprob": None}]}, id="null-content-logprob"),
pytest.param(
{
"content": [{"token": "Hi", "logprob": -0.1, "top_logprobs": []}],
"refusal": [{"token": "I", "logprob": None}],
},
id="null-refusal-logprob",
),
],
)
def test_convert_to_model_response_object_passes_nonconforming_logprobs_through(
upstream_logprobs: Mapping[str, object],
) -> None:
result: Final = convert_to_model_response_object(
response_object={
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1702685778,
"model": "test-model",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "Hi"},
"logprobs": upstream_logprobs,
"finish_reason": "stop",
}
],
},
model_response_object=ModelResponse(),
)
assert json.loads(result.model_dump_json())["choices"][0]["logprobs"] == upstream_logprobs
def test_convert_to_model_response_object_error():
"""Test error handling for None response object."""
with pytest.raises(Exception, match="Error in response object format"):