Repository navigation
fix(google-genai): report a streamed response as one output message per candidate - #908
Open
pcbeingused333 wants to merge 3 commits into
Open
pcbeingused333 wants to merge 3 commits into
pcbeingused333 wants to merge 3 commits into
Conversation
…er candidate The streaming wrappers collected every chunk's candidates and turned each into its own output message, so a response streamed in N chunks was recorded as N assistant messages holding fragments of the text. Merge the streamed candidates by index before building the output messages: adjacent text fragments of the same kind (thought or not) are joined, other parts are kept in order, and the last finish reason wins, so a streamed response is reported like the non-streamed one.
Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
Pull request dashboard statusWaiting on reviewers · refreshed 2026-10-17 03:28 UTC Review the latest changes. Status above doesn't look right?
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Contentless candidates now produce artificial empty output messages instead of being omitted.
1 open issue
What changed in this PR
Fixes Google GenAI streaming telemetry to report one output message per candidate rather than per chunk.
Changes:
- Merges candidates by index, joining adjacent text while preserving part order and the latest finish reason.
- Adds merge tests and shared sync/async streaming coverage.
- Records the fix in the changelog.
| File | Description |
|---|---|
| instrumentation/opentelemetry-instrumentation-google-genai/tests/generate_content/test_merge_streamed_candidates.py | Tests merging and chunk immutability. |
| instrumentation/opentelemetry-instrumentation-google-genai/tests/generate_content/streaming_base.py | Adds sync/async output-message regression coverage. |
| instrumentation/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/generate_content.py | Merges candidates before generating streamed output messages. |
| instrumentation/opentelemetry-instrumentation-google-genai/.changelog/908.fixed | Documents the streaming correction. |
Review effort: Balanced
Comment on lines
+603
to
+611
| target = merged[index] = Candidate( | ||
| index=candidate.index, content=Content(parts=[]) | ||
| ) | ||
| if candidate.finish_reason is not None: | ||
| target.finish_reason = candidate.finish_reason | ||
| if not candidate.content: | ||
| continue | ||
| if candidate.content.role: | ||
| target.content.role = candidate.content.role |
…tput messages The merge created an empty Content for every candidate index, so a stream whose candidate never carried content (for example a blocked response) was reported as an output message with no parts. Non-streamed responses skip such candidates. Create the merged content only once a chunk supplies some.
Author
|
/easycla |
pcbeingused333
force-pushed
the
fix/google-genai-stream-merge-output
branch
from
October 17, 2026 03:25
dda3275 to
e470616
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
generate_content_stream(sync and async) recorded each streamed chunk's candidate as its own output message, so a response streamed in N chunks showed up as N assistant messages holding fragments. The stream wrappers now merge candidates by index before building output messages: adjacent text fragments of the same kind (thought or not) are joined, other parts keep their order, and the last finish reason wins, matching the non-streamed output.Type of change
How has this been tested?
test_merges_streamed_chunks_into_one_output_messageinstreaming_base.py(runs for the sync and async streaming tests); fails on main with two messages.test_merge_streamed_candidates.py: merging per candidate index, thought text kept apart, function calls kept, last finish reason, and no mutation of the streamed chunks.tests/(without conformance): same 8 e2e errors as main (need credentials), 4 more passing.Checklist
This change was made with Claude Code; the tests above were run on main and on this branch.