Skip to content

Conversation

@jitokim
Copy link
Contributor

@jitokim jitokim commented Nov 12, 2025

Fixes TypeError when generating JSON schemas: "Object of type 'FieldInfo'
is not JSON serializable". This occurs when model_json_schema() includes
internal Pydantic metadata in the output.

Solution: Add mode='serialization' parameter to model_json_schema() to
ensure only JSON-serializable data structures are generated.

Changes:

  • Modified _convert_pydantic_to_openai_function() to pass mode='serialization'
  • Added test_pydantic_fieldinfo_serialization() to verify correct behavior
  • Resolves nested Pydantic v2 model conversion issues

Impact:

  • Fixes schema generation for nested Pydantic v2 models
  • No breaking changes (Pydantic v1 code path unchanged)
  • Previously failing test_convert_to_openai_function_nested_v2 now passes

@jitokim jitokim requested a review from eyurtsev as a code owner November 12, 2025 04:46
@github-actions github-actions bot added core Related to the package `langchain-core` fix labels Nov 12, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 12, 2025

CodSpeed Performance Report

Merging #33932 will degrade performances by 26%

Comparing jitokim:fix/pydantic-fieldinfo-serialization (d1dafab) with master (83c078f)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

❌ 13 regressions
⏩ 21 skipped1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Mode Benchmark BASE HEAD Change
WallTime test_async_callbacks_in_sync 18.3 ms 24.7 ms -26%
WallTime test_import_time[BaseChatModel] 456.1 ms 521.9 ms -12.61%
WallTime test_import_time[CallbackManager] 397 ms 463 ms -14.25%
WallTime test_import_time[ChatPromptTemplate] 506.9 ms 602.4 ms -15.85%
WallTime test_import_time[Document] 164.9 ms 189.5 ms -13%
WallTime test_import_time[HumanMessage] 235.8 ms 269.2 ms -12.43%
WallTime test_import_time[InMemoryRateLimiter] 157.7 ms 175.3 ms -10.05%
WallTime test_import_time[InMemoryVectorStore] 531.6 ms 619.3 ms -14.15%
WallTime test_import_time[LangChainTracer] 387.8 ms 439.1 ms -11.69%
WallTime test_import_time[PydanticOutputParser] 455.8 ms 530 ms -14%
WallTime test_import_time[RunnableLambda] 433.6 ms 495 ms -12.41%
WallTime test_import_time[Runnable] 427.1 ms 488.5 ms -12.57%
WallTime test_import_time[tool] 445.4 ms 530 ms -15.97%

Footnotes

  1. 21 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

…ntic v2

Fixes TypeError when generating JSON schemas: "Object of type 'FieldInfo'
is not JSON serializable". This occurs when model_json_schema() includes
internal Pydantic metadata in the output.

Solution: Add mode='serialization' parameter to model_json_schema() to
ensure only JSON-serializable data structures are generated.

Changes:
- Modified _convert_pydantic_to_openai_function() to pass mode='serialization'
- Added test_pydantic_fieldinfo_serialization() to verify correct behavior
- Resolves nested Pydantic v2 model conversion issues

Impact:
- Fixes schema generation for nested Pydantic v2 models
- No breaking changes (Pydantic v1 code path unchanged)
- Previously failing test_convert_to_openai_function_nested_v2 now passes

Signed-off-by: jitokim <[email protected]>
@jitokim jitokim force-pushed the fix/pydantic-fieldinfo-serialization branch from ef386ba to bfedad2 Compare November 12, 2025 04:57
result = convert_to_openai_function(MyModel)

# This should not raise an error if FieldInfo objects are properly serialized
json_str = json.dumps(result)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this test dumb and check result against a dict with a single assert?

We want to be able to see immediately what the expected value of result is instead of having to read through different assertions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eyurtsev Thanks for reivew

Signed-off-by: jitokim <[email protected]>
@jitokim jitokim requested a review from eyurtsev November 14, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Related to the package `langchain-core` fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants