Skip to content

Preserve falsy parsed outputs - #1839

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

Preserve falsy parsed outputs#1839
sylvesterkaczmarek wants to merge 1 commit into
anthropics:mainfrom
sylvesterkaczmarek:fix/falsy-parsed-output

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Preserve valid falsy structured outputs returned by messages.parse() and beta.messages.parse().

Both ParsedMessage.parsed_output and ParsedBetaMessage.parsed_output currently use a truthiness check:

if content.type == "text" and content.parsed_output:

As a result, valid parsed outputs such as False, 0, "", [], and {} are incorrectly exposed as None.

Fix

Check explicitly for None instead of truthiness:

if content.type == "text" and content.parsed_output is not None:

This preserves all valid structured-output values while continuing to return None when no parsed output is present.

The fix is applied consistently to both:

  • ParsedMessage
  • ParsedBetaMessage

Regression coverage

Adds focused tests covering:

  • False
  • 0
  • empty string
  • empty list
  • empty dict
  • genuinely absent parsed_output

The regression also verifies that an earlier text block with parsed_output=None does not prevent a later valid falsy parsed value from being returned.

@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner August 16, 2026 23:24
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