Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: an image's detail reaches a Chat or Responses upstream; Responses' original goes to Chat as high (mysterious_dove)

yetone44a626bed3c4
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 614c47c3d..d44e51123 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -200,6 +200,7 @@ recorded as a prediction failure (`noPrediction`).
- A tool call a model writes into its reply's text, when the request offered tools, is made the call it is ([`textcalls.go`](../../internal/gateway/textcalls.go), #823): Hermes' `<tool_call>{…}</tool_call>`, DeepSeek's DSML (`<|DSML|invoke name=…>` with its parameters; its mark found as the parse regexes read it, by `dsmlMark`, not from a list of spellings: any run of pipes, ASCII or full-width or mixed, spaces after `<` and around `/`, `<||DSML||` too, #2623), DeepSeek's half-Hermes `<parameter>` form (#917) and GLM's `<arg_key>`/`<arg_value>` (#906), read by `parseTextCalls`. Each block must name an offered tool and have arguments that can be read; else the text goes on as it came. The text before the first block is kept, what is left of the tags goes, and the reply ends as one that called tools (`finish_reason` `tool_calls`, `stop_reason` `tool_use`). A DSML close with no block before it (`</||DSML|| calls>`) is held as a block is, and dropped when it is tags and nothing else. A reply whose model made a call itself has no call read out of its text, but tags and nothing else left in it after the call go, with the spaces before them (喜欢一个人 on Discord: a relay made the calls of DeepSeek's invokes and left the wrapper's close as text). Every route reads it the same way: a relayed Chat stream (`textCallTidy`), a relayed Chat reply that came whole, JSON or under `text/plain` or no type (`chatWhole` with `wholeTextCalls`, #1845: a `stream:false` request kept the markup and `finish_reason` `stop`), which also covers a vendor that answered a streamed request whole (`wholeAsStream`), and every translation, which magpie always asks streamed, a plugin's included, whatever the client asked (`textCallSee`). A relayed Responses or Messages upstream's text is not read for calls. Covered by `TestTextWritten*`, `TestDSMLClose*`, `TestDSMLBlockClosedWithCalls`, `TestGLMTextCall*`, `TestPluginTextWrittenCall` and `TestQuietWholeReplyStreamed`.
- Structured output is carried across every translation (`Format`, [`format.go`](../../internal/gateway/format.go)). It is read from Responses' `text.format`, Chat's `response_format`, Anthropic's `output_config.format` and Gemini's `responseMimeType` with `responseJsonSchema`/`responseSchema`: `json_schema` with its name, description, schema and strict, or `json_object`. Each upstream is asked in its own field: `text.format` on Responses, `response_format` on Chat, `output_config.format` on Messages, and `generationConfig.responseMimeType`/`responseJsonSchema` on Gemini and Code Assist. An API with no name for it gets `response`. Messages and the Claude subscription's `--json-schema` take a schema only, so `json_object` is told in the system prompt there (`inSystem`). A 400 or 422 that names the field (`refusesFormat`: DeepSeek's "This response_format type is unavailable now", a schema Anthropic can't hold) marks the provider and model (`formatRefused`), and the request is asked again with the format told in the system prompt, as are later requests to that provider and model. A relayed request keeps its format as sent. A Gemini client's format is also still told in its system prompt, as before. A model may still wrap its JSON answer in a Markdown code fence; a client that asked for structured output, in a field or told in the system prompt (`wantsJSON`), gets the JSON inside a single unlabelled or json-labelled fence when that content is a complete valid JSON value, streamed (`fenceEncoder`) or whole (`unfencedResult`), in [`format_fence.go`](../../internal/gateway/format_fence.go). Invalid or truncated JSON, other fence languages, text outside the fence, and answers to plain-text clients reach the client as they came. This normalization applies to translated replies; native protocol passthrough stays byte-preserving.
- A tool's images reach the model on every translation (`toolOutput` in [`ir.go`](../../internal/gateway/ir.go) reads them from Anthropic's `tool_result`, Responses' `function_call_output`, such as Codex's `view_image` output `[{"type":"input_image","image_url":"data:…"}]`, and a Chat tool message). Anthropic's Messages keeps them in the `tool_result`. Chat puts them in a user message right after the results, and the result says they follow; Gemini and Code Assist after the function's response. Kiro's results hold text alone, so Kiro's plugin (from 0.1.9 on) sends them with the message that answers the call, and only the latest images go again (#369, #1861). `TestCodexToolImageReachesAnthropicPlugin` and the Kiro plugin's `tool-images.test.mjs`.
+- An image's `detail` (`Part.Detail`, read from a Responses `input_image` and a Chat `image_url`, in a message or a tool's output) is carried on a translation to Chat and Responses (`chatImage`, `chatDetail`, `responsesDetail` in [`chat.go`](../../internal/gateway/chat.go)). Chat takes `low`, `high` and `auto` as they came, and Responses' `original`, which Chat lacks, as `high`; an image without one goes without. Responses takes the four as they came, and anything else, none included, as `auto` (#1474). Messages and Gemini have no such field, so it isn't sent there (mysterious_dove on Discord, Codex: `high` and `original` were dropped on the way to Chat; `TestResponsesImageDetailReachesChat`, `TestChatImageDetailReachesResponses`).
- Group members are `provider/model[:effort]`. A member with a fixed effort is asked for that effort, bounded by the group's levels (#671).
- A group offers agents the reasoning levels its members have in common (`groupEntries` in [`group.go`](../../internal/provider/group.go)), or the ones it names (`Group.Levels`, #295). A member that doesn't follow the agent's effort is left out of that: one fixed at an effort, one whose levels nothing knows (#597), one that thinks with no levels, one the user made reasonless, and one known not to think (`Entry.Quiet`), which the gateway sends no reasoning ask (`withoutReasoningAsk`, [`quiet.go`](../../internal/gateway/quiet.go)). A quiet member used to empty the group's levels, so Codex got its entry with no `supported_reasoning_levels`, showed no effort control and ran at its own medium (#2127, `TestGroupQuietMemberKeepsCodexEffortControl`).
- An agent whose config can't carry an effort has one picked in magpie (`provider.AgentEffort`, settings' `agentEfforts`; Cursor Private Inference's row, #1003). It is asked for on that agent's requests to a model that reasons (`Provider.Thinks`), in place of the level the request sent, fitted to the model's levels and bounded by the group's. Order: a member's fixed effort, then the effort the rule that put the member first sets (`Rule.UseEffort`), then a model's `:effort` suffix, then the turn's pick (a routing rule's), then the agent's pick, then the request's own. A model that doesn't reason is sent as asked; the default (none picked) leaves every request as it came. The pick beats the request's own because Cursor sends its own default as often as a real choice. Picking Default hands the choice back to Cursor.
diff --git a/internal/gateway/chat.go b/internal/gateway/chat.go
index bdd4faf49..2f0bf60c9 100644
--- a/internal/gateway/chat.go
+++ b/internal/gateway/chat.go
@@ -167,7 +167,8 @@ func chatParts(raw json.RawMessage) []Part {
Type string `json:"type"`
Text string `json:"text"`
ImageURL struct {
- URL string `json:"url"`
+ URL string `json:"url"`
+ Detail string `json:"detail"`
} `json:"image_url"`
File struct {
FileData string `json:"file_data"`
@@ -182,7 +183,9 @@ func chatParts(raw json.RawMessage) []Part {
case "text":
out = append(out, Part{Kind: Text, Text: it.Text})
case "image_url":
- out = append(out, imagePart(it.ImageURL.URL))
+ im := imagePart(it.ImageURL.URL)
+ im.Detail = it.ImageURL.Detail
+ out = append(out, im)
case "file":
if p, ok := chatFile(it.File.FileData, it.File.FileID, it.File.Filename); ok {
out = append(out, p)
@@ -225,6 +228,38 @@ func imagePart(u string) Part {
return Part{Kind: Image, URL: u}
}
+// chatImage is an image as a Chat Completions image_url, with the detail
+// the client asked for: Chat takes low, high and auto as they are, and
+// Responses' original, which Chat lacks, is high, the closest it has.
+func chatImage(p Part) map[string]any {
+ u := map[string]any{"url": dataURL(p)}
+ if d := chatDetail(p.Detail); d != "" {
+ u["detail"] = d
+ }
+ return map[string]any{"type": "image_url", "image_url": u}
+}
+
+// chatDetail is detail as Chat Completions takes it, or "" for none.
+func chatDetail(detail string) string {
+ switch d := strings.ToLower(strings.TrimSpace(detail)); d {
+ case "low", "high", "auto":
+ return d
+ case "original":
+ return "high"
+ }
+ return ""
+}
+
+// responsesDetail is detail as a Responses input_image takes it: the
+// client's own when the API knows it, else auto.
+func responsesDetail(detail string) string {
+ switch d := strings.ToLower(strings.TrimSpace(detail)); d {
+ case "low", "high", "auto", "original":
+ return d
+ }
+ return "auto"
+}
+
func dataURL(p Part) string {
if p.URL != "" && p.Data == "" {
return p.URL
@@ -298,7 +333,7 @@ func buildChat(r *Request, model, host string, rejectTemp bool) []byte {
}
seen = append(seen, map[string]any{"type": "text", "text": "[From the result of " + of + ":]"})
for _, im := range p.Images {
- seen = append(seen, map[string]any{"type": "image_url", "image_url": map[string]any{"url": dataURL(im)}})
+ seen = append(seen, chatImage(im))
}
}
showSeen := func() {
@@ -420,7 +455,7 @@ func buildChat(r *Request, model, host string, rejectTemp bool) []byte {
content = append(content, map[string]any{"type": "text", "text": attachmentText(p)})
case Image:
plain = false
- content = append(content, map[string]any{"type": "image_url", "image_url": map[string]any{"url": dataURL(p)}})
+ content = append(content, chatImage(p))
case ToolResult:
out := p.Text
if n := len(p.Images); n > 0 {
diff --git a/internal/gateway/image_detail_chat_test.go b/internal/gateway/image_detail_chat_test.go
new file mode 100644
index 000000000..bea85395e
--- /dev/null
+++ b/internal/gateway/image_detail_chat_test.go
@@ -0,0 +1,97 @@
+package gateway
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+// chatImageDetails is the detail of every image_url in a Chat body, in
+// order, "" for one without.
+func chatImageDetails(t *testing.T, body []byte) []string {
+ t.Helper()
+ var v any
+ if err := json.Unmarshal(body, &v); err != nil {
+ t.Fatalf("body: %v\n%s", err, body)
+ }
+ var out []string
+ var walk func(any)
+ walk = func(v any) {
+ switch x := v.(type) {
+ case map[string]any:
+ if x["type"] == "image_url" {
+ u, _ := x["image_url"].(map[string]any)
+ d, _ := u["detail"].(string)
+ out = append(out, d)
+ }
+ for k, c := range x {
+ if k != "image_url" {
+ walk(c)
+ }
+ }
+ case []any:
+ for _, c := range x {
+ walk(c)
+ }
+ }
+ }
+ walk(v)
+ return out
+}
+
+// A Responses request's image detail reaches a Chat Completions upstream:
+// Chat's image_url takes low, high and auto as they are, and Responses'
+// original, which Chat lacks, goes as high, the closest it has. An image
+// with no detail goes without one. A tool's image keeps its detail too
+// (mysterious_dove, Codex: detail=high and detail=original were dropped).
+func TestResponsesImageDetailReachesChat(t *testing.T) {
+ img := `"image_url":"data:image/png;base64,` + pngA + `"`
+ body := `{"model":"m","input":[
+ {"role":"user","content":[{"type":"input_text","text":"look"},
+ {"type":"input_image","detail":"high",` + img + `},
+ {"type":"input_image","detail":"original",` + img + `},
+ {"type":"input_image","detail":"low",` + img + `},
+ {"type":"input_image","detail":"auto",` + img + `},
+ {"type":"input_image",` + img + `}]},
+ {"type":"function_call","call_id":"c1","name":"view_image","arguments":"{}"},
+ {"type":"function_call_output","call_id":"c1","output":[{"type":"input_image","detail":"original",` + img + `}]}]}`
+ r, err := parseResponses([]byte(body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ out := buildChat(r, "m", "api.example.com", false)
+ got := chatImageDetails(t, out)
+ want := []string{"high", "high", "low", "auto", "", "high"}
+ if len(got) != len(want) {
+ t.Fatalf("want %d images, got %v\n%s", len(want), got, out)
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ t.Errorf("image %d: detail %q, want %q\n%s", i, got[i], want[i], out)
+ }
+ }
+}
+
+// A Chat request's image detail reaches a Responses upstream as the client
+// asked, not as auto; one without detail still goes as auto (#1474).
+func TestChatImageDetailReachesResponses(t *testing.T) {
+ url := `"data:image/png;base64,` + pngA + `"`
+ body := `{"model":"m","messages":[{"role":"user","content":[{"type":"text","text":"look"},
+ {"type":"image_url","image_url":{"url":` + url + `,"detail":"low"}},
+ {"type":"image_url","image_url":{"url":` + url + `,"detail":"high"}},
+ {"type":"image_url","image_url":{"url":` + url + `}}]}]}`
+ r, err := parseChat([]byte(body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ out := buildResponses(r, "m", "api.example.com", false)
+ images := inputImages(t, string(out))
+ want := []string{"low", "high", "auto"}
+ if len(images) != len(want) {
+ t.Fatalf("want %d input_image, got %d\n%s", len(want), len(images), out)
+ }
+ for i, im := range images {
+ if d, _ := im["detail"].(string); d != want[i] {
+ t.Errorf("image %d: detail %q, want %q\n%s", i, d, want[i], out)
+ }
+ }
+}
diff --git a/internal/gateway/ir.go b/internal/gateway/ir.go
index bb6cf54a2..080a37e7d 100644
--- a/internal/gateway/ir.go
+++ b/internal/gateway/ir.go
@@ -39,6 +39,10 @@ type Part struct {
MediaType string
Data string // base64
URL string
+ // Detail is an image's detail as the client asked for it (low, high,
+ // auto, or Responses' original), which a Chat or Responses upstream
+ // is given back (chatDetail, responsesDetail)
+ Detail string
// tool_call
ID string
@@ -761,6 +765,7 @@ func toolOutput(raw json.RawMessage) (string, []Part) {
URL string `json:"url"`
} `json:"source"`
ImageURL json.RawMessage `json:"image_url"`
+ Detail string `json:"detail"` // a Responses input_image's
}
if len(raw) == 0 || raw[0] != '[' || decodeJSON(raw, &blocks) != nil {
return text, nil
@@ -776,15 +781,19 @@ func toolOutput(raw json.RawMessage) (string, []Part) {
}
case "input_image", "image_url":
var u string
+ detail := b.Detail
if decodeJSON(b.ImageURL, &u) != nil {
var o struct {
- URL string `json:"url"`
+ URL string `json:"url"`
+ Detail string `json:"detail"`
}
decodeJSON(b.ImageURL, &o)
- u = o.URL
+ u, detail = o.URL, o.Detail
}
if u != "" {
- images = append(images, imagePart(u))
+ im := imagePart(u)
+ im.Detail = detail
+ images = append(images, im)
}
}
}
diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go
index 8834be3f6..2e219558d 100644
--- a/internal/gateway/responses.go
+++ b/internal/gateway/responses.go
@@ -763,7 +763,7 @@ func orphanedToolOutputs(body []byte) []byte {
content = append(content, map[string]any{"type": "input_text", "text": text})
}
for _, image := range images {
- content = append(content, map[string]any{"type": "input_image", "detail": "auto", "image_url": dataURL(image)})
+ content = append(content, map[string]any{"type": "input_image", "detail": responsesDetail(image.Detail), "image_url": dataURL(image)})
}
if len(content) == 0 {
content = append(content, map[string]any{"type": "input_text", "text": "Tool result received."})
@@ -887,6 +887,7 @@ func responsesParts(raw json.RawMessage) []Part {
Type string `json:"type"`
Text string `json:"text"`
ImageURL string `json:"image_url"`
+ Detail string `json:"detail"`
}
decodeJSON(raw, &items)
var out []Part
@@ -896,7 +897,9 @@ func responsesParts(raw json.RawMessage) []Part {
out = append(out, Part{Kind: Text, Text: it.Text})
case "input_image":
if it.ImageURL != "" {
- out = append(out, imagePart(it.ImageURL))
+ im := imagePart(it.ImageURL)
+ im.Detail = it.Detail
+ out = append(out, im)
}
}
}
@@ -1160,7 +1163,7 @@ func buildResponses(r *Request, model, host string, rejectTemp bool) []byte {
content = append(content, map[string]any{"type": t, "text": attachmentText(p)})
case Image:
if m.Role != "assistant" {
- content = append(content, map[string]any{"type": "input_image", "detail": "auto", "image_url": dataURL(p)})
+ content = append(content, map[string]any{"type": "input_image", "detail": responsesDetail(p.Detail), "image_url": dataURL(p)})
}
case Thinking:
if !replay && sealedReasoning(p) {
@@ -1197,7 +1200,7 @@ func buildResponses(r *Request, model, host string, rejectTemp bool) []byte {
items = append(items, map[string]any{"type": "input_text", "text": p.Text})
}
for _, im := range p.Images {
- items = append(items, map[string]any{"type": "input_image", "detail": "auto", "image_url": dataURL(im)})
+ items = append(items, map[string]any{"type": "input_image", "detail": responsesDetail(im.Detail), "image_url": dataURL(im)})
}
output = items
}