pydantic: KeyError during model_rebuild/recursive schemas

Initial Checks

  • I confirm that I’m using Pydantic V2 installed directly from the main branch, or equivalent

Description

Defining a recursive model via model_rebuild sometimes fails with:

s = {'schema_ref': 'aiopenapi3.me.L8235d4c12_d58c_4316_829f_4f72825f8720_:94308158795872', 'type': 'definition-ref'}
recurse = <bound method _WalkCoreSchema._walk of <pydantic._internal._core_utils._WalkCoreSchema object at 0x7f8d34350a60>>

    def count_refs(s: core_schema.CoreSchema, recurse: Recurse) -> core_schema.CoreSchema:
        if not is_definition_ref_schema(s):
            return recurse(s, count_refs)
        ref = s['schema_ref']
        ref_counts[ref] += 1
        if current_recursion_ref_count[ref] != 0:
            involved_in_recursion[ref] = True
            return s
    
        current_recursion_ref_count[ref] += 1
>       recurse(all_defs[ref], count_refs)
E       KeyError: 'aiopenapi3.me.L8235d4c12_d58c_4316_829f_4f72825f8720_:94308158795872'

So far I can not reproduce easily, but I saw recent changes to _simplify_schema_references so … maybe … this is enough already?

Example Code

No response

Python, Pydantic & OS Version

pydantic version: 2.0a4
        pydantic-core version: 0.30.0 release build profile
                 install path: ~/venv/openapi3/lib/python3.10/site-packages/pydantic
               python version: 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
                     platform: Linux-5.19.0-40-generic-x86_64-with-glibc2.35
     optional deps. installed: ['email-validator', 'typing-extensions']

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (13 by maintainers)

Most upvoted comments

I can confirm that all the example snippets in this issue currently don’t error on main. I’m a bit concerned that since none of them attempt validation, the issue has been pushed off, but at the same time we (and by “we” I mostly mean @adriangb) have made many improvements so 🤞 it might just be fixed.

I definitely want to get this resolved ASAP, but I wasn’t able to with a quick investigation. Hoping to find time to look into this more this week. Either way we’ll definitely release a new release as soon as it is fixed

@chriszs could you share your code?

Do not waste time on this yet, I think this may be unrelated to pydantic itself but misuse of it.