Skip to content
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

Infinite recursion on model_json_schema() if recusive model is a field in another model #7655

Closed
1 task done
Matthew-Burkard opened this issue Sep 26, 2023 · 3 comments
Closed
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2 duplicate

Comments

@Matthew-Burkard
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Calling model_json_schema() on a model with a recursive field results in a recursion error if the recursive model is used as a type in another model field.

Example error:

Traceback (most recent call last):
  File "/home/matthew/Projects/PyCharm/openrpc/error.py", line 14, in <module>
    RecursiveModel.model_json_schema()
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/main.py", line 385, in model_json_schema
    return model_json_schema(
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 2153, in model_json_schema
    return schema_generator_instance.generate(cls.__pydantic_core_schema__, mode=mode)
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 432, in generate
    json_schema = definitions_remapping.remap_json_schema(json_schema)
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 214, in remap_json_schema
    schema['$defs'] = {
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 215, in <dictcomp>
    self.remap_defs_ref(DefsRef(key)): self.remap_json_schema(value)
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 214, in remap_json_schema
...
... (stack trace repeats for a couple thousand lines here)
...
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 214, in remap_json_schema
    schema['$defs'] = {
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 215, in <dictcomp>
    self.remap_defs_ref(DefsRef(key)): self.remap_json_schema(value)
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 219, in remap_json_schema
    schema[key] = self.remap_json_schema(value)
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 219, in remap_json_schema
    schema[key] = self.remap_json_schema(value)
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 219, in remap_json_schema
    schema[key] = self.remap_json_schema(value)
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 208, in remap_json_schema
    return [self.remap_json_schema(item) for item in schema]
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 208, in <listcomp>
    return [self.remap_json_schema(item) for item in schema]
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 219, in remap_json_schema
    schema[key] = self.remap_json_schema(value)
  File "/home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic/json_schema.py", line 206, in remap_json_schema
    return self.remap_json_ref(JsonRef(schema))
RecursionError: maximum recursion depth exceeded

Example Code

from typing import Optional
from pydantic import BaseModel

class RecursiveModel(BaseModel):
    recursive_field: Optional["RecursiveModel"]

class AnotherModel(BaseModel):
    field: RecursiveModel

RecursiveModel.model_json_schema()

Python, Pydantic & OS Version

pydantic version: 2.4.1
        pydantic-core version: 2.10.1
          pydantic-core build: profile=release pgo=true
                 install path: /home/matthew/.cache/pypoetry/virtualenvs/openrpc-pwkskCd0-py3.10/lib/python3.10/site-packages/pydantic
               python version: 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
                     platform: Linux-6.4.6-76060406-generic-x86_64-with-glibc2.35
             related packages: mypy-1.5.1 typing_extensions-4.8.0
@Matthew-Burkard Matthew-Burkard added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Sep 26, 2023
@sydney-runkle
Copy link
Member

Hi @Matthew-Burkard,

Thanks for reporting this issue. Looks like this is working in v 2.3.0 and v 2.4.0, so the issue likely has to do with our 2.4.1 patch release or the 2.10.1 pydantic-core release.

I'll log this on our known list of issues + we'll address this issue promptly!

@sydney-runkle
Copy link
Member

Alright - a similar issue was reported in #7645, which we have a fix for in #7653. We'll have that across the line shortly 😄

@sydney-runkle sydney-runkle self-assigned this Sep 26, 2023
@sydney-runkle
Copy link
Member

Closing bc this is effectively a duplicate of #7645

@sydney-runkle sydney-runkle added duplicate and removed unconfirmed Bug not yet confirmed as valid/applicable labels Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 duplicate
Projects
None yet
Development

No branches or pull requests

2 participants