Repository navigation
fix: skip nil responses in generateContent - #630
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the LLM interaction logic by introducing critical nil checks. It resolves a potential stability issue where the application could crash due to nil pointer dereferences if an LLM model returned an unexpected nil response. The changes ensure that the system gracefully handles such scenarios, improving overall reliability. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
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. |
There was a problem hiding this comment.
Code Review
This pull request addresses a potential nil pointer dereference in base_flow.go by adding nil checks in callLLM and generateContent. The change in generateContent correctly prevents a panic when a model yields a nil response, and a regression test has been added to verify this fix. My review suggests that the corresponding nil check added in callLLM may be redundant due to the fix in generateContent and could be removed for code clarity.
920857b to
edf91d3
Compare
|
Rebased onto latest |
edf91d3 to
622417e
Compare
|
Rebased this onto current Checked locally: |
karolpiotrowicz
left a comment
There was a problem hiding this comment.
Apologies for the long silence here — this sat without a maintainer review far longer than it should have.
The change still applies. generateContent on main at 058933d still reaches base_flow.go:944 — } else if !resp.Partial { — with no nil check, and nothing fixed it in the meantime. Driving a third-party model.LLM that yields (nil, nil) through the public runner: on main the invocation ends with node "a" panicked: runtime error: invalid memory address or nil pointer dereference, and with your patch it does not. A mixed [nil, final] stream still delivers the final response, so the continue does what it should. The guard is at the right layer.
Three things before it can go in.
Rebase. base_flow.go merges cleanly, but base_flow_test.go conflicts — main has grown a lot of tests since your base and the module moved to google.golang.org/adk/v2. It is a plain end-of-file collision, so it should be quick.
A trailing nil after a partial response wipes the span. base_flow.go:939 does lastResponse = *response before your guard runs, so a skipped nil overwrites the last good response and the deferred span-end records nothing. On a [partial(prompt=10, candidates=20), nil] stream the generate_content span comes out with empty input and output token attributes, where the same stream without the trailing nil records 10 and 20. [final, nil] is unaffected — a non-partial response has already closed the span — so this is specifically the streaming-then-nil shape.
The fix is to move the check above line 939, but please use the conjunct form:
if resp == nil && err == nil {
continue
}The && err == nil matters more than it looks. Every model in the tree reports failure as yield(nil, err), so a bare if resp == nil { continue } hoisted above line 939 skips the error branch entirely — I ran it, and a model returning an error produced zero yields with no error reaching the caller. Your current placement does not have that problem. It is only the hoist that needs the guard on err.
The test never runs its assertion. Once the fix works callLLM yields nothing, so the loop body — and the only t.Fatalf — is dead. It would pass equally if continue were return, which would drop every response after a nil. It is a real regression test for the panic, so it earns its place, but asserting the yielded count and adding the [nil, final] case would pin the skip-and-keep-consuming behaviour.
Two smaller ones. The comment cites #586, but that was the runconfig.FromContext nil, fixed separately and already referenced in this same file at base_flow.go:842 — better to describe the real trigger, since no in-tree model can produce a nil response and what this defends against is a third-party model.LLM. And the new mockModel duplicates mockModelForTest in the same package, so you can drop it.
One thing I checked and it is fine: the skipped nil produces a run with no events and a nil error, which looks like it hides the failure. main already behaves that way for a non-nil content-less response — base_flow.go:652 skips those deliberately, mirroring adk-python's base_llm_flow.py:1484. Your change routes one more input into an outcome the framework already treats as normal. A log line on the skip would be a nice touch, not a blocker.
Not yours to fix, but worth knowing: the same "a continue bypasses the consumer's only stop signal" shape already exists at base_flow.go:652, and a model emitting content-less responses without bound will spin there on main today. I will raise that separately.
Finally, the title still says callLLM — since your June revision the fix lives entirely in generateContent.
622417e to
8613b7c
Compare
|
Addressed all requested changes and rebased onto current
Verified locally with |
karolpiotrowicz
left a comment
There was a problem hiding this comment.
The revision does everything I asked, and the conjunct form is exactly right.
I re-ran the checks against the rebased head (8613b7c) on top of current main (ffddb2a). The span keeps the partial's token attributes now — a [partial(prompt=10, candidates=20), nil] stream records 10 and 20 where it previously recorded nothing — model errors still reach the caller, and [nil, final] still delivers the final response.
The part worth calling out is the test work, which is stronger than what I asked for. I mutated the guard three ways in a scratch copy and re-ran your committed suite against each:
guard deleted -> TestCallLLMSkipsNilResponseAndContinues/nil_only panics
guard back in the old spot -> TestGenerateContentTracingNoFinalResponse: expected input tokens 10, got
guard reduced to bare resp == nil -> TestGenerateContentTracingError: expected span status "Error", got "Unset"
All three regressions are now caught by tests in the repo. In particular that last one means someone can no longer quietly simplify the condition back to resp == nil and swallow every model error — that was the gap I was most worried about leaving open, and appending the trailing yield(nil, nil) to the existing telemetry test closes it neatly.
Three small things, none of them blocking, all optional.
The new table has no error column, so the (nil, err) case cannot be written in it. That case is covered, but only indirectly, by the span-status assertion in TestGenerateContentTracingError. A wantErr field and a third row asserting that callLLM yields exactly one item carrying the error would make the guarantee direct rather than inferred from telemetry.
The nil then final row asserts a count of one but not that the surviving response is the one carrying "done". It happens not to matter today, since the obvious wrong implementation would panic rather than yield the wrong item, but capturing the response and comparing its text would make the test say what its name claims.
The comment reads "may yield an empty response" while the guard tests nil-ness. A non-nil &model.LLMResponse{} trailing a partial still blanks the span — I checked, and it does that on main too, so it is not something this PR introduced and not yours to fix here. Saying "a nil response" would just keep the comment matched to the code.
One thing I want to be explicit about, since I raised it last time and it is still true: continue skips the yield, so a model emitting nils without bound still cannot be stopped by the consumer. I said it was not yours to fix and I stand by that — the identical shape already exists at base_flow.go:652 and is reachable on main today with a more ordinary producer. I will file that separately rather than hold this up for it.
CI is green across the board, including apidiff. From my side this is ready.
Co-authored-by: wolo <[email protected]>
…1630, #630) (#1692) * fix(internal/utils): skip nil Parts in the Parts accessors (#1556) (cherry picked from commit f4666ec) * fix(llminternal): build arrays when assembling streamed function call arguments (#1630) (cherry picked from commit d6bdb0d) * fix: skip empty model responses in generateContent (#630) (cherry picked from commit 8b0de7b)
Co-authored-by: wolo <[email protected]>
Summary
Adds nil checks for
LLMResponseinbase_flow.goto prevent nil pointer dereference panics when a model yields a nil response.Problem: In
callLLM()andgenerateContent(), the code accessesresp.LLMResponseandresp.Contentwithout checking for nil. When a model returns an empty stream or yieldsnil, this causes a panic.Fix:
resp.LLMResponseincallLLM()(line ~337) — skips the iterationrespingenerateContent()(line ~413) — skips nil responsesRelated to #586.
Test plan
TestCallLLMNilResponse— verifies no panic when model yields nilgo test ./internal/llminternal/...passesgo vet ./...passes