fix: guard against nil LLMResponse in base_flow callLLM#630
fix: guard against nil LLMResponse in base_flow callLLM#630koriyoshi2041 wants to merge 1 commit into
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.
| // Guard against nil LLMResponse to prevent nil pointer dereference | ||
| // when the model returns no response (e.g. empty stream). | ||
| if resp.LLMResponse == nil { | ||
| continue | ||
| } |
There was a problem hiding this comment.
This nil check for resp.LLMResponse appears to be redundant due to the corresponding fix in generateContent. The generateContent function will no longer yield a response where LLMResponse is nil if the error is also nil. In cases where generateContent yields an error, the preceding if err != nil block in this function is executed and always returns, so this check is not reached. As this code seems to be unreachable, it could be removed to improve clarity.
920857b to
edf91d3
Compare
|
Rebased onto latest |
When a model's GenerateContent yields a nil LLMResponse, the code in both generateContent and callLLM accesses fields on the nil pointer (resp.Partial and resp.Content), causing a panic. Add nil guards in generateContent to skip nil responses and in callLLM to continue past nil LLMResponse values. Add a regression test that verifies callLLM handles nil responses without panicking. Fixes google#586
edf91d3 to
622417e
Compare
|
Rebased this onto current Checked locally: |
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