Skip to content

fix(llminternal): drop nil parts in the streaming aggregator - #2190

Merged
wolo-lab merged 3 commits into
google:mainfrom
PratikDhanave:fix/stream-aggregator-nil-part
Nov 9, 2026
Merged

wolo-lab merged 3 commits into
google:mainfrom
PratikDhanave:fix/stream-aggregator-nil-part

Conversation

@PratikDhanave

Copy link
Copy Markdown
Contributor

Problem

The streaming aggregator's empty-part filter dereferences each part before any nil test:

for _, part := range llmResponse.Content.Parts {
    // gemini 3 in streaming returns a last response with an empty part. We will filter it out.
    if reflect.ValueOf(*part).IsZero() {   // panics when part == nil

so a streamed chunk whose candidate carries a nil part panics the aggregation loop. A nil part is exactly the kind of junk chunk this filter exists to drop, and TestProcessResponseNilCandidate directly above already treats degenerate chunks as reachable ("per converters.go").

Change

if part == nil || reflect.ValueOf(*part).IsZero() { continue }, plus TestProcessResponseNilPart — the part-level sibling of the nil-candidate regression — which also asserts the following real text part is still aggregated. The test panics before the fix (confirmed) and passes after.

Verification

  • go test ./internal/llminternal/ -race -count=1 — full package passes.
  • golangci-lint run ./internal/llminternal/... — 0 issues.

Companion to #2189 (same nil-Part class in session.IsFinalResponse), kept separate since they are different subsystems.

The aggregator's empty-part filter dereferences each part for its
reflect.IsZero check before any nil test, so a streamed chunk whose
candidate carries a nil part panics the aggregation loop. A nil part is
the same kind of junk chunk the filter exists to drop (the comment
cites gemini 3's trailing empty part), and the nil-candidate regression
test above it already treats degenerate chunks as reachable.

Skip nil parts before dereferencing, and pin it with a part-level
sibling of TestProcessResponseNilCandidate that also asserts the
following real text part is kept; it panics before the fix.
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Thanks for the PR, @PratikDhanave.

One thing before review. Our contribution guide asks for an issue behind each change, and I could not find one linked here.

  • If an issue already exists, add Closes: #<number> or Related: #<number> to the description.
  • If not, open one — the link prefills the bug template with your PR title — then reference it the same way.

If this is a small doc or typo fix, ignore me. A maintainer can open the issue for you if you would rather.

@wolo-lab wolo-lab 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.

One change is needed before merge, and it is in the test, not the guard. The guard itself is right: part == nil short-circuits before the dereference, every non-nil part goes through the identical reflect check as before, and reverting the fix makes TestProcessResponseNilPart panic at the old line.

Must change before merge:

  • Add a case with text on both sides of the nil (inline note on the test). The current case only shows that the part after a nil survives. It cannot tell whether the nil disturbed aggregator state on the way past, and a guard that also flushed the text buffer on nil passes the whole package today.

Two comment-wording nits are inline as well, neither blocking.

One pre-existing thing worth knowing while you are in this area, since it is the same class as #2189: the per-chunk partial that ProcessResponse yields at stream_aggregator.go:79 aliases candidate.Content from converters.go:29, so once the aggregator stops panicking the nil part travels on in the partial response. The consumers on the base flow path (utils.FunctionCalls, the thought-only check) already skip nil parts, so I found no panic downstream. Not something this PR has to solve.

Review follow-up: the nil-part regression test now also covers text on
both sides of the nil (which must aggregate to a single part, so a
guard that flushed buffered text on nil fails it) and a trailing nil.
Reword the test and filter comments to say the nil part is skipped,
not the chunk dropped.
@PratikDhanave

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review — all three points addressed in fb4a955:

  • Text on both sides of the nil: the test is now a three-case table — nil first, nil last, and ["a", nil, "b"] asserting the result is the single aggregated part "ab", so a guard that flushed or reset buffered text on the nil fails it.
  • Reworded the test comment to say the nil part is skipped (not the chunk dropped), per your suggestion.
  • Reworded the filter comment to lead with the panic rationale: "A nil part would panic the reflect check below, so skip it the same way."

Also noted re: the aliased candidate.Content letting the nil travel on in the partial — agreed that's out of scope here since the downstream consumers already skip nils.

@wolo-lab wolo-lab 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.

LGTM, thanks!

@wolo-lab
wolo-lab merged commit 46ff99b into google:main Nov 9, 2026
15 checks passed
wolo-lab added a commit that referenced this pull request Nov 16, 2026
…2071, #1249, #2070, #2190, #2301, #2310, #2308) (#2350)

* fix(llminternal): keep a streamed text chunk's thought signature on the merged part (#2071)

(cherry picked from commit 28201d6)

* test(llminternal): pin keep-last semantics for streaming usage metadata (#1249)

(cherry picked from commit afe6c86)

* fix(llminternal): keep ModelVersion on the aggregated streaming response (#2070)

(cherry picked from commit 8be9129)

* fix(llminternal): drop nil parts in the streaming aggregator (#2190)

(cherry picked from commit 46ff99b)

* fix(llminternal): skip live responses that carry nothing (#2301)

(cherry picked from commit 8cddb9d)

* fix(llminternal): keep responses that carry only grounding metadata (#2310)

(cherry picked from commit fc6de07)

* fix(llminternal): keep streamed function-call and event ids stable (#2308)

(cherry picked from commit 3188db9)
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.

2 participants