Conversation
Pydantic 2.13.0 changed PydanticUserError's base class from TypeError to RuntimeError (pydantic/pydantic#12579). _try_create_model_and_schema was relying on `except TypeError` to catch PydanticInvalidForJsonSchema when a return type can't be represented in JSON Schema (e.g. a Callable field). On pydantic 2.13.0 the exception escapes and tool registration crashes instead of falling back to unstructured output. Catch PydanticUserError explicitly so the fallback works regardless of which built-in exception it subclasses.
Merged
5 tasks
Contributor
There was a problem hiding this comment.
LGTM — simple, targeted pydantic 2.13 compatibility fix.
Extended reasoning...
Overview
Single-file change to func_metadata.py: adds PydanticUserError to the import and to the exception tuple in _try_create_model_and_schema. No logic changes beyond the broader catch.
Security risks
None. This is a narrow exception-handling fix with no auth, crypto, or data-exposure concerns.
Level of scrutiny
Low. The change is mechanical and well-understood: pydantic 2.13 changed PydanticUserError's base from TypeError to RuntimeError, breaking the implicit catch. Catching the named exception explicitly is the correct and idiomatic fix. TypeError is retained for safety.
Other factors
PydanticUserErrorhas existed since pydantic 2.0, so no version-compatibility issues.- Existing test
test_structured_output_unserializable_type_errorexercises this exact code path. - No bugs found by the automated review system.
- No prior reviews on this PR.
Kludex
approved these changes
Apr 13, 2026
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.
Pydantic 2.13.0 changed
PydanticUserError's base class fromTypeErrortoRuntimeError(pydantic/pydantic#12579)._try_create_model_and_schemawas relying onexcept TypeErrorto catchPydanticInvalidForJsonSchemawhen a return type contains a field that can't be represented in JSON Schema (e.g.Callable). On pydantic 2.13.0 that exception now escapes, so registering such a tool crashes instead of falling back to unstructured output.This adds
PydanticUserErrorto the except tuple so the fallback works on all supported pydantic versions.Motivation and Context
Without this, on pydantic 2.13.0:
Previously this logged a message and continued with
output_schema=None.How Has This Been Tested?
Covered by the existing
test_structured_output_unserializable_type_error, which fails on pydantic 2.13.0 without this change and passes with it. Verified locally on the locked pydantic version; CI will exercise bothlowest-directand the locked version.Breaking Changes
None.
Types of changes
Checklist
Additional context
PydanticUserErroris the parent of bothPydanticInvalidForJsonSchemaandPydanticSchemaGenerationErrorand has existed since pydantic 2.0, so catching it is correct regardless of which built-in exception it subclasses in a given pydantic release.TypeErroris kept in the tuple to avoid changing behaviour for any other code path that might raise a plainTypeErrorhere.AI Disclaimer