beta.chat.completions.parse returns unhandled ValidationError
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 31.6k
- Forks
- 5.7k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 96
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
In some occasions while using the Completion API with Structured Outputs, the SDK fails and returns a ValidationError:
ValidationError: 1 validation error for RawResponse
Invalid JSON: EOF while parsing a value at line 1 column 600 [type=json_invalid, input_value=' ... ', input_type=str]
For further information visit https://errors.pydantic.dev/2.9/v/json_invalid
This does not happen every time, but we use it in a production service and this unpredictable behavior is hard to prevent.
To Reproduce
- Create a Pydantic model
- Instantiate an OpenAI client
- Use the method
OpenAI.beta.chat.completions.parse(...)with the following arguments - Repeat a few times for seeing the error
from pydantic import BaseModel
from openai import OpenAI
class RawResponse(BaseModel):
answer: str
client = OpenAI(api_key=...)
completion = client.beta.chat.completions.parse(
model='gpt-4o-2024-08-06',
messages=messages,
max_tokens=750,
n=1,
stop=None,
temperature=0.1,
response_format=RawResponse
)
After a few times, this fails with:
ValidationError: 1 validation error for RawResponse
Invalid JSON: EOF while parsing a value at line 1 column 600 [type=json_invalid, input_value=' ... ', input_type=str]
For further information visit https://errors.pydantic.dev/2.9/v/json_invalid
Code snippets
No response
OS
debian:bullseye-slim
Python version
CPython 3.10.8
Library version
openai 1.48.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at OpenAI.beta.chat.completions.parse(...) and reproduce the intermittent Structured Outputs failure with the supplied Pydantic model. Trace where the response becomes RawResponse and inspect the error-handling boundary; done means the reproduction is covered by a regression check and no longer surfaces an unhandled ValidationError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100