Skip to content

Conversation

@zzstoatzz
Copy link
Collaborator

Summary

Fixes serialization failures when using parameterized Pydantic generic models (e.g., APIResult[str]) with the JSON serializer.

The Problem

When serializing a parameterized generic like APIResult[str], the JSON encoder was storing the class name with brackets:

{"__class__": "module.APIResult[str]", "data": {...}}

This causes deserialization to fail because Python's import system cannot import APIResult[str] - only APIResult.

The Fix

Extract the origin class from Pydantic's __pydantic_generic_metadata__ when encoding, so the serialized output uses the importable base class:

{"__class__": "module.APIResult", "data": {...}}

Test plan

  • Added regression test test_pydantic_generic_model_roundtrip
  • All existing serializer tests pass

🤖 Generated with Claude Code

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 18, 2025

CodSpeed Performance Report

Merging #19868 will not alter performance

Comparing fix/json-serializer-pydantic-generics (139b051) with main (080230e)

Summary

✅ 2 untouched

When using parameterized Pydantic generic models like `APIResult[str]`,
the JSON serializer was storing class names with brackets (e.g.,
`module.APIResult[str]`) which cannot be imported by Python's import
system, causing deserialization to fail.

This fix extracts the origin class from Pydantic's generic metadata,
ensuring the serialized class name is importable (e.g., `module.APIResult`).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@zzstoatzz zzstoatzz force-pushed the fix/json-serializer-pydantic-generics branch from cf30eb0 to 7a91c0c Compare December 18, 2025 17:05
Ensures the Pydantic generic fix doesn't break standard Generic dataclasses.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@zzstoatzz zzstoatzz marked this pull request as ready for review December 18, 2025 17:38
@zzstoatzz zzstoatzz merged commit bcc62ca into main Dec 18, 2025
58 checks passed
@zzstoatzz zzstoatzz deleted the fix/json-serializer-pydantic-generics branch December 18, 2025 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants