Repository navigation
fix(llminternal): drop nil parts in the streaming aggregator - #2190
Conversation
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.
|
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 this is a small doc or typo fix, ignore me. A maintainer can open the issue for you if you would rather. |
wolo-lab
left a comment
There was a problem hiding this comment.
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.
|
Thanks for the thorough review — all three points addressed in fb4a955:
Also noted re: the aliased |
…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)
Problem
The streaming aggregator's empty-part filter dereferences each part before any nil test:
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
TestProcessResponseNilCandidatedirectly above already treats degenerate chunks as reachable ("per converters.go").Change
if part == nil || reflect.ValueOf(*part).IsZero() { continue }, plusTestProcessResponseNilPart— 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-
Partclass insession.IsFinalResponse), kept separate since they are different subsystems.