fix(antigravity): sanitize request.contents to remove invalid metadata entries#1326
fix(antigravity): sanitize request.contents to remove invalid metadata entries#1326HaD0Yun wants to merge 1 commit intorouter-for-me:mainfrom
Conversation
…a entries When conversation history is malformed or contains previous request objects as message entries, the Antigravity/Gemini API returns 'Invalid JSON payload' errors like: Unknown name 'safetySettings' at 'request.contents[15]' This fix adds sanitizeRequestContents() that filters out any content entries containing request-level metadata fields (safetySettings, model, userAgent, requestType, requestId, sessionId, systemInstruction, toolConfig, etc.) before sending to the upstream API. Fixes invalid content entries being passed through that contain full request objects instead of proper message content (role + parts).
Summary of ChangesHello @HaD0Yun, 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 API requests to the Antigravity/Gemini API by implementing a sanitization mechanism for conversation history. It addresses a recurring issue where malformed entries containing request-level metadata fields in the 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. 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
|
There was a problem hiding this comment.
Code Review
The pull request introduces a sanitizeRequestContents function to filter invalid entries from the request.contents field, preventing Invalid JSON payload errors from the Antigravity/Gemini API. This is a good improvement for robustness. The changes include adding the function call in buildRequest and implementing the sanitization logic, along with comprehensive unit tests. Overall, the solution addresses the problem effectively.
| } | ||
| newContentsJSON += "]" | ||
|
|
||
| result, _ := sjson.SetRawBytes(payload, contentsPath, []byte(newContentsJSON)) |
There was a problem hiding this comment.
The error returned by sjson.SetRawBytes is currently ignored. While sjson is generally robust, it's a good practice to handle potential errors, especially when modifying a critical payload. If SetRawBytes fails, the function will return the original, potentially invalid, payload without any indication of an issue, which could lead to the very errors this function aims to prevent.
result, err := sjson.SetRawBytes(payload, contentsPath, []byte(newContentsJSON))
if err != nil {
log.Errorf("sanitizeRequestContents: failed to set raw bytes for contents: %v", err)
return payload // Return original payload on error, or handle as appropriate
}| newContentsJSON := "[" | ||
| for i, content := range validContents { | ||
| if i > 0 { | ||
| newContentsJSON += "," | ||
| } | ||
| newContentsJSON += content.Raw | ||
| } | ||
| newContentsJSON += "]" |
There was a problem hiding this comment.
Reconstructing the newContentsJSON string by concatenating content.Raw in a loop can be inefficient for very large contents arrays. While gjson.Result.Raw provides the raw JSON, repeatedly concatenating strings can lead to multiple memory allocations. Consider building a slice of interface{} or map[string]interface{} from validContents and then marshaling it once to JSON, or exploring if sjson offers a more direct way to replace an array with a slice of gjson.Result objects.
| } | ||
|
|
||
| for i, content := range contentsResult.Array() { | ||
| invalidFields := []string{"safetySettings", "model", "userAgent", "requestType", "requestId", "sessionId", "systemInstruction", "toolConfig", "generationConfig", "project", "request", "contents"} |
There was a problem hiding this comment.
The invalidFields slice is created inside the loop for each content entry. This leads to redundant allocations and can be inefficient. It should be defined once outside this inner loop.
| invalidFields := []string{"safetySettings", "model", "userAgent", "requestType", "requestId", "sessionId", "systemInstruction", "toolConfig", "generationConfig", "project", "request", "contents"} | |
| invalidFields := []string{"safetySettings", "model", "userAgent", "requestType", "requestId", "sessionId", "systemInstruction", "toolConfig", "generationConfig", "project", "request", "contents"} | |
| for _, field := range invalidFields { |
|
Please provide a payload to verify this PR. I believe these fields are all valid and do not require any additional removal. |
Summary
sanitizeRequestContents()function to filter invalid content entries before sending to Antigravity/Gemini APIThe Problem
When conversation history is malformed or contains previous request objects as message entries, the Antigravity/Gemini API returns errors like:
The Fix
This PR adds a sanitization step in
buildRequest()that:request.contentsInvalid fields filtered:
safetySettings,model,userAgent,requestType,requestId,sessionId,systemInstruction,toolConfig,generationConfig,project,request,contentsValid content entries should only contain:
role,partsTesting
sanitizeRequestContents()