fix: catch PydanticUserError when generating output schema (pydantic 2.13 compat) (#2434)
This commit is contained in:
parent
2dfb51a4d1
commit
5cbd259c3b
1 changed files with 10 additions and 3 deletions
|
|
@ -9,7 +9,7 @@ from typing import Annotated, Any, cast, get_args, get_origin, get_type_hints
|
||||||
import anyio
|
import anyio
|
||||||
import anyio.to_thread
|
import anyio.to_thread
|
||||||
import pydantic_core
|
import pydantic_core
|
||||||
from pydantic import BaseModel, ConfigDict, Field, WithJsonSchema, create_model
|
from pydantic import BaseModel, ConfigDict, Field, PydanticUserError, WithJsonSchema, create_model
|
||||||
from pydantic.fields import FieldInfo
|
from pydantic.fields import FieldInfo
|
||||||
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaWarningKind
|
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaWarningKind
|
||||||
from typing_extensions import is_typeddict
|
from typing_extensions import is_typeddict
|
||||||
|
|
@ -402,9 +402,16 @@ def _try_create_model_and_schema(
|
||||||
# Use StrictJsonSchema to raise exceptions instead of warnings
|
# Use StrictJsonSchema to raise exceptions instead of warnings
|
||||||
try:
|
try:
|
||||||
schema = model.model_json_schema(schema_generator=StrictJsonSchema)
|
schema = model.model_json_schema(schema_generator=StrictJsonSchema)
|
||||||
except (TypeError, ValueError, pydantic_core.SchemaError, pydantic_core.ValidationError) as e:
|
except (
|
||||||
|
PydanticUserError,
|
||||||
|
TypeError,
|
||||||
|
ValueError,
|
||||||
|
pydantic_core.SchemaError,
|
||||||
|
pydantic_core.ValidationError,
|
||||||
|
) as e:
|
||||||
# These are expected errors when a type can't be converted to a Pydantic schema
|
# These are expected errors when a type can't be converted to a Pydantic schema
|
||||||
# TypeError: When Pydantic can't handle the type
|
# PydanticUserError: When Pydantic can't handle the type (e.g. PydanticInvalidForJsonSchema);
|
||||||
|
# subclasses TypeError on pydantic <2.13 and RuntimeError on pydantic >=2.13
|
||||||
# ValueError: When there are issues with the type definition (including our custom warnings)
|
# ValueError: When there are issues with the type definition (including our custom warnings)
|
||||||
# SchemaError: When Pydantic can't build a schema
|
# SchemaError: When Pydantic can't build a schema
|
||||||
# ValidationError: When validation fails
|
# ValidationError: When validation fails
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue