-
Notifications
You must be signed in to change notification settings - Fork 19.7k
fix(core): Resolve 'FieldInfo is not JSON serializable' error in Pydantic v2 #33932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix(core): Resolve 'FieldInfo is not JSON serializable' error in Pydantic v2 #33932
Conversation
CodSpeed Performance ReportMerging #33932 will degrade performances by 26%Comparing
|
| 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
-
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]>
ef386ba to
bfedad2
Compare
| result = convert_to_openai_function(MyModel) | ||
|
|
||
| # This should not raise an error if FieldInfo objects are properly serialized | ||
| json_str = json.dumps(result) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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]>
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:
Impact: