Skip to content

Conversation

@jitokim
Copy link
Contributor

@jitokim jitokim commented Nov 12, 2025

Description

Fixes "TypeError: Object of type 'FieldInfo' is not JSON serializable" that occurred when using with_structured_output(..., method="json_mode") with Pydantic v2 models containing Field metadata.

The issue occurred because model_json_schema() was called without the mode parameter, causing Pydantic v2 to use validation mode which can include non-serializable FieldInfo objects in certain contexts. Adding mode='serialization' tells Pydantic to use its serialization schema generation mode, which properly handles FieldInfo objects.

Using mode='serialization' is also semantically correct since the schema describes the model's output format (what Gemini should generate), not input validation rules.

Changes:

  • Add mode='serialization' to model_json_schema() in chat_models.py:2562
  • Add unit test for Pydantic v2 FieldInfo serialization
  • Add unit test for Pydantic v1 backward compatibility
  • Verify both Pydantic v1 (schema() method) and v2 paths work correctly

Backward compatibility:

  • Pydantic v1 models: Continue using schema() method (line 2560)
  • Pydantic v2 models: Now use model_json_schema(mode='serialization')
  • No public API changes, fully backward compatible

Relevant issues

Type

🆕 New Feature
🐛 Bug Fix
🧹 Refactoring
📖 Documentation
🚄 Infrastructure
✅ Test

Changes(optional)

Testing(optional)

Note(optional)

Fixes "TypeError: Object of type 'FieldInfo' is not JSON serializable"
that occurred when using with_structured_output(..., method="json_mode")
with Pydantic v2 models containing Field metadata.

The issue occurred because model_json_schema() was called without the
mode parameter, causing Pydantic v2 to use validation mode which can
include non-serializable FieldInfo objects in certain contexts. Adding
mode='serialization' tells Pydantic to use its serialization schema
generation mode, which properly handles FieldInfo objects.

Using mode='serialization' is also semantically correct since the schema
describes the model's output format (what Gemini should generate), not
input validation rules.

Changes:
- Add mode='serialization' to model_json_schema() in chat_models.py:2562
- Add unit test for Pydantic v2 FieldInfo serialization
- Add unit test for Pydantic v1 backward compatibility
- Verify both Pydantic v1 (schema() method) and v2 paths work correctly

Backward compatibility:
- Pydantic v1 models: Continue using schema() method (line 2560)
- Pydantic v2 models: Now use model_json_schema(mode='serialization')
- No public API changes, fully backward compatible

Signed-off-by: jitokim <[email protected]>
@jitokim jitokim force-pushed the fix/pydantic-fieldinfo-serialization branch from 08399cd to ff98a8d Compare November 12, 2025 05:06
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.

1 participant