Uh oh!
There was an error while loading. Please reload this page.
fix: catch PydanticUserError when generating output schema (pydantic 2.13 compat) - #2434
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.
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.
Uh oh!
There was an error while loading. Please reload this page.
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