Skip to content

fix: guard against nil LLMResponse in base_flow callLLM#630

Open
koriyoshi2041 wants to merge 1 commit into
google:mainfrom
koriyoshi2041:fix/nil-pointer-base-flow
Open

fix: guard against nil LLMResponse in base_flow callLLM#630
koriyoshi2041 wants to merge 1 commit into
google:mainfrom
koriyoshi2041:fix/nil-pointer-base-flow

Conversation

@koriyoshi2041
Copy link
Copy Markdown

Summary

Adds nil checks for LLMResponse in base_flow.go to prevent nil pointer dereference panics when a model yields a nil response.

Problem: In callLLM() and generateContent(), the code accesses resp.LLMResponse and resp.Content without checking for nil. When a model returns an empty stream or yields nil, this causes a panic.

Fix:

  • Added nil guard for resp.LLMResponse in callLLM() (line ~337) — skips the iteration
  • Added nil guard for resp in generateContent() (line ~413) — skips nil responses

Related to #586.

Test plan

  • Added TestCallLLMNilResponse — verifies no panic when model yields nil
  • go test ./internal/llminternal/... passes
  • go vet ./... passes

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Nil Pointer Dereference Fix: Implemented nil checks for LLMResponse within the callLLM function and for resp within the generateContent function in base_flow.go to prevent panics when a model yields a nil response or an empty stream.
  • New Test Case: Added TestCallLLMNilResponse in base_flow_test.go to specifically verify that the system does not panic when an LLM model returns a nil response, addressing a regression related to issue Nil pointer dereference in base_flow.go: runconfig.FromContext returns nil #586.

🧠 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
  • internal/llminternal/base_flow.go
    • Added a nil guard for resp.LLMResponse in callLLM() to skip iteration if the response is nil.
    • Added a nil guard for resp in generateContent() to skip nil responses before further processing.
  • internal/llminternal/base_flow_test.go
    • Imported context, iter, and runconfig packages.
    • Added a mockModel struct to simulate LLM behavior for testing.
    • Implemented TestCallLLMNilResponse to verify that callLLM does not panic when a model yields a nil LLMResponse.
Activity
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Mar 6, 2026

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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread internal/llminternal/base_flow.go Outdated
Comment on lines +337 to +341
// Guard against nil LLMResponse to prevent nil pointer dereference
// when the model returns no response (e.g. empty stream).
if resp.LLMResponse == nil {
continue
}
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.

medium

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.

@koriyoshi2041 koriyoshi2041 force-pushed the fix/nil-pointer-base-flow branch 2 times, most recently from 920857b to edf91d3 Compare May 31, 2026 09:27
@koriyoshi2041
Copy link
Copy Markdown
Author

Rebased onto latest main to resolve the conflict — it was a textual clash in base_flow_test.go with the newly added TestPreprocess_Toolset, plus callLLM gained an artifactDelta parameter upstream which the regression test now passes. Both tests pass and go vet/gofmt are clean.

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
@koriyoshi2041 koriyoshi2041 force-pushed the fix/nil-pointer-base-flow branch from edf91d3 to 622417e Compare June 8, 2026 08:13
@koriyoshi2041
Copy link
Copy Markdown
Author

Rebased this onto current main and tightened the patch based on the earlier review note: the extra callLLM nil guard is gone now, so the fix lives at the generateContent boundary and the regression test still covers the callLLM entry point.

Checked locally:

go test ./internal/llminternal
go vet ./internal/llminternal
git diff --check

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.

1 participant