diff --git a/internal/gateway/draw.go b/internal/gateway/draw.go index 713234901..d9ffb8464 100644 --- a/internal/gateway/draw.go +++ b/internal/gateway/draw.go @@ -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"} diff --git a/internal/gateway/gemini_models_prefix_test.go b/internal/gateway/gemini_models_prefix_test.go new file mode 100644 index 000000000..669278c57 --- /dev/null +++ b/internal/gateway/gemini_models_prefix_test.go @@ -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) + } + } +} diff --git a/internal/provider/gemini_api.go b/internal/provider/gemini_api.go index 328ffe36d..7df7ebefd 100644 --- a/internal/provider/gemini_api.go +++ b/internal/provider/gemini_api.go @@ -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" } diff --git a/internal/provider/gemini_api_test.go b/internal/provider/gemini_api_test.go index 497073c7c..948dd8aa4 100644 --- a/internal/provider/gemini_api_test.go +++ b/internal/provider/gemini_api_test.go @@ -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)