Skip to content

Fix RecursionError with self-referential models in CliApp#783

Merged
hramezani merged 2 commits intomainfrom
fix/cli-self-referential-recursion
Feb 17, 2026
Merged

Fix RecursionError with self-referential models in CliApp#783
hramezani merged 2 commits intomainfrom
fix/cli-self-referential-recursion

Conversation

@hramezani
Copy link
Copy Markdown
Member

Summary

  • Fixes RecursionError with Self-Referential Models in CliApp #781
  • When using CliApp.run() with a model containing a self-referential field (e.g., foo: Optional["Foo"]), the CLI argument parser entered infinite recursion between _add_parser_args and _add_parser_submodels
  • Added a model_path parameter that tracks ancestor models in the current recursion chain. Before expanding a submodel into CLI flags, it checks whether the model is already being processed — if so, it skips the expansion to break the cycle
  • Self-referential fields can still be set via JSON (e.g., --foo '{"foo": {"foo": null}}'), only the infinite CLI flag expansion is prevented

Test plan

  • Added test_cli_self_referential_model covering nested JSON values for self-referential models
  • All 164 CLI tests pass
  • Full test suite passes (361 passed, 56 skipped, 1 pre-existing unrelated failure)

🤖 Generated with Claude Code

@hramezani hramezani force-pushed the fix/cli-self-referential-recursion branch from b386dac to c2c660a Compare February 16, 2026 17:19
Comment thread pydantic_settings/sources/providers/cli.py Outdated
@hramezani hramezani force-pushed the fix/cli-self-referential-recursion branch from 9c62414 to 329c799 Compare February 17, 2026 16:51
@hramezani hramezani merged commit bd0ebe6 into main Feb 17, 2026
19 checks passed
@hramezani hramezani deleted the fix/cli-self-referential-recursion branch February 17, 2026 16:54
@hramezani hramezani mentioned this pull request Feb 19, 2026
2 tasks
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.

RecursionError with Self-Referential Models in CliApp

2 participants