Skip to content

Stream OpenAPI 3.2 itemSchema responses in the generated client - #6767

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

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

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #6594

Summary by CodeRabbit

  • New Features

    • Generated Python clients now provide synchronous and asynchronous helpers that yield typed items from server-sent events and JSON Lines/NDJSON streams as they arrive.
    • Server-sent event helpers can decode event fields, including event data, type, ID, and retry value.
    • JSON Lines streams may accept a final line without a newline for supported media types; NDJSON requires the final newline.
    • Responses without item-stream helpers continue to be read as complete bodies.
  • Documentation

    • Added guidance on item-stream helper generation, response behavior, and stream-format differences.

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

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The client generator now creates typed SSE and JSON Lines helpers for eligible OpenAPI 3.2 responses with itemSchema. Generated sync and async clients expose these helpers. The runtime decodes SSE fields and streams items, while operation methods continue to return raw stream bodies.

Changes: Item-schema stream support

Layer / File(s) Summary
Plan item-schema stream helpers
src/datamodel_code_generator/_client/plan.py, src/datamodel_code_generator/_client/streams.py, src/datamodel_code_generator/_client/target.py, src/datamodel_code_generator/_client/render.py
The planner separates streamed item types from whole-body types. It generates helpers for eligible response media, respects declared helpers, checks name collisions, and applies media-specific final-line rules.
Decode SSE event fields
src/datamodel_code_generator/_runtime/protocols/streams.py
Event plans can pass parsed SSE fields as JSON codec input, including data and applicable event metadata.
Generate client stream APIs
tests/data/expected/main/generation_platform/client/packages/item-streams*/..., tests/data/expected/main/generation_platform/client/packages/item-streams-declared/...
Generated outputs define typed stream plans, helper APIs, operation methods, and response types for sync and async clients using dataclass and Pydantic backends.
Validate and document item streams
tests/api_generation/scenarios/client_item_streams.py, tests/api_generation/support/client_scenarios.py, tests/api_generation/test_client_generation.py, tests/api_generation/*.refs, docs/python-client-helpers.md, docs/python-client.md
The scenario covers SSE, JSON Lines, NDJSON, invalid items, cut connections, and whole-body reads. Documentation describes helper generation and response behavior.

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

Change: Feature · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant GeneratedHelper
  participant open_events
  participant _dispatched
  participant ModelCodec
  GeneratedHelper->>open_events: Open stream with EventPlan
  open_events->>_dispatched: Dispatch parsed SSE event
  _dispatched->>ModelCodec: Pass serialized event fields
Loading
Merge Risk: Minimal · up to 1f8e3

The change adds typed SSE and JSON Lines helpers for itemSchema responses, and the supplied evidence shows no concrete merge-blocking defect. One narrow edge case, SSE events with empty data in the new field-mode helpers, was not verified.

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 "Stream OpenAPI 3.2 itemSchema responses in the generated client" matches the main change. The PR adds generated client support for OpenAPI 3.2 response media types that use itemSchema. Such…
Linked Issues check Passed Issue #6594 requires that an OpenAPI 3.2 response media type with itemSchema yield a typed stream of item models: SSE events and JSON Lines items. The change set implements this as follows:
- plan.py:…
Out of Scope Changes check Passed All source changes relate to item-schema streaming:
- _client/plan.py, streams.py, render.py, target.py: planning and rendering of item-stream helpers.
- _runtime/protocols/streams.py: SSE field decod…
Full details: Title check

Explanation

The title "Stream OpenAPI 3.2 itemSchema responses in the generated client" matches the main change. The PR adds generated client support for OpenAPI 3.2 response media types that use itemSchema. Such responses now yield typed SSE event or JSON Lines item streams instead of a single decoded value or raw bytes. The linked issue #6594 asks for the same behavior, so the title reflects the primary objective.

Full details: Linked Issues check

Explanation

Issue #6594 requires that an OpenAPI 3.2 response media type with itemSchema yield a typed stream of item models: SSE events and JSON Lines items. The change set implements this as follows:

  • plan.py: Planner.media assigns the schema use to item for STREAMED media (text/event-stream, application/jsonl, application/x-ndjson) when the schema has an item_stream_array projection.
  • streams.py: item_protocols creates an SSE or NDJSON stream helper for each operation's first success response media with itemSchema, unless a declared helper exists. Helper names collide-check against declared helpers.
  • _runtime/protocols/streams.py: EventPlan.fields decodes SSE event fields into the item model.
  • Expected generated output (item-streams fixture, dataclass and Pydantic v2): watch_events returns EventStream[models.ItemSchema] (Event), and tail_logs, stream_records, and get_excerpt return EventStream[models.ItemSchemaModel] (LogEntry).
  • Test scenario client_item_streams.py covers SSE field decoding, JSONL/NDJSON items, invalid items, and connection cuts, for sync and async clients.
    The issue's reported failure (whole body decoded as one Event) is addressed by the item-stream helpers. Default operation methods still return raw str or bytes, as documented. The issue's expected behavior is framed as 'like the existing stream support', which is the protocol helper path. Assessment is based on the whole-PR summary and generated expected outputs. No source inspection was possible in this run.
Full details: Out of Scope Changes check

Explanation

All source changes relate to item-schema streaming:

  • _client/plan.py, streams.py, render.py, target.py: planning and rendering of item-stream helpers.
  • _runtime/protocols/streams.py: SSE field decoding for item schemas.
  • docs/python-client.md and docs/python-client-helpers.md: document the new behavior.
  • tests/api_generation scenario, reference manifests, and expected outputs for item-streams, item-streams-declared, and item-streams-collision: cover the new behavior.
    The generated client package files (lazy getattr exports, AsyncClient and Client views, options and errors re-exports, JSONValue export) appear in the expected output of the new item-streams fixture. They are generated scaffolding for that fixture, not independent changes. Streams.py also now uses shared STREAM_MEDIA and STREAMED definitions in place of local lists. This refactor supports the new planning and is connected to it. No unrelated change was identified. The inspection depth was limited by the available evidence (summary and expected outputs only).
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 4d46314 and 1f8e385.

Files ignored due to path filters: 13

  • docs/llms-full.txt is excluded by none and included by none
  • tests/data/expected/main/generation_platform/client/item-streams-collision.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/generation_platform/client/item-streams-declared.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/generation_platform/client/item-streams.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/_generated/__init__.py is excluded by !**/_generated/** and included by tests/**/*.py, tests/data/expected/**/*.py
  • 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/__init__.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
  • tests/data/generation_platform/client/cases.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/generation_platform/client/item-streams.yaml is excluded by !tests/data/**/*.yaml and included by none
  • tests/data/generation_platform/client/protocols/item-streams-collision.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/generation_platform/client/protocols/item-streams.json is excluded by !tests/data/**/*.json and included by none
Files selected for processing: 51

  • docs/python-client-helpers.md
  • docs/python-client.md
  • src/datamodel_code_generator/_client/plan.py
  • src/datamodel_code_generator/_client/render.py
  • src/datamodel_code_generator/_client/streams.py
  • src/datamodel_code_generator/_client/target.py
  • src/datamodel_code_generator/_runtime/protocols/streams.py
  • tests/api_generation/scenarios/client_item_streams.py
  • tests/api_generation/support/client_scenarios.py
  • tests/api_generation/test_client_generation.py
  • tests/api_generation/test_client_generation.refs
  • tests/api_generation/test_client_runtime.refs
  • 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/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/_async_client.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/_client.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/_operations.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/errors.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/options.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/protocols/__init__.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/dataclasses_dataclass/client/resources/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/resources/default/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/resources/default/_async.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/resources/default/_sync.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/responses.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/types/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/types/default/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/client/types/default/_operations.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/dataclasses_dataclass/models.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/_async_client.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/_client.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/_operations.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/errors.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/options.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/protocols/__init__.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
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/resources/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/resources/default/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/resources/default/_async.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/resources/default/_sync.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/responses.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/types/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/types/default/__init__.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/client/types/default/_operations.py
  • tests/data/expected/main/generation_platform/client/packages/item-streams/pydantic_v2_BaseModel/models.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-12T10:25:41.740268Z 1f8e385 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

@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!

@codspeed

codspeed Bot commented Nov 12, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 10.54%

1 warning

⚠️ 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

❌ 2 regressed benchmarks
✅ 51 untouched benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_perf_target_render[fastapi-small] 24.1 ms 27 ms -10.64%
❌ test_perf_inherited_required_fields[required-base-first] 827.7 ms 924.2 ms -10.44%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix-6594 (1f8e385) with main (4d46314)

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. ↩

@koxudaxi
Koudai Aono (koxudaxi) merged commit 5152497 into main Nov 12, 2026
116 of 117 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-6594 branch November 12, 2026 13:19
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: The behaviour diff gate shows only both-fail cells, so default model output is unchanged. The breaking changes are in Python client generation, which 0.95.0 has; the baseline lists no new features, options or public names. In plan.py, a response media that is SSE or JSON Lines or NDJSON and has an item_stream_array itemSchema projection now gets use None, with the item binding moved to a new item field. The operation method therefore returns raw str or bytes instead of a model-typed result. The docs confirm this: the sentence saying a method returns the decoded body gains an exception, and the expected outputs show text_branch and binary_branch with str and bytes type aliases. A new item_protocols step in target.py adds an SSE or NDJSON helper for each such operation automatically. It also raises APIGenerationError with E_NAME_COLLISION when a declared helper name contains or is contained in a derived helper name, which stops existing helper files with names like default from generating. Moving the media constants from streams.py to plan.py only touches private names, and the new EventPlan fields flag defaults to False, so declared helpers behave as before.

Content for Release Notes

Code Generation Changes

  • Generated client methods return raw bodies for itemSchema stream responses - For an OpenAPI 3.2 response media of text/event-stream or a JSON Lines or NDJSON type (application/jsonl, application/jsonlines, application/ndjson, application/x-jsonl, application/x-jsonlines, application/x-ndjson) whose schema is only an itemSchema, the generated client no longer binds the model of the item schema's array projection to the operation's response. The operation method, its with_response view and its result type alias now read the whole body raw: str for an event stream (a text branch) and bytes for JSON Lines or NDJSON (a binary branch). For example, WatchEventsResponse becomes str, and a JSON plus JSON Lines response becomes models.ExcerptGetResponse | bytes. Code that relied on the old result type must parse the raw body or use the new stream helper that regenerated clients now get at client.protocols.<resource>.<method>, which yields typed items (Stream OpenAPI 3.2 itemSchema responses in the generated client #6767)

Error Handling Changes

  • Helper names that collide with derived item stream helpers are now refused - When an OpenAPI 3.2 document has responses that stream an itemSchema, generation now derives a stream helper named <resource>.<method> for each such operation. A helper in the protocols helper file whose name contains a derived name, or is contained in one (for example a declared helper named default beside the derived default.watch_events), now fails generation with an E_NAME_COLLISION config diagnostic reported under the client protocols option. The same helper file used to generate. To fix it, rename the declared helper (Stream OpenAPI 3.2 itemSchema responses in the generated client #6767)

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.

The client does not stream an OpenAPI 3.2 response whose media type has itemSchema

1 participant