Skip to content

Bind the model a $ref names, not a root model another use made there - #6771

Merged
Koudai Aono (koxudaxi) merged 2 commits into
mainfrom
fix-6770
Nov 12, 2026
Merged

Koudai Aono (koxudaxi) merged 2 commits into
mainfrom
fix-6770

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Fixes: #6770

Summary by CodeRabbit

  • Bug Fixes
    • Generated streaming clients now use the schema-specific event models for event and NDJSON streams, improving the accuracy of stream item types across synchronous and asynchronous helpers.

@koxudaxi
Koudai Aono (koxudaxi) marked this pull request as ready for review November 12, 2026 14:11
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The binding lookup now uses recorded declaration types for value projections and follows references when resolving properties. Generated stream plans and helper annotations now use the referenced Event or LogEntry models instead of item-schema wrapper types.

Changes: Stream item binding

Layer / File(s) Summary
Resolve recorded declaration types
src/datamodel_code_generator/_target_binding.py
The binding lookup no longer depends on direction. Value projections use a recorded declaration DataType when available, and property resolution follows its reference.
Update generated stream types
tests/data/expected/main/generation_platform/client/packages/item-streams*/.../client/protocols/*
Expected stream plans and synchronous and asynchronous helper annotations use Event or LogEntry instead of item-schema wrapper types across the listed client configurations.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • Issue 6594: The changes cover the same item-stream and $ref binding path, but the PR does not claim to fix that issue’s streaming failure.
Merge Risk: Minimal · previous review 065a6

This change makes generated stream plans and helpers use the model a $ref names, such as Event or LogEntry, instead of a root wrapper. Reviewed evidence shows the generated codecs and annotations agree, and no concrete merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the core fix: a $ref-only itemSchema should bind the model the $ref names, not a root model that another use created at that location. The changeset alters _target_binding.py so the de…
Linked Issues check Passed [#6770] The issue requires that a $ref-only itemSchema produce the referenced model as the stream item type (for example EventStream[models.Event]) for every output model type, including pydantic_v2.B…
Out of Scope Changes check Passed All reported changes relate to the #6770 behavior. The source change in _target_binding.py alters how a declaration's recorded DataType is selected for $ref-based item types. The remaining changes are…
Full details: Title check

Explanation

The title names the core fix: a $ref-only itemSchema should bind the model the $ref names, not a root model that another use created at that location. The changeset alters _target_binding.py so the declared DataType is selected before resolving the emitted model reference, and the expected outputs switch stream item types from ItemSchema/ItemSchemaModel wrappers to Event and LogEntry. The title is specific and matches the linked issue, though its wording is somewhat abstract for a reader scanning history.

Full details: Linked Issues check

Explanation

[#6770] The issue requires that a $ref-only itemSchema produce the referenced model as the stream item type (for example EventStream[models.Event]) for every output model type, including pydantic_v2.BaseModel. The summary reports a source change in src/datamodel_code_generator/_target_binding.py. _SchemaUses.projection now selects the declaration's recorded DataType before it resolves an emitted model reference. bound_node returns that recorded type for "value" projections. TargetApiOpenAPIParser.property_type follows a DataType reference before it looks up the property field. The reviewable expected outputs match the requirement. In item-streams/pydantic_v2_BaseModel, _helpers.py and _async_helpers.py now return EventStream[models.Event] for /events. The same files return EventStream[models.LogEntry] for the NDJSON endpoints. _plans.py now uses models.Event for STREAM_0. The dataclasses_dataclass outputs show the same types, so the two output model types now agree. Uncertainty: the generated model_bindings.py files for both output model types match the exclusion rule !/_generated/ and were not inspected. The conclusion relies on the source change and the reviewable expected outputs. Both were read from the summary, not from the full diff.

Full details: Out of Scope Changes check

Explanation

All reported changes relate to the #6770 behavior. The source change in _target_binding.py alters how a declaration's recorded DataType is selected for $ref-based item types. The remaining changes are regenerated expected outputs under tests/data/expected/main/generation_platform/client/packages/item-streams and item-streams-declared. These outputs show the new item types for /events, /logs, /records, and /excerpt. The item-streams-declared changes update STREAM_1 to STREAM_3 to models.LogEntry, which matches the same binding behavior. The bound_node change removes the direction filter, so it applies to all callers. That broader effect is still a mechanism of this fix and is not a demonstrated unrelated change. Ignored files were not inspected. The model_bindings.py and runtime/item-streams.txt files are excluded and do not change this conclusion.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 5152497 and 065a694.

Files ignored due to path filters: 3

  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/_generated/model_bindings.py is excluded by !**/_generated/** and included by tests/**/*.py, tests/data/expected/**/*.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/_generated/model_bindings.py is excluded by !**/_generated/** and included by tests/**/*.py, tests/data/expected/**/*.py
  • tests/data/expected/main/generation_platform/client/runtime/item-streams.txt is excluded by !tests/data/**/*.txt and included by none
