gateway: a Gemini image model listed as models/{id} is drawn at models/{id}, and a Lite one is sent no 2K

Dani Ariyanto on Discord: Nano Banana 2.1 on Google AI Studio always
answered 404 "models/nano-banana-2.1 is not found". AI Studio's
OpenAI-compatible /models (the Google Gemini preset's list) names every
model models/{id}, so the provider's id is models/gemini-nano-banana-2.1
(google/models/gemini-nano-banana-2.1 at the gateway, the model
Automatic image generation picked for them). Chat already dropped the
prefix (GeminiPath), but drawGemini put the id after its own /models/,
and asked /v1beta/models/models/gemini-nano-banana-2.1:generateContent.
GeminiModelPath now drops a leading models/ itself, so every path built
from it agrees: chat (GeminiPath), image generation (drawGemini), a
plugin's Code Assist path and Vertex's. The listed ids are left as they
are: renaming them would orphan every pick saved as google/models/... .

The reporter also saw gemini-3.1-flash-lite-image (Nano Banana 2 Lite)
refuse size 1024x1536 with 400 "Image size 2K is not supported for this
model". takesImageSize now says which models may be asked an imageSize
past 1K: not Gemini 2.x (as before) and not a Gemini / Nano Banana Lite,
read without models/ or a relay's google/. imageConfigOf (Gemini API
and Code Assist draws) and openRouterSize (OpenRouter's Image API
resolution) both ask it; such a model gets the aspect ratio alone and
draws at its 1K, as it did with no size asked.

