New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(2.4.0/2.4.1) LookupError during schema gen (simple repro) #7645
Comments
I've got a slightly related issue with recursive type in geojson-pydantic. Happy to open a new issue if it's not related at all from pydantic import BaseModel
from typing_extensions import Annotated
from typing import List, Literal, Union, Tuple
from pydantic import Field
class Point(BaseModel):
type: Literal["Point"]
coordinates: Union[Tuple[float, float], Tuple[float, float, float]]
class GeometryCollection(BaseModel):
type: Literal["GeometryCollection"]
geometries: List["Geometry"]
Geometry = Annotated[
Union[
Point,
GeometryCollection,
],
Field(discriminator="type"),
]
GeometryCollection.model_rebuild()
---------------------------------------------------------------------------
SchemaError Traceback (most recent call last)
<ipython-input-2-2c593a18e1c0> in <cell line: 25>()
23 ]
24
---> 25 GeometryCollection.model_rebuild()
26
~/Dev/venv/py39/lib/python3.9/site-packages/pydantic/main.py in model_rebuild(cls, force, raise_errors, _parent_namespace_depth, _types_namespace)
468 # manually override defer_build so complete_model_class doesn't skip building the model again
469 config = {**cls.model_config, 'defer_build': False}
--> 470 return _model_construction.complete_model_class(
471 cls,
472 cls.__name__,
~/Dev/venv/py39/lib/python3.9/site-packages/pydantic/_internal/_model_construction.py in complete_model_class(cls, cls_name, config_wrapper, raise_errors, types_namespace)
496
497 # debug(schema)
--> 498 cls.__pydantic_core_schema__ = schema = validate_core_schema(schema)
499 cls.__pydantic_validator__ = create_schema_validator(schema, core_config, config_wrapper.plugin_settings)
500 cls.__pydantic_serializer__ = SchemaSerializer(schema, core_config)
~/Dev/venv/py39/lib/python3.9/site-packages/pydantic/_internal/_core_utils.py in validate_core_schema(schema)
629 if 'PYDANTIC_SKIP_VALIDATING_CORE_SCHEMAS' in os.environ:
630 return schema
--> 631 return _validate_core_schema(schema)
SchemaError: Invalid Schema:
definitions.definitions.1.model.schema.model-fields.fields.geometries.schema.list.items_schema.tagged-union.choices.GeometryCollection
Recursion error - cyclic reference detected [type=recursion_loop, input_value={'type': 'model', 'cls': ...yCollection:4353815568'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.4/v/recursion_loop |
seeing it here too, and a local git blame puts the change at #7624 and a slight variant on the original reproducible example gives a from typing import Optional
from pydantic import BaseModel
class Foo(BaseModel):
bar: "Bar"
class Bar(BaseModel):
foo: Optional[Foo]
Foo.model_rebuild()
print(Bar.model_json_schema()) File "/Users/talley/mambaforge/envs/useq-schema/lib/python3.11/site-packages/pydantic/json_schema.py", line 215, in <dictcomp>
self.remap_defs_ref(DefsRef(key)): self.remap_json_schema(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/talley/mambaforge/envs/useq-schema/lib/python3.11/site-packages/pydantic/json_schema.py", line 219, in remap_json_schema
schema[key] = self.remap_json_schema(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/talley/mambaforge/envs/useq-schema/lib/python3.11/site-packages/pydantic/json_schema.py", line 219, in remap_json_schema
schema[key] = self.remap_json_schema(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/talley/mambaforge/envs/useq-schema/lib/python3.11/site-packages/pydantic/json_schema.py", line 219, in remap_json_schema
schema[key] = self.remap_json_schema(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/talley/mambaforge/envs/useq-schema/lib/python3.11/site-packages/pydantic/json_schema.py", line 208, in remap_json_schema
return [self.remap_json_schema(item) for item in schema]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/talley/mambaforge/envs/useq-schema/lib/python3.11/site-packages/pydantic/json_schema.py", line 208, in <listcomp>
return [self.remap_json_schema(item) for item in schema]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/talley/mambaforge/envs/useq-schema/lib/python3.11/site-packages/pydantic/json_schema.py", line 219, in remap_json_schema
schema[key] = self.remap_json_schema(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/talley/mambaforge/envs/useq-schema/lib/python3.11/site-packages/pydantic/json_schema.py", line 206, in remap_json_schema
return self.remap_json_ref(JsonRef(schema))
^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded while calling a Python object
|
I just want to thank everyone for being so quick to report these schema building issues introduced in 2.4 with clean reproductions of the issue. Every report is helping us improve our test suite, and we are working urgently to address these problems. |
Initial Checks
Description
Was hoping the 2.4.1 patch would fix, but there's a new schema error on these two new versions. (same code works 2.3.0)
Edit: managed to make a simple reproduction.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: