Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.3k
Open
Description
Initial Checks
- I confirm that I'm using Pydantic V2
Description
The core schema defined with__get_pydantic_core_schema__ on a stdlib dataclass does not always work, when the class is used a field in other types. Switching toBaseModel or even pydantics dataclasses fixes this, but is unfortunately not an option for me.
The issue also occurs if the second usage of the type is slightly different, likeFoo &list[Foo].
I encountered this when updating from version 2.10.6 to 2.11, but it also happens with 2.12.
Example Code
importdataclassesfrompprintimportpprintfrompydanticimportGetCoreSchemaHandler,TypeAdapterfrompydantic_coreimportcore_schema,CoreSchema@dataclasses.dataclassclassFoo:field:str@classmethoddef__get_pydantic_core_schema__(cls,source_type:CoreSchema,handler:GetCoreSchemaHandler )->CoreSchema:returncore_schema.no_info_before_validator_function(lambda_: {"field":"overridden"},schema=handler(source_type),serialization=core_schema.plain_serializer_function_ser_schema(lambda_: {"field":"overridden"},info_arg=False, ), )@dataclasses.dataclassclassBar:a:Foob:Fooif__name__=='__main__':adapter=TypeAdapter(Bar)pprint(adapter.core_schema)print(adapter.dump_python(Bar(a=Foo(field="a"),b=Foo(field="b"), ) ) )# {"a": {"field": "overridden"}, "b": {"field": "b"}}# -> should be both {"field": "overridden"}print(adapter.validate_python( {"a": {"field":"a"},"b": {"field":"b"}, } ) )# Bar(a=Foo(field='overridden'), b=Foo(field='b'))# -> should be both Foo(field='overridden')
Python, Pydantic & OS Version
pydantic version: 2.12.4 pydantic-core version: 2.41.5 pydantic-core build: profile=release pgo=false python version: 3.13.9 (main, Oct 14 2025, 21:29:44) [Clang 20.1.4 ] platform: Linux-6.8.0-87-generic-x86_64-with-glibc2.39 related packages: typing_extensions-4.15.0 mypy-1.18.2 fastapi-0.121.2 commit: unknown