Skip to content

fix(streaming): clear error_code when the aggregated SSE response has content - #8084

Open
r14dd wants to merge 1 commit into
google:mainfrom
r14dd:fix/7973-stream-error-code
Open

r14dd wants to merge 1 commit into
google:mainfrom
r14dd:fix/7973-stream-error-code

Conversation

@r14dd

@r14dd r14dd commented Nov 10, 2026

Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:

StreamingResponseAggregator.close() sets error_code and error_message from finish_reason for every non-STOP finish reason, even when the aggregated response has content. LlmResponse.create (non-streaming) sets them only when there is no content. So the same MAX_TOKENS reply with text is an error under StreamingMode.SSE and not under StreamingMode.NONE.

Solution:

In both close() branches (progressive SSE and the old path), clear error_code and error_message when the aggregated content has parts and there is a candidate. This matches LlmResponse.create, which sets an error only when the candidate has no content. An empty reply with a non-STOP finish reason still returns the error, and finish_reason is kept on the response.

Behaviour change for streaming callers: a caller that reads error_code to detect a non-STOP finish (MAX_TOKENS, SAFETY, RECITATION, ...) on a response that has content must read finish_reason instead, as non-streaming callers already do.

Scope, as agreed on #7973:

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.

  • All unit tests pass locally.

  • New test_close_error_matches_llm_response_create (tests/unittests/utils/test_streaming_utils.py): the same response goes through LlmResponse.create and the aggregator, in both progressive and non-progressive mode, for STOP, MAX_TOKENS, SAFETY, RECITATION and OTHER, with text and with no parts. It compares error_code, error_message and finish_reason.

  • Four existing tests pinned the old SSE behaviour and are updated to the create behaviour (finish_reason kept, error_code and error_message cleared): test_close_with_error, test_generate_content_async_stream_aggregated_content_regardless_of_finish_reason, test_generate_content_async_stream_with_thought_and_text_error_handling, test_generate_content_async_stream_error_info_set_for_non_stop_finish_reason.

pytest tests/unittests
23863 passed, 116 skipped, 12 xfailed, 2 xpassed

With the close() change reverted, the new parity test and test_close_with_error fail (9 failures across the two).

Manual End-to-End (E2E) Tests:

I ran the same MAX_TOKENS-with-text response through InMemoryRunner in both modes (details in this comment on #7973), on main 70e1cbd with the same rule applied in close():

NONE SSE
main error_code=None error_code=MAX_TOKENS
with this change error_code=None error_code=None

An empty MAX_TOKENS reply still comes back as an error in both modes.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Direction picked by the triager on #7973 (streaming change only, full create rule rather than MAX_TOKENS only).

@google-cla

google-cla Bot commented Nov 10, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@r14dd
r14dd force-pushed the fix/7973-stream-error-code branch from 0691eb0 to 8c5157d Compare November 10, 2026 13:15
@r14dd
r14dd marked this pull request as ready for review November 10, 2026 13:23
@r14dd
r14dd force-pushed the fix/7973-stream-error-code branch from 8c5157d to 3510bfd Compare November 25, 2026 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gemini sets error_code for a MAX_TOKENS response with content only when streaming

2 participants