Run at this change on f9492fd8e (rebased since onto 6abea5f8e, which touches internal/agent only; vet below at the pushed head):
- mica: go test -timeout 45m -count=1 ./internal/gateway/ ./internal/provider/: ok (gateway 1124.8s, provider 160.1s)
- wsl-run (Linux): the same: ok (gateway 1532s, provider 199s)
- mica at the pushed head: the three new tests ok; GOOS=linux and GOOS=windows go vet ./internal/gateway/ ./internal/provider/ ok; gofmt -l clean.
Broken by hand (old gemini_api.go and draw.go, openRouterSize given the
new signature only): TestGeminiImageModelListedWithModelsPrefix FAIL
"404 ... (asked /v1beta/models/models/gemini-nano-banana-2.1:generateContent)",
TestOpenRouterSizeOfALiteImageModel FAIL "resolution:2K",
TestGeminiModelPathKeepsSlashes FAIL for the three models/ ids. With
takesImageSize returning true for Lite, the lite case is red
(imageConfig {"aspectRatio":"2:3","imageSize":"2K"}).
Not tried: a real AI Studio key drawing Nano Banana 2.1 or 2 Lite; the
tests use the reporter's 404 and 400 texts with no network. Whether
Lite takes imageSize 512 is not known, so it is asked none.
This commit is contained in:
yetone
2026-11-17 20:06:48 +08:00
parent 6abea5f8e8
commit b1d5912def
4 changed files with 138 additions and 11 deletions
+31 -9
View File
@@ -1796,7 +1796,7 @@ func (s *Server) drawOpenRouterImages(ctx context.Context, p provider.Provider,
req[k] = v
}
}
openRouterSize(req, d.Size)
openRouterSize(req, model, d.Size)
if len(d.Images) > 0 {
refs := make([]map[string]any, 0, len(d.Images))
for _, pic := range d.Images {
@@ -1819,8 +1819,9 @@ func (s *Server) drawOpenRouterImages(ctx context.Context, p provider.Provider,
// size among what it takes, and Sourceful's Riverflow V2 and xAI's answer
// a pixel size with 400 "does not support explicit pixel sizes. Use
// resolution and aspect_ratio instead." A ratio ("16:9") is an
// aspect_ratio, and a tier ("2K", "auto") goes as the size it is.
func openRouterSize(req map[string]any, size string) {
// aspect_ratio, and a tier ("2K", "auto") goes as the size it is. A
// model that draws at 1K alone (takesImageSize) is sent no resolution.
func openRouterSize(req map[string]any, model, size string) {
size = strings.TrimSpace(size)
if size == "" {
return
@@ -1835,7 +1836,7 @@ func openRouterSize(req map[string]any, size string) {
return
}
req["aspect_ratio"] = r
if t := imageSizeOf(size); t != "" {
if t := imageSizeOf(size); t != "" && takesImageSize(model) {
req["resolution"] = t
}
}
@@ -2334,16 +2335,14 @@ func imageSizeOf(size string) string {
// imageConfigOf is the imageConfig an images API size asks a Gemini image
// model to draw at: the ratio the size is nearest, with the imageSize
// beside it when the size asks past the model's 1K default. Gemini 2.x
// image models draw at 1K only and Google documents imageSize for the
// Gemini 3 image models, so a 2.x model is sent the ratio alone, as
// before. nil for none.
// beside it when the size asks past the model's 1K default and the model
// takes one (takesImageSize). nil for none.
func imageConfigOf(model, size string) map[string]string {
c := map[string]string{}
if r := aspectOf(size); r != "" {
c["aspectRatio"] = r
}
if s := imageSizeOf(size); s != "" && !strings.HasPrefix(strings.ToLower(model), "gemini-2") {
if s := imageSizeOf(size); s != "" && takesImageSize(model) {
c["imageSize"] = s
}
if len(c) == 0 {
@@ -2352,6 +2351,29 @@ func imageConfigOf(model, size string) map[string]string {
return c
}
// takesImageSize is whether a model can be asked for an imageSize past 1K.
// A Gemini image model that draws at 1K alone refuses one: Gemini 2.x's
// (Google documents imageSize for the Gemini 3 image models only), and a
// Lite's, which answers 2K with 400 "Image size 2K is not supported for
// this model" (gemini-3.1-flash-lite-image, Nano Banana 2 Lite: Dani
// Ariyanto on Discord). Such a model is sent the ratio alone and draws at
// its own 1K, as it does with no size asked. The id is read as Google
// names it, without models/ or a relay's google/; any other model is
// asked as before.
func takesImageSize(model string) bool {
m := strings.ToLower(strings.TrimSpace(model))
if i := strings.LastIndex(m, "/"); i >= 0 {
m = m[i+1:]
}
switch {
case strings.HasPrefix(m, "gemini-2"):
return false
case strings.HasPrefix(m, "gemini-") || strings.HasPrefix(m, "nano-banana"):
return !strings.Contains(m, "lite")
}
return true
}
// grokAspects are the aspect ratios Grok's Imagine API takes.
var grokAspects = []string{"1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "2:1", "1:2", "19.5:9", "9:19.5", "20:9", "9:20"}
@@ -0,0 +1,96 @@
package gateway
import (
"fmt"
"io"
"net/http"
"strings"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// AI Studio's OpenAI-compatible /models names each model models/{id}, and
// an image model listed so was drawn at /v1beta/models/models/{id}, which
// the Gemini API answered 404 "models/nano-banana-2.1 is not found" (Dani
// Ariyanto on Discord: google/models/gemini-nano-banana-2.1, the image
// model Automatic picked). It is drawn at /v1beta/models/{id}, as its
// chat already was (GeminiPath), and its imageConfig is read from the id
// without models/. A Lite image model, which answers imageSize 2K with
// 400 "Image size 2K is not supported for this model", is sent the ratio
// alone.
func TestGeminiImageModelListedWithModelsPrefix(t *testing.T) {
fresh(t)
var path string
var body []byte
if err := provider.Save(provider.Provider{ID: "gg", Name: "Google", Chat: "https://generativelanguage.googleapis.com/v1beta/openai", Key: "key",
Models: []string{"models/gemini-nano-banana-2.1", "models/gemini-3-pro-image-preview", "models/gemini-3.1-flash-lite-image", "gemini-3.1-flash-lite-image"}}); err != nil {
t.Fatal(err)
}
s := New()
s.client = &http.Client{Transport: countTransport(func(r *http.Request) (*http.Response, error) {
b, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
path, body = r.URL.Path, b
if strings.Contains(r.URL.Path, "/models/models/") {
return &http.Response{StatusCode: http.StatusNotFound, Header: http.Header{"Content-Type": {"application/json"}},
Body: io.NopCloser(strings.NewReader(`{"error":{"code":404,"message":"models/nano-banana-2.1 is not found for API version v1beta, or is not supported for generateContent.","status":"NOT_FOUND"}}`))}, nil
}
if !strings.HasSuffix(r.URL.Path, ":generateContent") {
return nil, fmt.Errorf("unexpected request to %s", r.URL)
}
return &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(strings.NewReader(geminiDrawn))}, nil
})}
draw := func(model, size string) {
t.Helper()
req := `{"model":"gg/` + model + `","prompt":"a magpie"`
if size != "" {
req += `,"size":"` + size + `"`
}
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", req+"}")
if code != 200 || len(a.Data) != 1 {
t.Fatalf("%s: %d %s (asked %s)", model, code, raw, path)
}
}
draw("models/gemini-nano-banana-2.1", "")
if want := "/v1beta/models/gemini-nano-banana-2.1:generateContent"; path != want {
t.Fatalf("drawn at %s, want %s", path, want)
}
// models/ hides nothing of the model: a Gemini 3 image model still
// takes the size past 1K
draw("models/gemini-3-pro-image-preview", "1536x1024")
if sent := gjson.GetBytes(body, "generationConfig.imageConfig").Raw; !sameConfig(sent, `{"aspectRatio":"3:2","imageSize":"2K"}`) {
t.Fatalf("pro 1536x1024: imageConfig was %s", sent)
}
// Nano Banana 2 Lite draws at 1K alone: the ratio, no imageSize, under
// either spelling
for _, m := range []string{"gemini-3.1-flash-lite-image", "models/gemini-3.1-flash-lite-image"} {
draw(m, "1024x1536")
if sent := gjson.GetBytes(body, "generationConfig.imageConfig").Raw; !sameConfig(sent, `{"aspectRatio":"2:3"}`) {
t.Fatalf("%s 1024x1536: imageConfig was %s", m, sent)
}
}
}
// OpenRouter's Image API is sent no resolution for a model that draws at
// 1K alone, and one for any other model asked past 1K, as before.
func TestOpenRouterSizeOfALiteImageModel(t *testing.T) {
for _, c := range []struct{ model, res string }{
{"google/gemini-3.1-flash-lite-image", ""},
{"google/gemini-3-pro-image-preview", "2K"},
{"sourceful/riverflow-v2", "2K"},
} {
req := map[string]any{}
openRouterSize(req, c.model, "1024x1536")
got, _ := req["resolution"].(string)
if got != c.res || req["aspect_ratio"] != "2:3" {
t.Errorf("%s: %v, want resolution %q beside 2:3", c.model, req, c.res)
}
}
}
+7 -2
View File
@@ -82,8 +82,13 @@ func geminiKept(u string) bool {
// /models/example/model-demo, the path the relay routes by, not
// /models/example%2Fmodel-demo (#6695, LuoRuiyao on X). An id with an
// empty, . or .. segment, which a server or proxy could clean into a
// path outside models/, goes as one escaped segment instead.
// path outside models/, goes as one escaped segment instead. An id with
// the models/ Google's own lists name a model by (AI Studio's
// OpenAI-compatible /models lists "models/gemini-…") is asked without
// it, since the path already has one: drawn at models/models/… the
// Gemini API answered 404 (Dani Ariyanto on Discord, Nano Banana 2.1).
func GeminiModelPath(model string) string {
model = strings.TrimPrefix(model, "models/")
segs := strings.Split(model, "/")
for i, s := range segs {
if s == "" || s == "." || s == ".." {
@@ -97,7 +102,7 @@ func GeminiModelPath(model string) string {
// GeminiPath is the path under a Gemini base model is asked at: streamed as
// server-sent events (alt=sse) when stream, else one JSON reply.
func GeminiPath(model string, stream bool) string {
m := GeminiModelPath(strings.TrimPrefix(model, "models/"))
m := GeminiModelPath(model)
if stream {
return "/models/" + m + ":streamGenerateContent?alt=sse"
}
+4
View File
@@ -55,6 +55,10 @@ func TestGeminiModelPathKeepsSlashes(t *testing.T) {
"a//b": "a%2F%2Fb",
"/abs": "%2Fabs",
"trail/": "trail%2F",
// the models/ Google's lists name a model by is the path's own
"models/gemini-nano-banana-2.1": "gemini-nano-banana-2.1",
"models/example/model-demo": "example/model-demo",
"models/../x": "..%2Fx",
} {
if got := GeminiModelPath(in); got != want {
t.Errorf("GeminiModelPath(%q) = %q, want %q", in, got, want)