What this thread does
Do first: pass the Pydantic model class as response_format to client.chat.completions.parse() inside an exception boundary before reading message.parsed.
Short answer
Diagnostic: Invalid JSON: EOF while parsing a value
As of November 2026, OpenAI Python 3.58.0 provides client.chat.completions.parse. The helper raises for length/content-filter termination before returning the parsed completion. A completed transport response is therefore not sufficient evidence of a usable domain object. Helper contract.
Choose / don't choose
- Typed structured output: choose the parsing helper and a Pydantic class when the selected model supports the schema contract; don't pass a serialized class instance as the type declaration.
- Refusal or filtered output: handle as a separate application result; don't regenerate solely to bypass the refusal/filter.
- Malformed or truncated output: choose a bounded fresh request only when policy, budget and operation semantics permit; don't invent missing JSON fields or close braces and accept fabricated data.
Not for: Claude schema limits; see claude-api-structured-outputs-schema-limits.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
response_format | Pass a Pydantic model to the parse method | Current Python helper | helpers |
max_completion_tokens | An upper bound including visible output tokens and reasoning tokens | Current Chat Completions schema | request types |
n | Omit for one choice; newer models reject explicit n, even n=1 | Current Chat Completions schema | request types |
max_retries | Default 2 retries for certain errors; 0 disables SDK retries | Current Python SDK | retry contract |
SDK transport retries are separate from an application retry after parsing failure; a ValidationError is not a documented automatic transport retry case. Count both layers in the application's request budget.
Order of operations
1. Select a supported model/schema pair and an output budget, accounting for visible output tokens and reasoning tokens. Shorten the requested result if it cannot fit; a larger budget cannot exceed the model limit. request budget. 2. Wrap the call itself: handle Pydantic ValidationError, LengthFinishReasonError and ContentFilterFinishReasonError before attempting to read a result. The parser implementation raises on length or content_filter; the length exception retains its completion for diagnosis. 3. After a successful return, inspect the intended choice's finish_reason. For a text-only schema acceptance policy, require stop; route tool-call or other endings separately. Do not expect post-return code to catch length termination already raised by the helper. 4. Inspect choice.message.refusal before accepting choice.message.parsed. Route a refusal separately, then require message.parsed is not None. The parser only attempts rich content parsing when content is present and there is no refusal. Parser gates. 5. Apply domain validation before side effects. On malformed output, a conservative example policy permits at most one new generation with a clarified size/schema request; on truncation, reduce requested content or raise the allowed output budget within model limits. This retry count is an application policy, not an SDK default. Never apply a partial object's business effects and then retry the whole operation.
Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|---|---|---|
Invalid JSON: EOF while parsing a value | Content could not be parsed into the expected object; do not assume the cause from EOF alone | Catch ValidationError around the call, classify completion/budget where available and reject partial data | marinomaria, September 30, 2024, current parser |
Could not parse response content as the length limit was reached | Parser raises LengthFinishReasonError when choice.finish_reason is length | Handle LengthFinishReasonError; alter requested size or valid budget before a bounded new request | parser |
message.parsed is None | The content parser returns None unless a rich response format, content and no refusal are present | Check refusal first; fail closed on absent parsed data | parser |
Missing
- No universal
max_completion_tokensvalue follows without a selected model and output size. Consult that model's current official limit. - The historical EOF report does not establish a current fixed-in release or a single root cause. Current parser gates and exception handling remain necessary.
- A local JSON repair policy cannot recover information that was never generated; no automatic partial-data acceptance is specified here.
Checked
2026-11-24; OpenAI Python 3.58.0 release, helpers/README, request types, parser/exception source and marinomaria's September 30, 2024 issue rechecked. No model requests executed.
Started from a post by @OpenAI, packaged by the editor. Original post