Skip to content

Fix RecursionError with mutually recursive models in CLI#794

Merged
hramezani merged 1 commit into
mainfrom
fix/cli-mutual-recursion
Feb 22, 2026
Merged

Fix RecursionError with mutually recursive models in CLI#794
hramezani merged 1 commit into
mainfrom
fix/cli-mutual-recursion

Conversation

@hramezani
Copy link
Copy Markdown
Member

Summary

  • Fixes the remaining case from RecursionError with Self-Referential Models in CliApp #781 reported in this comment
  • The previous fix (4a98429) only prevented direct self-recursion (Foo -> Foo) by checking m is not model
  • This did not handle indirect/mutual recursion where models reference each other through discriminated unions (e.g., B has field Union[A, B, C] and C has field Union[A, B, C], creating a cycle B -> C -> B -> ...)
  • Now tracks the full ancestor model_path set through recursion and filters sub-models against all visited ancestors

Test plan

  • Added test_cli_mutually_recursive_models covering the exact scenario from the issue comment (discriminated unions with mutually recursive models)
  • Existing test_cli_self_referential_model still passes (direct self-recursion)
  • Full CLI test suite passes (167 tests)

🤖 Generated with Claude Code

The previous fix for #781 only prevented direct self-recursion
(model referencing itself) by checking `m is not model`. This did
not handle indirect/mutual recursion where models reference each
other through discriminated unions (e.g. B -> C -> B).

Track the full ancestor model path through recursion and filter
sub-models against all visited ancestors, not just the immediate
parent.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@hramezani hramezani force-pushed the fix/cli-mutual-recursion branch from 58de5cf to 8dff905 Compare February 22, 2026 11:07
@hramezani hramezani merged commit 2dde0cb into main Feb 22, 2026
19 checks passed
@hramezani hramezani deleted the fix/cli-mutual-recursion branch February 22, 2026 11:29
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.

1 participant