gateway: an image Gemini doesn't read goes as PNG or a note, not a refused turn (fwxkogaia on X)
A Claude Desktop conversation on antigravity/gemini-4-carbon was turned away several times with "Antigravity: Request contains an invalid argument." (Anthropic to Gemini, 1.3 MB, the last input a tool result). Antigravity's model list says what Gemini 4 reads: png, jpeg, webp, heic, heif among the images. magpie passed an image block's media type through as it came, so a GIF, an SVG, or an image block with no type went to Google as it was, and since a history keeps it, every later turn failed. geminiInline now sends such an image as a PNG when Go can read it (a GIF's first frame), names a type from the bytes when the block gave none, and puts a short note in its place when it can't be read. It covers images in messages and in tool results, for every Gemini route that builds through buildCodeAssistSent. Run: TestGeminiRequestHasOnlyImageTypesGeminiReads (Antigravity and Gemini routes), red with the two call sites put back, green with them. On mote: go test -run 'Gemini|Antigravity|CodeAssist|Vertex|Image|Agy|Factory|Zed' ./internal/gateway ok, go vet for darwin, windows and linux nogui clean. Not reproduced: the reporter's own request. The owner's Antigravity account answers every generate call 403 (no licence) and Gemini 4 Carbon is listed disabled, Ultra only, so the live call could not be tried. The image type is the cause the model list supports, not one seen in their request; the other suspects (tool schemas, thinking level, tool_result shape, size: Carbon's window is 1M) already have handling or don't fit. The full suite was not run.
This commit is contained in:
@@ -408,7 +408,7 @@ func buildCodeAssistSent(r *Request, sent, agent string) []byte {
|
||||
}
|
||||
case Image, File:
|
||||
if p.Data != "" {
|
||||
parts = append(parts, map[string]any{"inlineData": map[string]any{"mimeType": p.MediaType, "data": p.Data}})
|
||||
parts = append(parts, geminiInline(p.MediaType, p.Data))
|
||||
} else if p.URL != "" {
|
||||
parts = append(parts, map[string]any{"fileData": map[string]any{"mimeType": p.MediaType, "fileUri": p.URL}})
|
||||
}
|
||||
@@ -456,7 +456,7 @@ func buildCodeAssistSent(r *Request, sent, agent string) []byte {
|
||||
var ims []map[string]any
|
||||
for _, im := range p.Images {
|
||||
if im.Data != "" {
|
||||
ims = append(ims, map[string]any{"inlineData": map[string]any{"mimeType": im.MediaType, "data": im.Data}})
|
||||
ims = append(ims, geminiInline(im.MediaType, im.Data))
|
||||
} else if im.URL != "" {
|
||||
ims = append(ims, map[string]any{"fileData": map[string]any{"mimeType": im.MediaType, "fileUri": im.URL}})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"encoding/base64"
|
||||
"image"
|
||||
"image/png"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// geminiImageTypes are the image types Gemini reads, as Antigravity's
|
||||
// model list gives them for Gemini 4 (supportedMimeTypes: png, jpeg,
|
||||
// webp, heic, heif). Any other type in a request turns the whole call away
|
||||
// with "Request contains an invalid argument." (fwxkogaia on X: Claude
|
||||
// Desktop → gemini-4-carbon), and a history keeps the image, so every
|
||||
// later turn is turned away too.
|
||||
var geminiImageTypes = map[string]bool{
|
||||
"image/png": true, "image/jpeg": true, "image/webp": true, "image/heic": true, "image/heif": true,
|
||||
}
|
||||
|
||||
// geminiInline is an image or file as the part Gemini takes it in, or the
|
||||
// text that says it was left out. An image of a type Gemini doesn't read
|
||||
// goes as a PNG when Go can read it (a GIF's first frame), a type that says
|
||||
// nothing is named by its bytes, and one that can't be read is a note, not
|
||||
// a refusal of the turn. Files that aren't images go as they are.
|
||||
func geminiInline(mediaType, data string) map[string]any {
|
||||
mt := strings.ToLower(strings.TrimSpace(mediaType))
|
||||
if i := strings.IndexByte(mt, ';'); i >= 0 {
|
||||
mt = strings.TrimSpace(mt[:i])
|
||||
}
|
||||
if mt == "" || mt == "application/octet-stream" || mt == "image" {
|
||||
if raw, err := base64.StdEncoding.DecodeString(firstChunk(data)); err == nil || len(raw) > 0 {
|
||||
if d := http.DetectContentType(raw); strings.HasPrefix(d, "image/") || mt == "" {
|
||||
mt = d
|
||||
}
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(mt, "image/") || geminiImageTypes[mt] {
|
||||
return map[string]any{"inlineData": map[string]any{"mimeType": cmp.Or(mt, mediaType), "data": data}}
|
||||
}
|
||||
if raw, err := base64.StdEncoding.DecodeString(data); err == nil {
|
||||
if img, _, err := image.Decode(bytes.NewReader(raw)); err == nil {
|
||||
var out bytes.Buffer
|
||||
if png.Encode(&out, img) == nil {
|
||||
return map[string]any{"inlineData": map[string]any{"mimeType": "image/png", "data": base64.StdEncoding.EncodeToString(out.Bytes())}}
|
||||
}
|
||||
}
|
||||
}
|
||||
return map[string]any{"text": "[An image of type " + mt + " was left out: Gemini reads PNG, JPEG, WebP, HEIC and HEIF.]"}
|
||||
}
|
||||
|
||||
// firstChunk is the start of base64 data, a multiple of four characters
|
||||
// long, enough to tell its type.
|
||||
func firstChunk(data string) string {
|
||||
if len(data) > 64 {
|
||||
data = data[:64]
|
||||
}
|
||||
return data[:len(data)/4*4]
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/gif"
|
||||
"image/png"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func gifData(t *testing.T) string {
|
||||
t.Helper()
|
||||
im := image.NewPaletted(image.Rect(0, 0, 4, 4), color.Palette{color.Black, color.White})
|
||||
var b bytes.Buffer
|
||||
if err := gif.Encode(&b, im, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(b.Bytes())
|
||||
}
|
||||
|
||||
// fwxkogaia on X: a Claude Desktop conversation on gemini-4-carbon was
|
||||
// turned away with "Request contains an invalid argument." Gemini reads
|
||||
// PNG, JPEG, WebP, HEIC and HEIF; a GIF or an SVG in the history failed
|
||||
// every turn after it.
|
||||
func TestGeminiRequestHasOnlyImageTypesGeminiReads(t *testing.T) {
|
||||
svg := base64.StdEncoding.EncodeToString([]byte(`<svg xmlns="http://www.w3.org/2000/svg"/>`))
|
||||
r := &Request{Messages: []Message{
|
||||
{Role: "user", Parts: []Part{
|
||||
{Kind: Text, Text: "look"},
|
||||
{Kind: Image, MediaType: "image/gif", Data: gifData(t)},
|
||||
{Kind: Image, MediaType: "image/svg+xml", Data: svg},
|
||||
{Kind: Image, MediaType: "", Data: gifData(t)},
|
||||
{Kind: ToolCall, ID: "t1", Name: "Read", Args: json.RawMessage(`{}`)},
|
||||
}},
|
||||
{Role: "user", Parts: []Part{{Kind: ToolResult, CallID: "t1", Text: "ok", Images: []Part{{MediaType: "image/gif", Data: gifData(t)}}}}},
|
||||
}}
|
||||
for _, agent := range []string{"antigravity", "gemini"} {
|
||||
body := string(buildCodeAssist(r, "gemini-4-carbon", agent))
|
||||
for _, bad := range []string{`"mimeType":"image/gif"`, `"mimeType":"image/svg+xml"`, `"mimeType":""`} {
|
||||
if strings.Contains(body, bad) {
|
||||
t.Errorf("%s: request carries %s", agent, bad)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(body, `"mimeType":"image/png"`) || !strings.Contains(body, "image of type image/svg+xml was left out") {
|
||||
t.Errorf("%s: GIF not as PNG or SVG not noted: %s", agent, body)
|
||||
}
|
||||
}
|
||||
var pngOK bool
|
||||
if m := geminiInline("image/gif", gifData(t))["inlineData"].(map[string]any); m["mimeType"] == "image/png" {
|
||||
raw, _ := base64.StdEncoding.DecodeString(m["data"].(string))
|
||||
_, err := png.Decode(bytes.NewReader(raw))
|
||||
pngOK = err == nil
|
||||
}
|
||||
if !pngOK {
|
||||
t.Error("GIF not written as a readable PNG")
|
||||
}
|
||||
// what Gemini reads, and files that aren't images, go as they are
|
||||
for _, mt := range []string{"image/png", "image/jpeg", "image/webp", "image/heic", "application/pdf"} {
|
||||
if m := geminiInline(mt, "AAAA")["inlineData"].(map[string]any); m["mimeType"] != mt || m["data"] != "AAAA" {
|
||||
t.Errorf("%s changed: %v", mt, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user