Skip to content

Comments

fix(python): Resolve PydanticUserError for mutually recursive models in Pydantic v2#10519

Merged
tjb9dc merged 6 commits intomainfrom
tb/special-ghost-refs-are-handled
Nov 12, 2025
Merged

fix(python): Resolve PydanticUserError for mutually recursive models in Pydantic v2#10519
tjb9dc merged 6 commits intomainfrom
tb/special-ghost-refs-are-handled

Conversation

@tjb9dc
Copy link
Contributor

@tjb9dc tjb9dc commented Nov 12, 2025

Summary

Fixes PydanticUserError for mutually recursive models in Pydantic v2 by properly handling
self-referential union member ghost references.

Changes

  1. Reverted imports_manager.py: Removed F401 from noqa comments (back to # noqa: E402, I001)
  2. Added special handling for self-referential union member dependencies:
    - 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
  3. Extended to single property union members in discriminated unions

Example

Before:

  update_forward_refs(ObjectJsonSchemaPropertyInput,
  ArrayJsonSchemaPropertyInput="ArrayJsonSchemaPropertyInput")

After:

  from .array_json_schema_property_input import ArrayJsonSchemaPropertyInput  # noqa: E402, I001

  update_forward_refs(ObjectJsonSchemaPropertyInput,
  ArrayJsonSchemaPropertyInput=ArrayJsonSchemaPropertyInput)

tjb9dc and others added 4 commits November 12, 2025 14:01
…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>
from pydantic import ValidationError
from seed import Acai

def test_recursive_model():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay a test

@tjb9dc tjb9dc merged commit 7e3bbd5 into main Nov 12, 2025
53 checks passed
@tjb9dc tjb9dc deleted the tb/special-ghost-refs-are-handled branch November 12, 2025 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants