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
When a PydanticBaseModel field uses a union type (BaseModel | JSONMixin) and contains a nestedBaseModel with a dataclass subclass instance,model_dump() produces an empty dict{} in Pydantic 2.12.4. This works correctly in Pydantic 2.12.3.
Version Information
- Pydantic version: 2.12.4 (bug occurs) / 2.12.3 (works correctly)
- pydantic-core version: 2.41.5 (bug occurs) / 2.41.4 (works correctly)
- Python version: 3.12+
- OS: Any
Example Code
importdataclassesimportfastclasses_jsonimportpydantic@fastclasses_json.dataclass_json@dataclasses.dataclassclassBaseError(fastclasses_json.JSONMixin):code:strmessage:str@fastclasses_json.dataclass_json@dataclasses.dataclassclassNetworkError(BaseError):code:str=dataclasses.field(default='network_error',init=False)classResult(pydantic.BaseModel):error:BaseErrorclassContainer(pydantic.BaseModel):model_config=pydantic.ConfigDict(arbitrary_types_allowed=True)# Required for JSONMixin in unionmessage:pydantic.BaseModel|fastclasses_json.JSONMixindefmain()->None:print(f"Pydantic{pydantic.__version__}\n")base_error=BaseError(code='base',message='test')network_error=NetworkError(message='test')network_error_dict=NetworkError(message='test').to_dict()container_base=Container(message=Result(error=base_error))container_network=Container(message=Result(error=network_error))container_network_dict=Container(message=Result(error=network_error_dict))print(f"BaseError:{'✓'ifcontainer_base.model_dump()['message']!= {}else'✗'}")print(f"NetworkError:{'✓'ifcontainer_network.model_dump()['message']!= {}else'✗✗✗ BUG'}")print(f"Convert to dict:{'✓'ifcontainer_network_dict.model_dump()['message']!= {}else'✗'}")if__name__=='__main__':main()
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.12.11 (main, Jul 31 2025, 12:08:01) [Clang 17.0.0 (clang-1700.0.13.5)] platform: macOS-15.5-arm64-arm-64bit related packages: typing_extensions-4.15.0 commit: unknown