Skip to content

Conversation

@tjb9dc
Copy link
Collaborator

@tjb9dc tjb9dc commented Dec 18, 2025

Description

Refs customer feedback about performance bottlenecks in discriminated union factory and visit methods.

Link to Devin run: https://app.devin.ai/sessions/ac18896281bc4c41a46e21d637ce328f
Requested by: [email protected] (@tjb9dc)

Changes Made

This PR optimizes the factory and visit methods for discriminated unions to avoid expensive .dict() serialization/deserialization. Previously, these methods would:

  1. Call .dict(exclude_unset=True) on the source model (deep serialization)
  2. Spread the dict into a new model constructor (re-validation)

The new implementation uses shallow field copying via copy_and_update_model, which:

  • Directly copies field values from source.__dict__
  • Preserves fields_set / __pydantic_fields_set__ for exclude_unset semantics
  • Handles extra fields (fields not in schema)
  • Uses construct/model_construct to bypass validation (matching original behavior)

Files changed:

  • Added copy_and_update_model utility to all 4 pydantic utility variants
  • Updated discriminated_union_with_utils_generator.py to emit optimized code
  • Added export and getter in core_utilities.py
  • Updated seed test outputs for unions fixture

Updates since last revision

  • Fixed mypy no-any-return errors by wrapping return values with typing.cast(Model, ...) in all 4 pydantic utility variants

Human Review Checklist

  • Verify copy_and_update_model correctly handles fields_set tracking (lines 288-323 in pydantic_utilities.py)
  • Verify Pydantic v1 vs v2 code paths are correct (the v1_on_v2 variants use pydantic.v1.BaseModel)
  • Check the generator changes in discriminated_union_with_utils_generator.py - especially the make_visitor_argument_for_same_properties function and write_copy_and_update closure
  • Review generated code in seed tests (e.g., union_with_base_properties.py) to confirm the optimization is applied correctly
  • Note: The diff includes unrelated _build_url test additions in test_http_client.py - these appear to be from a merge/rebase and may need to be reviewed separately

Testing

  • Seed tests pass for unions fixture (all 3 configurations: no-custom-config, union-utils, union-naming-v1)
  • Lint checks pass (pnpm run check)
  • Mypy type checking passes
  • May want to run additional seed tests for other fixtures with discriminated unions

…() serialization

- Add copy_and_update_model utility function to all pydantic utility variants
- Update discriminated union generator to use shallow field copying instead of .dict()
- Significantly reduces runtime overhead and memory usage for deep, tree-like API shapes
- Preserves exclude_unset semantics, field aliases, and extras handling
- Supports both Pydantic v1 and v2

Co-Authored-By: [email protected] <[email protected]>
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@github-actions
Copy link
Contributor

This PR is stale because it has been open 25 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale This PR hasn't has any commits or comments in 25 days or more. label Jan 13, 2026
@github-actions
Copy link
Contributor

This PR was closed because it has been inactive for 5 days after being marked stale.

@github-actions github-actions bot closed this Jan 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Stale This PR hasn't has any commits or comments in 25 days or more.

Development

Successfully merging this pull request may close these issues.

2 participants