mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(translate): cap the gemini image count at 10
A client could send a huge n and the translator forwarded it as the Imagen sampleCount. The translator now refuses n above 10 with a request error.
This commit is contained in:
@@ -46,9 +46,13 @@ func geminiImageAspectRatio(size string) (string, error) {
|
||||
// rejected the same way chat's translators reject an unsupported field
|
||||
// (isTruthy, translate_anthropic.go): Imagen has no equivalent for
|
||||
// either. n defaults to 1 when absent or non-positive, matching OpenAI's
|
||||
// own default. The request's "model" field is deliberately never read
|
||||
// own default, and above maxImagesPerRequest is refused. The request's "model" field is deliberately never read
|
||||
// here — the adapter reads it separately to build the :predict URL, the
|
||||
// same convention chatCompletion/embeddings already follow.
|
||||
// maxImagesPerRequest bounds the n a client can ask for, so one call cannot
|
||||
// request an unbounded Imagen sampleCount.
|
||||
const maxImagesPerRequest = 10
|
||||
|
||||
func geminiImagesRequestFromOpenAI(req map[string]any) (map[string]any, error) {
|
||||
if rf, ok := req["response_format"].(string); ok && rf == "url" {
|
||||
return nil, &translateError{msg: `response_format "url" is not supported for gemini image generation (the gateway stores nothing to host a URL from)`}
|
||||
@@ -65,6 +69,9 @@ func geminiImagesRequestFromOpenAI(req map[string]any) (map[string]any, error) {
|
||||
n := int64(1)
|
||||
if v, ok := req["n"]; ok {
|
||||
if parsed, ok2 := toInt64(v); ok2 && parsed > 0 {
|
||||
if parsed > maxImagesPerRequest {
|
||||
return nil, &translateError{msg: fmt.Sprintf("n must be at most %d for gemini image generation", maxImagesPerRequest)}
|
||||
}
|
||||
n = parsed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,3 +157,39 @@ func TestGeminiImageAspectRatio_UnknownSizeErrors(t *testing.T) {
|
||||
t.Error("notSupported = true, want false (this is a 400, not a 501)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGeminiImagesRequest_CountBounds pins the accepted range for n: absent
|
||||
// or non-positive defaults to 1, and more than maxImagesPerRequest is refused.
|
||||
func TestGeminiImagesRequest_CountBounds(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
req map[string]any
|
||||
name string
|
||||
want int64
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "absent defaults to one", req: map[string]any{"prompt": "p"}, want: 1},
|
||||
{name: "zero defaults to one", req: map[string]any{"prompt": "p", "n": 0.0}, want: 1},
|
||||
{name: "negative defaults to one", req: map[string]any{"prompt": "p", "n": -4.0}, want: 1},
|
||||
{name: "in range", req: map[string]any{"prompt": "p", "n": 4.0}, want: 4},
|
||||
{name: "at the cap", req: map[string]any{"prompt": "p", "n": float64(maxImagesPerRequest)}, want: maxImagesPerRequest},
|
||||
{name: "one over the cap", req: map[string]any{"prompt": "p", "n": float64(maxImagesPerRequest + 1)}, wantErr: true},
|
||||
{name: "huge", req: map[string]any{"prompt": "p", "n": 1e9}, wantErr: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
out, err := geminiImagesRequestFromOpenAI(tt.req)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
if tt.wantErr {
|
||||
return
|
||||
}
|
||||
params, _ := out["parameters"].(map[string]any)
|
||||
if got, _ := params["sampleCount"].(int64); got != tt.want {
|
||||
t.Errorf("sampleCount = %v, want %d", params["sampleCount"], tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user