[None][fix] handle empty stream in process_streaming_events#16262
[None][fix] handle empty stream in process_streaming_events#16262lonexreb wants to merge 1 commit into
Conversation
If the generator yielded nothing (empty or aborted stream), final_res was never bound and get_final_response(final_res) raised UnboundLocalError. Initialize final_res = None and return early when no output was produced. Signed-off-by: lonexreb <reach2shubhankar@gmail.com>
📝 WalkthroughWalkthrough
ChangesEmpty stream handling
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
tests/unittest/llmapi/apps/test_responses_empty_stream.py (2)
26-49: 📐 Maintainability & Code Quality | 🔵 TrivialNo QA list update is needed.
This change only adds a narrow unit test under
tests/unittest/; no integration or release schedule is affected.As per path instructions, unittest-only changes do not require an entry under
tests/integration/test_lists/qa/.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unittest/llmapi/apps/test_responses_empty_stream.py` around lines 26 - 49, The comment confirms that no implementation change or QA list update is required. Keep the narrow unit test test_process_streaming_events_handles_empty_stream under tests/unittest/, and do not add an entry under tests/integration/test_lists/qa/.Source: Path instructions
21-28: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAnnotate the async test helpers.
Add return annotations to
_empty_streamandrunto satisfy the Python typing guidelines.Proposed fix
+from collections.abc import AsyncGenerator + -async def _empty_stream(): +async def _empty_stream() -> AsyncGenerator[None, None]: return yield # pragma: no cover - makes this an async generator ... - async def run(): + async def run() -> None:As per coding guidelines, “Always annotate functions.”
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unittest/llmapi/apps/test_responses_empty_stream.py` around lines 21 - 28, Add return type annotations to the async helper functions _empty_stream and the nested run inside test_process_streaming_events_handles_empty_stream, using the appropriate async-generator and coroutine return types consistent with their implementations.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/unittest/llmapi/apps/test_responses_empty_stream.py`:
- Around line 26-49: The comment confirms that no implementation change or QA
list update is required. Keep the narrow unit test
test_process_streaming_events_handles_empty_stream under tests/unittest/, and do
not add an entry under tests/integration/test_lists/qa/.
- Around line 21-28: Add return type annotations to the async helper functions
_empty_stream and the nested run inside
test_process_streaming_events_handles_empty_stream, using the appropriate
async-generator and coroutine return types consistent with their
implementations.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 643f0d76-2cb7-487f-b0bd-990420148a9d
📒 Files selected for processing (2)
tensorrt_llm/serve/responses_utils.pytests/unittest/llmapi/apps/test_responses_empty_stream.py
Description
process_streaming_events(tensorrt_llm/serve/responses_utils.py, the/v1/responsesstreaming path) bindsfinal_resonly inside the stream loop:If the generator yields nothing (empty or aborted stream),
final_resis never bound andget_final_response(final_res)raisesUnboundLocalError, escaping the async generator.Change
Initialize
final_res = Nonebefore the loop and return early when no output was produced (no final response to build).Test
New CPU-only unit test in
tests/unittest/llmapi/apps/test_responses_empty_stream.py: drivingprocess_streaming_eventswith an empty async stream (processor mocked) completes without raising and does not callget_final_response.Summary by CodeRabbit
Bug Fixes
Tests