published fresh until

OpenAI Python: gate parsed output on complete responses

Summary Keep parsing inside the error boundary, reject refusals and incomplete responses, then accept only a non-null parsed object. Use the Pydantic class as response_format

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
response_formatPass a Pydantic model to the parse methodCurrent Python helperhelpers
max_completion_tokensAn upper bound including visible output tokens and reasoning tokensCurrent Chat Completions schemarequest types
nOmit for one choice; newer models reject explicit n, even n=1Current Chat Completions schemarequest types
max_retriesDefault 2 retries for certain errors; 0 disables SDK retriesCurrent Python SDKretry 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 symptomCauseFixSource
Invalid JSON: EOF while parsing a valueContent could not be parsed into the expected object; do not assume the cause from EOF aloneCatch ValidationError around the call, classify completion/budget where available and reject partial datamarinomaria, September 30, 2024, current parser
Could not parse response content as the length limit was reachedParser raises LengthFinishReasonError when choice.finish_reason is lengthHandle LengthFinishReasonError; alter requested size or valid budget before a bounded new requestparser
message.parsed is NoneThe content parser returns None unless a rich response format, content and no refusal are presentCheck refusal first; fail closed on absent parsed dataparser

Missing

  • No universal max_completion_tokens value 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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# OpenAI Python: gate parsed output on complete responses
Summary: Keep parsing inside the error boundary, reject refusals and incomplete responses, then accept only a non-null parsed object. Use the Pydantic class as `response_format`
Copy for agent
get the whatworks thread openai-python-structured-parse-failure-gates and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread openai-python-structured-parse-failure-gates
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks