fix(python): Resolve PydanticUserError for mutually recursive models in Pydantic v2#10519
Merged
fix(python): Resolve PydanticUserError for mutually recursive models in Pydantic v2#10519
Conversation
…th update_forward_refs This change improves handling of circular references in self-referential union members by: 1. Reverting F401 from noqa comments in imports_manager.py (back to E402, I001) 2. Tracking self-referential union member dependencies separately 3. Generating update_forward_refs(Model, GhostRef=GhostRef) instead of simple update_forward_refs(Model) 4. Using non-quoted references by creating new references without must_import_after_current_declaration flag This ensures ghost references are properly imported and referenced in update_forward_refs calls. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
- python-sdk: 4.35.3 -> 4.35.4 - pydantic: 1.9.1 -> 1.9.2 - fastapi: 1.11.1 -> 1.11.2 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Resolved version conflict in sdk/versions.yml by bumping to 4.36.1 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
tjb9dc
commented
Nov 12, 2025
| from pydantic import ValidationError | ||
| from seed import Acai | ||
|
|
||
| def test_recursive_model(): |
aditya-arolkar-swe
approved these changes
Nov 12, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes PydanticUserError for mutually recursive models in Pydantic v2 by properly handling
self-referential union member ghost references.
Changes
- Track them separately in FernAwarePydanticModel
- Generate update_forward_refs(Model, GhostRef=GhostRef) instead of simple
update_forward_refs(Model)
- Create non-quoted references by removing must_import_after_current_declaration flag
Example
Before:
After: