-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: disable Gemini grounding for code generation to preserve array indices #8918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…dex removal When Gemini grounding is enabled, it misinterprets array indices like [0] as citation markers and removes them from generated code. This fix detects code generation contexts and temporarily disables grounding to preserve array indices. Fixes #8914
Review SummaryI've reviewed this PR and identified 2 issues that should be addressed:
|
| "def\\s+\\w+\\s*\\(", // Python function definition | ||
| "function\\s+\\w+\\s*\\(", // JavaScript function | ||
| "class\\s+\\w+", // Class definition | ||
| "\\[\\d+\\]", // Array index patterns |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern is overly broad and will incorrectly match citation-style references in regular text (e.g., "see reference [1]" or "table [2] shows results"), causing grounding to be disabled for non-code contexts where users discuss numbered references or citations. Consider removing this pattern or making it more specific to code contexts by requiring surrounding code syntax.
| "\\[\\d+\\]", // Array index patterns | ||
| "array\\[", | ||
| "list\\[", | ||
| "fruits\\[0\\]", // Specific to the reported issue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern is redundant as it's already covered by the more general "\\[\\d+\\]" pattern on line 83. The broader pattern will match fruits[0] along with any other array index. Recommend removing this specific pattern to reduce code duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review complete. Found 2 issues that should be addressed before approval.
Description
This PR addresses Issue #8914 where Gemini's grounding feature was incorrectly removing array indices like
[0]from generated code, interpreting them as citation markers.Problem
When Gemini's grounding feature is enabled, it misinterprets bracketed numbers in code (e.g.,
fruits[0]) as citation markers and removes them. This causes generated code to be syntactically incorrect.Solution
Added intelligent detection of code generation contexts to temporarily disable grounding when:
Changes
isCodeGenerationContext()method to detect code-related promptscreateMessage()andcompletePrompt()to conditionally disable groundingTesting
Notes
This is a workaround for what appears to be a limitation in Google's Gemini API grounding feature. The grounding feature interprets bracketed numbers as citations, which is problematic for code generation. This fix ensures that grounding is only disabled when necessary, maintaining its benefits for non-code contexts.
Fixes #8914
cc @sergedc - This PR implements a targeted fix that detects code generation contexts and temporarily disables grounding to preserve array indices. The grounding feature will still work normally for non-code queries.
Important
Disable Gemini grounding for code generation contexts to preserve array indices, with detection logic and tests added.
isCodeGenerationContext()ingemini.tsto detect code generation contexts and disable grounding.createMessage()andcompletePrompt()ingemini.tsto conditionally disable grounding based on code context.gemini.spec.tsto verify grounding is disabled in code contexts and enabled otherwise.This description was created by
for 67f0f0e. You can customize this summary. It will automatically update as commits are pushed.