Conversation
…l_fields Pydantic 2.12 raises PydanticUserError when config= is passed to TypeAdapter and the resolved inner type is a BaseModel, dataclass, or TypedDict. The guard in get_model_fields() only checked bare BaseModel/dataclass types via lenient_issubclass, which does not unwrap Annotated. As a result, Json[SerializeAsAny[Inner]] (which expands to Annotated[Inner, SerializeAsAny(), Json] at runtime) slipped through and caused a crash in app.openapi(). Extract a _needs_no_config() helper that recursively unwraps Annotated before checking the inner type, covering all affected forms: - Annotated[BaseModel, ...] - Json[BaseModel] → Annotated[BaseModel, Json] - Json[SerializeAsAny[BaseModel]] → Annotated[BaseModel, SerializeAsAny(), Json] Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
get_model_fields()infastapi/_compat/v2.pywas passingmodel.model_configto
TypeAdapterfor fields whose annotation isJson[SerializeAsAny[SomeModel]].Pydantic 2.12 tightened its validation and now raises
PydanticUserErrorin thiscase, crashing
app.openapi().Root Cause
get_model_fields()decided whether to suppressconfig=by checking:Pydantic raises
PydanticUserErrorwhenconfig=is passed toTypeAdapterwhose resolved type is a
BaseModel, dataclass, orTypedDict.When
Source[SerializeAsAny[Inner]]is resolved, the field annotation becomesJson[SerializeAsAny[Inner]].At runtime
Json[X]expands toAnnotated[X, Json]andSerializeAsAny[X]expands to
Annotated[X, SerializeAsAny()], so the full type isAnnotated[Annotated[Inner, SerializeAsAny()], Json].lenient_issubclassreturns
Falsefor this (it is not a bareBaseModel, dataclass, orTypedDict), somodel_configwas forwarded toTypeAdapter— which Pydantic2.12 now rejects.
Source[Inner](withoutSerializeAsAny) did not crash because in that casefield_info.annotationresolves toInneritself (a bareBaseModelsubclass),so
lenient_issubclassreturnsTrueandmodel_configis already suppressedbefore
TypeAdapteris ever called. TheSerializeAsAnywrapper is whatprevented this guard from triggering: the annotation becomes
Json[SerializeAsAny[Inner]]rather thanInner, andlenient_issubclassreturns
Falsefor that composite type.Changes
fastapi/_compat/v2.pyExtracted a
_needs_no_config()helper that recursively unwrapsAnnotatedbefore checking the inner type:
Because
Json[X]is itselfAnnotated[X, Json]at runtime, andSerializeAsAny[X]isAnnotated[X, SerializeAsAny()], the recursiveAnnotated-unwrapping branch covers all affected forms (two levels ofrecursion are needed for the
SerializeAsAnycase):Annotated[BaseModel subclass, ...]Json[BaseModel subclass]→Annotated[BaseModel subclass, Json]Json[SerializeAsAny[BaseModel subclass]]→
Annotated[SerializeAsAny[BaseModel subclass], Json]→
Annotated[Annotated[BaseModel subclass, SerializeAsAny()], Json]→ unwraps twice to reach
BaseModel subclasstests/test_json_field_serialize_as_any.py(new)Two tests:
test_openapi_schema_generation_serialize_as_any_does_not_raise— regression for the reported crashtest_openapi_schema_generation_plain_inner_unaffected— baseline to confirmSource[Inner]continues to workPoC