Skip to content

[v1.x] fix: catch PydanticUserError when generating output schema (pydantic 2.13 compat)#2435

Merged
maxisbey merged 1 commit intov1.xfrom
maxisbey/v1.x-pydantic-213-func-metadata
Apr 13, 2026
Merged

[v1.x] fix: catch PydanticUserError when generating output schema (pydantic 2.13 compat)#2435
maxisbey merged 1 commit intov1.xfrom
maxisbey/v1.x-pydantic-213-func-metadata

Conversation

@maxisbey
Copy link
Copy Markdown
Contributor

Backport of #2434 to v1.x.

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 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 PydanticUserError to the except tuple so the fallback works on all supported pydantic versions.

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. The highest resolution leg of the v1.x CI matrix exercises pydantic 2.13.0 directly.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling

AI Disclaimer

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.
@maxisbey maxisbey marked this pull request as ready for review April 13, 2026 14:32
@maxisbey maxisbey requested a review from Kludex April 13, 2026 14:33
Copy link
Copy Markdown
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

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

LGTM — simple, targeted compatibility fix.

Extended reasoning...

Overview

This PR modifies a single file () to add to an existing tuple in . It is a backport of #2434 to the v1.x branch.

Security risks

None. This change only widens an exception catch for schema-generation errors, affecting fallback behavior when a return type is unserializable. No auth, crypto, or permission-sensitive code is touched.

Level of scrutiny

Low. The fix is mechanical: pydantic 2.13 changed 's base class from to , so the existing no longer caught it. Adding explicitly is the correct and minimal fix. The comment is updated to explain the version-specific inheritance difference.

Other factors

No bugs were found by the automated review. The change is covered by an existing test () and exercised by the CI matrix. Single-file, single-concern change with no design decisions to weigh.

@maxisbey maxisbey merged commit 8d4c2f5 into v1.x Apr 13, 2026
22 checks passed
@maxisbey maxisbey deleted the maxisbey/v1.x-pydantic-213-func-metadata branch April 13, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants