Skip to content

fix(types): preserve falsy parsed_output values - #2036

Open
feiiiiii5 wants to merge 1 commit into
anthropics:mainfrom
feiiiiii5:anthropic-sdk-python-C-207
Open

feiiiiii5 wants to merge 1 commit into
anthropics:mainfrom
feiiiiii5:anthropic-sdk-python-C-207

Conversation

@feiiiiii5

Copy link
Copy Markdown

ParsedMessage.parsed_output and ParsedBetaMessage.parsed_output return None for valid falsy structured outputs. A response whose parsed value is 0, False, "", [], or {} is indistinguishable from a response that was not parsed at all, for both non-streaming messages.parse() and streaming stream.get_final_message().

The parser stores the value correctly on the text block, but the property tests content.parsed_output for truthiness before returning it. None is the "not parsed" sentinel; every other value is a parsed output.

The fix checks content.parsed_output is not None in both ParsedMessage and ParsedBetaMessage. Tests cover all five falsy values for both message types and both sync and async streaming.

Test: the new tests fail on 202db72c (assert None == 0, None == False, None == "", None == [], None == {}) and pass on this branch (12 passed). The broader tests/lib/streaming, tests/lib/_parse, and tests/lib/threads suites are 715 passed; the Pydantic v1 targeted run is 15 passed. ./scripts/lint is clean (ruff, dependency caps, pyright, import smoke).

Details

A legitimate JSON null output remains indistinguishable from "not parsed" because parsed_output is Optional[ResponseFormatT]; that pre-existing type ambiguity is not changed here. No live API calls are made; streaming tests replay fixtures through respx.

The parsed-output property checked the parsed value for truthiness, so
0, False, an empty string, list, or dict were returned as None and
looked like an unparsed block. None is the not-parsed sentinel; return
every other value in both ParsedMessage and ParsedBetaMessage.
@feiiiiii5
feiiiiii5 requested a review from a team as a code owner October 26, 2026 06:59

This branch has not been deployed

No deployments
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