Files selected for processing: 8

  • src/datamodel_code_generator/_target_binding.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams-declared/pydantic_v2_BaseModel/client/protocols/_plans.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/protocols/_async_helpers.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/protocols/_helpers.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/protocols/_plans.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/protocols/_async_helpers.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/protocols/_helpers.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/protocols/_plans.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T14:14:16.759389Z 065a694 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 40.43%

2 warnings

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚠️ 53 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 53 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_unique_items_nested_model_validation 2.5 ms 1.5 ms +59.49%
⚡ test_perf_pydantic_field_name_resolution 2 ms 1.2 ms +58.49%
⚡ test_perf_simple_pydantic_v2_field_construction 8.9 ms 5.8 ms +52.43%
⚡ test_perf_unique_items_runtime_scalar_validation 2.3 ms 1.5 ms +52.38%
⚡ test_perf_graphql_style_pydantic_v2 646.6 ms 426.2 ms +51.72%
⚡ test_perf_mapped_reference_users 653.9 ms 436.9 ms +49.68%
⚡ test_perf_large_models_pydantic_v2 2.7 s 1.8 s +49.1%
⚡ test_perf_openapi_large 2.7 s 1.8 s +48.16%
⚡ test_perf_stripe_style_pydantic_v2 1.9 s 1.3 s +48.01%
⚡ test_perf_empty_pydantic_v2_field_render_plans 28.9 ms 19.6 ms +47.58%
⚡ test_perf_ordinary_array_constraint_generation 1.7 s 1.1 s +46.21%
⚡ test_perf_duplicate_names 799.6 ms 548.9 ms +45.68%
⚡ test_perf_aws_style_openapi_pydantic_v2 1.8 s 1.2 s +44.89%
⚡ test_perf_false_reference_validation 290.2 ms 201.5 ms +44.02%
⚡ test_perf_large_models_pydantic_v2_builtin 1,115.4 ms 778.7 ms +43.24%
⚡ test_perf_kubernetes_style_pydantic_v2 2 s 1.4 s +42.77%
⚡ test_perf_inherited_required_fields[required-derived-first] 636.2 ms 445.9 ms +42.7%
⚡ test_perf_complex_refs 1.6 s 1.2 s +42.29%
⚡ test_perf_extreme_large_schema 11.4 s 8 s +41.86%
⚡ test_perf_unique_items_schema_validators 1.6 s 1.1 s +41.39%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix-6770 (32acf96) with main (5152497)

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@koxudaxi
Koudai Aono (koxudaxi) merged commit f16595d into main Nov 12, 2026
116 of 117 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-6770 branch November 12, 2026 15:17
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: The source change in the private _target_binding module makes bound_node prefer the declaration's own type for the value projection over a model another reference emitted at the same location. The bound_node and bound signature changes are internal. The expected outputs for the item-streams client packages show the visible effect: stream helper return types, EventPlan generics and codec type parameters change from ItemSchema or ItemSchemaModel to the referenced models Event and LogEntry. For Pydantic v2 the codecs now construct the referenced model, so the runtime class of decoded items and the model named in validation errors change too. The release baseline lists no experimental features, options, presets or public names missing from 0.95.0, so this client generation behaviour is treated as released. The behaviour diff gate shows only both-fail cells, but it covers default model generation, not client generation. The documentation edits only describe the new behaviour. Overall this is a fix, but users who referenced the old item model names in their code may need to update it, so it is reported as a code generation breaking change.

Content for Release Notes

Code Generation Changes

  • Generated client stream helpers now type items as the model the itemSchema references - In generated Python clients, SSE, JSON Lines and NDJSON stream helpers whose itemSchema is a bare $ref now use the referenced model, for example EventStream[models.LogEntry], AsyncEventStream[models.Event] and EventPlan[models.LogEntry]. Before, they used the duplicate ItemSchema or ItemSchemaModel model generated at the item schema location. The codecs in _generated/model_bindings.py change to match. With Pydantic v2 output, decoded stream items are now instances of the referenced model, for example models.LogEntry instead of models.ItemSchemaModel, and validation error messages name that model. With dataclasses output, only the type annotations change. Code that annotates, isinstance-checks or pattern-matches stream items against models.ItemSchema or models.ItemSchemaModel has to switch to the referenced model (Bind the model a $ref names, not a root model another use made there #6771)

This analysis was produced by the release draft workflow

@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.96.0

This PR is now available in the latest release. See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stream items of a $ref-only itemSchema are root models with Pydantic v2

1 participant