Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Empty dict serialization for dataclass subclasses in union types (Pydantic 2.12.4) #12553

Open
Assignees
davidhewitt
Labels
bug V2Bug related to Pydantic V2
@kusaku

Description

@kusaku

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

Metadata

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp