Skip to content

fix: await typed LLM stream source cleanup - #1607

Merged
willkill07 merged 1 commit into
NVIDIA:release/0.11from
ericevans-nv:fix/typed-stream-source-cleanup
Nov 3, 2026
Merged

willkill07 merged 1 commit into
NVIDIA:release/0.11from
ericevans-nv:fix/typed-stream-source-cleanup

Conversation

@ericevans-nv

@ericevans-nv ericevans-nv commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Closing a stream returned by nemo_relay.typed.llm_stream_execute() could finish without closing its source iterator, leaving source-owned resources open. This change awaits the source iterator’s optional aclose() before forwarding finishes.

  • I confirm this contribution is my own work, or I have the right to submit it under this project's license.
  • I searched existing issues and open pull requests, and this does not duplicate existing work.

Details

The typed wrapper previously forwarded chunks without a cleanup boundary. It now retains the source iterator and awaits its optional aclose() in a finally block, including early closure, cancellation, and chunk-encoding failures. Cleanup errors propagate, and sources without aclose() remain supported.

Adds a regression through the native SDK and unit coverage for normal exhaustion, awaited early cleanup, repeated closure, cancellation, and iteration, encoding, and cleanup failures. The public docstring explains that callers stopping early should await stream.aclose().

Public signatures and returned JSON chunk behavior are unchanged.

Validation passed: 12 focused stream tests, the canonical just test-python suite (81 native, 1,319 Python, and 24 example tests), just docs, and all applicable commit hooks. Fern’s published-redirect comparison was skipped because its service returned HTTP 403.

Where should the reviewer start?

Start with the typed stream forwarding function, then the native-stream regression and lifecycle unit tests.

Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)

Summary by CodeRabbit

  • Bug Fixes
    • Streaming responses now close their underlying source when iteration completes or is stopped early, including when conversion fails or iteration is cancelled.
  • Documentation
    • Callers are advised to close streams when stopping early to help ensure source cleanup happens promptly.
  • Tests
    • Added coverage for cleanup timing and call counts after normal completion, early and repeated closure, iteration or conversion failures, cleanup errors, and cancellation.

@ericevans-nv
ericevans-nv requested a review from a team as a code owner November 2, 2026 21:12
@github-actions github-actions Bot added size:S PR is small Bug issue describes bug; PR fixes bug lang:python PR changes/introduces Python code labels Nov 2, 2026
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

The typed JSON stream adapter now awaits optional source-iterator cleanup when forwarding ends, including on early closure and conversion failures. Tests cover normal exhaustion, repeated closure, iteration and cleanup errors, encoding failures, and cancellation.

Changes

Typed stream cleanup

Layer / File(s) Summary
Await source iterator cleanup
python/nemo_relay/typed.py
The JSON adapter retains the source iterator and awaits its optional aclose() method in a finally block. The llm_stream_execute documentation describes source cleanup when forwarding ends.
Test stream lifecycle behavior
python/tests/test_typed.py
Added async test sources and coverage for native and typed stream cleanup, normal exhaustion, early closure, iteration and encoding failures, cleanup failures, and cancellation.

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

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to f9542

Typed streams await source cleanup when closed. Callers stopping early must close the stream explicitly. No merge-blocking risk remains.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title uses the Conventional Commits format with the allowed type 'fix', is 42 characters long, has no trailing period, and describes the main change: awaiting cleanup of the typed LLM stream sourc…
Description check Passed The description follows the repository template. It includes the Overview with both confirmation checkboxes marked, the Details section explaining the cleanup change, the Where should the reviewer sta…
Linked Issues check Passed Issue #1606 is an active direct link. The supplied PR evidence shows that python/nemo_relay/typed.py retains the source iterator and awaits its optional aclose() in finally. This covers normal e…
Out of Scope Changes check Passed The supplied whole-PR summary identifies changes only in python/nemo_relay/typed.py and python/tests/test_typed.py. The implementation and tests directly support issue #1606. No unrelated product …
Full details: Title check

Explanation

The title uses the Conventional Commits format with the allowed type 'fix', is 42 characters long, has no trailing period, and describes the main change: awaiting cleanup of the typed LLM stream source iterator. The title is related to the changeset.

Full details: Description check

Explanation

The description follows the repository template. It includes the Overview with both confirmation checkboxes marked, the Details section explaining the cleanup change, the Where should the reviewer start section with file links, and the Related Issues section that closes #1606. The description is complete and specific.

Full details: Linked Issues check

Explanation

Issue #1606 is an active direct link. The supplied PR evidence shows that python/nemo_relay/typed.py retains the source iterator and awaits its optional aclose() in finally. This covers normal exhaustion, early closure, cancellation, and chunk-encoding failure. Cleanup errors propagate, sources without aclose() remain supported, public signatures and JSON chunk behavior remain unchanged, and python/tests/test_typed.py adds lifecycle regression coverage.

Full details: Out of Scope Changes check

Explanation

The supplied whole-PR summary identifies changes only in python/nemo_relay/typed.py and python/tests/test_typed.py. The implementation and tests directly support issue #1606. No unrelated product behavior or files are evidenced.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 5 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ast-grep (0.51.1)

ast-grep could not parse rule config: /ast-grep-rules/ast-grep-essentials/rules/python/security/ssl-unverified-context.yml

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between b243ec4 and f95420a.

Files selected for processing: 2
  • python/nemo_relay/typed.py
  • python/tests/test_typed.py

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

Recent review details
Context from checks skipped due to timeout: 3
  • GitHub Check: CodeRabbit
  • GitHub Check: Preview docs
  • GitHub Check: Changes / Detect
Additional context used
Code guidelines: 1
CLAUDE.md — configured
Path-based instructions: 3
Tests should cover the behavior promised by the changed API surface, including error paths and cross-request isolation where relevant.

CodeRabbit configuration file

Files:

  • python/tests/test_typed.py
Review Python wrapper changes for typed API consistency, contextvars-based scope isolation, async behavior, and parity with the native extension.

CodeRabbit configuration file

Files:

  • python/nemo_relay/typed.py
Source excerpt: A public or observable runtime change requires identifying which bindings expose the affected contract.

CodeRabbit inference engine (CLAUDE.md)

Files:

  • python/nemo_relay/typed.py
Additional comments: 2
python/nemo_relay/typed.py: 1

798-800: LGTM!

Also applies to: 849-856

python/tests/test_typed.py: 1

85-135: LGTM!

Also applies to: 949-1043

willkill07
willkill07 previously approved these changes Nov 2, 2026
@willkill07 willkill07 added this to the 0.11 milestone Nov 2, 2026
@willkill07

Copy link
Copy Markdown
Member

@ericevans-nv this should also target release/0.11

@coderabbitai coderabbitai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • Fix comments: Fix CodeRabbit comments on this PR

Comment thread python/tests/test_typed.py Resolved
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

@ericevans-nv
ericevans-nv force-pushed the fix/typed-stream-source-cleanup branch from def9682 to b243ec4 Compare November 2, 2026 21:43
@ericevans-nv
ericevans-nv changed the base branch from main to release/0.11 November 2, 2026 21:44
@ericevans-nv
ericevans-nv dismissed willkill07’s stale review November 2, 2026 21:44

The base branch was changed.

@ericevans-nv
ericevans-nv enabled auto-merge (squash) November 2, 2026 21:46

@coderabbitai coderabbitai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • Fix comments: Fix CodeRabbit comments on this PR

Comment thread python/tests/test_typed.py Resolved
@willkill07
willkill07 merged commit b9665a9 into NVIDIA:release/0.11 Nov 3, 2026
46 checks passed

This branch was previously deployed

1 inactive deployment
fern — f95420ae Deployed Nov 3, 2026 by willkill07 via Clean up docs preview #6915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug issue describes bug; PR fixes bug lang:python PR changes/introduces Python code size:S PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Typed LLM stream closure does not await source iterator cleanup

2 participants