proxy: preserve string content during image fallback (#18002)

This commit is contained in:
Parth Sareen
2026-08-25 15:03:57 -07:00
committed by GitHub
parent 075aa7e147
commit ebf200f952
2 changed files with 21 additions and 0 deletions
+4
View File
@@ -900,6 +900,10 @@ func replaceUnsupportedImages(payload map[string]json.RawMessage) (bool, error)
func replaceImagesInContent(content json.RawMessage) (json.RawMessage, bool, error) {
var blocks []json.RawMessage
if err := json.Unmarshal(content, &blocks); err != nil {
var text string
if json.Unmarshal(content, &text) == nil {
return content, false, nil
}
return content, false, fmt.Errorf("decode Claude content for image fallback: %w", err)
}
+17
View File
@@ -1319,6 +1319,16 @@ func TestGatewayLazilyRetriesUnsupportedVision(t *testing.T) {
body: `{"model":"glm-5.2:cloud","messages":[{"role":"user","content":[{"type":"image","source":{"type":"base64","media_type":"image/png","data":"aW1hZ2U="}},{"type":"text","text":"what dis"}]},{"role":"assistant","content":[{"type":"text","text":"I cannot inspect it"}]},{"role":"user","content":[{"type":"text","text":"kk"}]}]}`,
preserve: "kk",
},
{
name: "string message history",
body: `{"model":"glm-5.2:cloud","messages":[{"role":"user","content":"plain history"},{"role":"user","content":[{"type":"image","source":{"type":"base64","media_type":"image/png","data":"aW1hZ2U="}},{"type":"text","text":"what dis"}]}]}`,
preserve: "plain history",
},
{
name: "string tool result history",
body: `{"model":"glm-5.2:cloud","messages":[{"role":"user","content":[{"type":"tool_result","tool_use_id":"tool_1","content":"plain tool output"}]},{"role":"user","content":[{"type":"image","source":{"type":"base64","media_type":"image/png","data":"aW1hZ2U="}},{"type":"text","text":"what dis"}]}]}`,
preserve: "plain tool output",
},
} {
t.Run(test.name, func(t *testing.T) {
var upstreamBodies [][]byte
@@ -1363,6 +1373,13 @@ func TestGatewayLazilyRetriesUnsupportedVision(t *testing.T) {
}
}
func TestReplaceImagesInContentRejectsUnsupportedShape(t *testing.T) {
_, _, err := replaceImagesInContent(json.RawMessage(`{"type":"text","text":"not a content array"}`))
if err == nil {
t.Fatal("expected unsupported content shape to fail")
}
}
func TestGatewayDoesNotSanitizeImagesOnSuccessfulRequest(t *testing.T) {
var upstreamBodies [][]byte
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {