Skip to content

fix: catch PydanticUserError when generating output schema (pydantic 2.13 compat)#2434

Merged
maxisbey merged 1 commit intomainfrom
maxisbey/pydantic-213-func-metadata
Apr 13, 2026
Merged

fix: catch PydanticUserError when generating output schema (pydantic 2.13 compat)#2434
maxisbey merged 1 commit intomainfrom
maxisbey/pydantic-213-func-metadata

Conversation

@maxisbey
Copy link
Copy Markdown
Contributor

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.

Motivation and Context

Without this, on pydantic 2.13.0:

class Config:
    name: str
    callback: Callable[[Any], Any] = lambda x: x

@server.tool()
def get_config() -> Config:  # raises PydanticInvalidForJsonSchema at decorator time
    ...

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 both lowest-direct and the locked version.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

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
  • I have added or updated documentation as needed

Additional context

PydanticUserError is the parent of both PydanticInvalidForJsonSchema and PydanticSchemaGenerationError and has existed since pydantic 2.0, so catching it is correct regardless of which built-in exception it subclasses in a given pydantic release. TypeError is kept in the tuple to avoid changing behaviour for any other code path that might raise a plain TypeError here.

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.
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 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

  • PydanticUserError has existed since pydantic 2.0, so no version-compatibility issues.
  • Existing test test_structured_output_unserializable_type_error exercises this exact code path.
  • No bugs found by the automated review system.
  • No prior reviews on this PR.

@maxisbey maxisbey merged commit 5cbd259 into main Apr 13, 2026
31 checks passed
@maxisbey maxisbey deleted the maxisbey/pydantic-213-func-metadata branch April 13, 2026 16:03
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