fix(core): improve error message for non-JSON-serializable tool schemas#34376
Merged
ccurme (ccurme) merged 3 commits intoFeb 22, 2026
Merged
Conversation
Merging this PR will improve performance by 35.11%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | WallTime | test_import_time[LangChainTracer] |
487.6 ms | 419.7 ms | +16.18% |
| ⚡ | WallTime | test_import_time[HumanMessage] |
287.6 ms | 240.1 ms | +19.76% |
| ⚡ | WallTime | test_import_time[InMemoryVectorStore] |
659.2 ms | 542.4 ms | +21.53% |
| ⚡ | WallTime | test_async_callbacks_in_sync |
24.5 ms | 18.1 ms | +35.11% |
| ⚡ | WallTime | test_import_time[CallbackManager] |
344.5 ms | 289.6 ms | +18.96% |
| ⚡ | WallTime | test_import_time[tool] |
601.5 ms | 477.9 ms | +25.88% |
| ⚡ | WallTime | test_import_time[ChatPromptTemplate] |
680.4 ms | 551.8 ms | +23.32% |
| ⚡ | WallTime | test_import_time[BaseChatModel] |
580.6 ms | 479.6 ms | +21.07% |
| ⚡ | WallTime | test_import_time[InMemoryRateLimiter] |
184 ms | 161.4 ms | +14.03% |
| ⚡ | WallTime | test_import_time[Document] |
201 ms | 169.8 ms | +18.33% |
| ⚡ | WallTime | test_import_time[PydanticOutputParser] |
583.2 ms | 486.5 ms | +19.86% |
| ⚡ | WallTime | test_import_time[Runnable] |
533.9 ms | 447.9 ms | +19.21% |
| ⚡ | WallTime | test_import_time[RunnableLambda] |
540.3 ms | 461.7 ms | +17.03% |
Comparing thromel:fix/pydantic-json-schema-error (32a1e52) with master (d6e46bb)
Footnotes
-
23 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. ↩
e5d076b to
9ac9e9c
Compare
When a Pydantic model with `arbitrary_types_allowed=True` contains custom Python classes that cannot be converted to JSON schema, users previously received a cryptic `PydanticInvalidForJsonSchema` error from deep in Pydantic's internals. This change catches `PydanticInvalidForJsonSchema` in `_convert_pydantic_to_openai_function()` and re-raises as a `TypeError` with a helpful message that: - Names the model that failed - Explains that tool argument schemas must be JSON-serializable - Suggests concrete solutions (convert to Pydantic models, use primitive types, or serialize to JSON strings) - Links to the custom tools documentation Fixes langchain-ai#34371
9ac9e9c to
456813c
Compare
# Conflicts: # libs/core/tests/unit_tests/utils/test_function_calling.py
ccurme (ccurme)
approved these changes
Feb 22, 2026
ccurme (ccurme)
approved these changes
Feb 22, 2026
| else: | ||
| msg = "Model must be a Pydantic model." | ||
| raise TypeError(msg) | ||
| except PydanticInvalidForJsonSchema as e: |
Collaborator
There was a problem hiding this comment.
this inherits TypeError
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a Pydantic model with
arbitrary_types_allowed=Truecontains custom Python classes that cannot be converted to JSON schema, users previously received a crypticPydanticInvalidForJsonSchemaerror from deep in Pydantic's internals.This change catches
PydanticInvalidForJsonSchemain_convert_pydantic_to_openai_function()and re-raises as aTypeErrorwith a helpful message that:Example of new error message
Test plan
test_convert_to_openai_function_arbitrary_type_errorthat verifies the improved error message is raisedtest_function_calling.pypassFixes #34371