Closed
Description
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This is an issue with the Python library
Describe the bug
#980 added token logprobs
to chat completions of type Optional[ChoiceLogprobs]
in openai.types.chat.chat_completion.Choice
and openai.types.chat.chat_completion_chunk.Choice
. In the latter, the default value is set to None
, while in the former it is not set. This causes backward compatibility problems with code written for versions prior to 1.5.0.
To Reproduce
Execution of the following code fails:
from openai.types.chat.chat_completion import ChatCompletionMessage, Choice
msg = ChatCompletionMessage(role="assistant", content="")
Choice(
index=0,
finish_reason="stop",
message=msg,
)
The output
----> 1 Choice(
2 index=0,
3 finish_reason="stop",
4 message=msg,
5 )
File /.venv-3.10/lib/python3.10/site-packages/pydantic/main.py:164, in BaseModel.__init__(__pydantic_self__, **data)
162 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
163 __tracebackhide__ = True
--> 164 __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
ValidationError: 1 validation error for Choice
logprobs
Field required [type=missing, input_value={'index': 0, 'finish_reas...=None, tool_calls=None)}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.5/v/missing
Setting logprobs
to None
fixes the problem.
from openai.types.chat.chat_completion import ChatCompletionMessage, Choice
msg = ChatCompletionMessage(role="assistant", content="")
Choice(
index=0,
finish_reason="stop",
message=msg,
logprobs=None # added line
)
Code snippets
see above
OS
Linux
Python version
Python 3.10.13
Library version
openai 1.6.0