fix: resolve subscripted generics in _build_discriminated_union_meta (pydantic v1)#1238
Open
ohxh wants to merge 1 commit intoanthropics:mainfrom
Open
fix: resolve subscripted generics in _build_discriminated_union_meta (pydantic v1)#1238ohxh wants to merge 1 commit intoanthropics:mainfrom
ohxh wants to merge 1 commit intoanthropics:mainfrom
Conversation
…(pydantic v1) `_build_discriminated_union_meta` accesses `variant.__fields__` directly on each Union variant. When a variant is a subscripted generic like `ParsedBetaTextBlock[ResponseFormatT]`, it is a `typing._GenericAlias` (not a class) and `.__fields__` raises `AttributeError` on Python 3.12. The SDK's own `is_basemodel_type` (line 460) already handles this by using `get_origin(type_) or type_` to extract the origin class before inspecting it. This applies the same pattern to the pydantic v1 branch of `_build_discriminated_union_meta`. Only the pydantic v1 code path is affected; pydantic v2 uses `_extract_field_schema_pv2` which handles generics correctly. Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
_build_discriminated_union_metain_models.pyaccessesvariant.__fields__directly on each Union variant. When a variant is a subscripted generic likeParsedBetaTextBlock[ResponseFormatT](fromParsedBetaContentBlock), it is atyping._GenericAlias— not a class — so.__fields__raisesAttributeErroron Python 3.12.This crashes streaming (
client.beta.messages.stream()) becauseaccumulate_eventcallsconstruct_type(type_=ParsedBetaContentBlock, ...)on everycontent_block_startevent, and when pydantic v1'svalidate_typefails to parse the block, it falls through to_build_discriminated_union_metawhich hits this bug.Only the pydantic v1 code path is affected. Pydantic v2 uses
_extract_field_schema_pv2which handles subscripted generics correctly.Fix
Use
get_origin(variant) or variantbefore accessing.__fields__, matching the pattern the SDK's ownis_basemodel_typealready uses (line 460):Repro
Made with Cursor