Skip to content

Preserve structured output stop reasons - #1850

Open
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/parsed-output-stop-reasons
Open

Preserve structured output stop reasons#1850
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/parsed-output-stop-reasons

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Preserve structured-output responses when generation terminates with stop_reason="refusal" or "max_tokens" instead of replacing the response with a schema-validation exception.

messages.parse() and the beta equivalent currently attempt to validate every text block against output_format unconditionally. Refusal text and max-token-truncated JSON are not guaranteed to satisfy the requested output schema, so those terminal responses can fail inside Pydantic/JSON parsing before the caller can inspect the model's actual stop_reason.

For example, a refusal containing ordinary refusal text is currently fed to TypeAdapter.validate_json(), and a response truncated at {"value": is treated as malformed structured output. In both cases the SDK loses the more important information: why generation stopped.

Fix

Skip structured-output validation only for:

  • refusal
  • max_tokens

The text remains available unchanged, parsed_output is None, and the original stop_reason is preserved.

Normal completed responses continue through the existing validation path, so malformed structured output on an ordinary completed turn still raises as before.

The behavior is applied symmetrically to GA and beta parsed messages.

Regression coverage

Adds tests verifying:

  • refusal text returns with parsed_output=None for GA and beta;
  • truncated JSON from max_tokens returns with parsed_output=None;
  • the original stop reason is preserved;
  • invalid structured output on a normally completed response still raises a validation error.

@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner August 17, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant