mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 08:06:50 +00:00
fix(deepseek): lift Responses tool output images
DeepSeek native Responses rejects Codex view_image results when function_call_output.output contains input_image parts. Move media into a following user message and keep the tool output as a string so call_id pairing remains valid.
This commit is contained in:
1 parent
881f320269
commit
881ab1b0c6
4 files changed
+208
-1
No files matched your search
@@ -0,0 +1,121 @@
|
||||
package apicompat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type responsesToolOutputMedia struct {
|
||||
callID string
|
||||
imageURL string
|
||||
}
|
||||
|
||||
// LiftResponsesToolOutputMedia moves image parts out of Responses tool outputs
|
||||
// and into a following user message. Native Responses endpoints such as
|
||||
// DeepSeek accept function_call_output.output as a string, but Codex view_image
|
||||
// returns an array containing input_image. Keeping the image inside the tool
|
||||
// output makes the upstream report "No tool output found for tool call ...".
|
||||
func LiftResponsesToolOutputMedia(input any) (any, bool) {
|
||||
items, ok := input.([]any)
|
||||
if !ok {
|
||||
return input, false
|
||||
}
|
||||
|
||||
rewritten := make([]any, 0, len(items)+1)
|
||||
pending := make([]responsesToolOutputMedia, 0)
|
||||
changed := false
|
||||
|
||||
flushMedia := func() {
|
||||
if len(pending) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
content := make([]map[string]any, 0, len(pending)*2)
|
||||
lastCallID := ""
|
||||
for _, media := range pending {
|
||||
if media.callID != lastCallID {
|
||||
text := "Tool output media"
|
||||
if media.callID != "" {
|
||||
text = fmt.Sprintf(toolOutputMediaAttribution, media.callID)
|
||||
}
|
||||
content = append(content, map[string]any{
|
||||
"type": "input_text",
|
||||
"text": text,
|
||||
})
|
||||
lastCallID = media.callID
|
||||
}
|
||||
content = append(content, map[string]any{
|
||||
"type": "input_image",
|
||||
"image_url": media.imageURL,
|
||||
})
|
||||
}
|
||||
|
||||
rewritten = append(rewritten, map[string]any{
|
||||
"type": "message",
|
||||
"role": "user",
|
||||
"content": content,
|
||||
})
|
||||
pending = pending[:0]
|
||||
}
|
||||
|
||||
for _, rawItem := range items {
|
||||
item, ok := rawItem.(map[string]any)
|
||||
if !ok || !isResponsesToolOutputItem(item) {
|
||||
flushMedia()
|
||||
rewritten = append(rewritten, rawItem)
|
||||
continue
|
||||
}
|
||||
|
||||
output, exists := item["output"]
|
||||
if !exists {
|
||||
rewritten = append(rewritten, item)
|
||||
continue
|
||||
}
|
||||
outputRaw, err := json.Marshal(output)
|
||||
if err != nil {
|
||||
rewritten = append(rewritten, item)
|
||||
continue
|
||||
}
|
||||
|
||||
outputText, media, didRewrite := extractToolOutputMedia(outputRaw)
|
||||
if !didRewrite {
|
||||
rewritten = append(rewritten, item)
|
||||
continue
|
||||
}
|
||||
|
||||
item["output"] = outputText
|
||||
callID := strings.TrimSpace(stringValue(item["call_id"]))
|
||||
for _, part := range media {
|
||||
if part.ImageURL == nil {
|
||||
continue
|
||||
}
|
||||
imageURL := strings.TrimSpace(part.ImageURL.URL)
|
||||
if imageURL == "" {
|
||||
continue
|
||||
}
|
||||
pending = append(pending, responsesToolOutputMedia{
|
||||
callID: callID,
|
||||
imageURL: imageURL,
|
||||
})
|
||||
}
|
||||
changed = true
|
||||
rewritten = append(rewritten, item)
|
||||
}
|
||||
|
||||
flushMedia()
|
||||
if !changed {
|
||||
return input, false
|
||||
}
|
||||
return rewritten, true
|
||||
}
|
||||
|
||||
func isResponsesToolOutputItem(item map[string]any) bool {
|
||||
switch strings.TrimSpace(stringValue(item["type"])) {
|
||||
case "function_call_output", "custom_tool_call_output",
|
||||
"tool_search_output", "tool_search_call_output", "mcp_tool_call_output":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package apicompat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLiftResponsesToolOutputMedia(t *testing.T) {
|
||||
var input any
|
||||
require.NoError(t, json.Unmarshal([]byte(`[
|
||||
{"type":"function_call","call_id":"call_image","name":"view_image","arguments":"{}"},
|
||||
{"type":"function_call_output","call_id":"call_image","output":[{"type":"input_image","image_url":"data:image/png;base64,AQID"}]}
|
||||
]`), &input))
|
||||
|
||||
lifted, changed := LiftResponsesToolOutputMedia(input)
|
||||
require.True(t, changed)
|
||||
|
||||
items, ok := lifted.([]any)
|
||||
require.True(t, ok)
|
||||
require.Len(t, items, 3)
|
||||
|
||||
outputItem, ok := items[1].(map[string]any)
|
||||
require.True(t, ok)
|
||||
outputText, ok := outputItem["output"].(string)
|
||||
require.True(t, ok)
|
||||
require.Contains(t, outputText, toolOutputMediaMarker)
|
||||
require.NotContains(t, outputText, "data:image/png")
|
||||
|
||||
mediaMessage, ok := items[2].(map[string]any)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "message", mediaMessage["type"])
|
||||
require.Equal(t, "user", mediaMessage["role"])
|
||||
|
||||
parts, ok := mediaMessage["content"].([]map[string]any)
|
||||
require.True(t, ok)
|
||||
require.Len(t, parts, 2)
|
||||
require.Equal(t, "input_text", parts[0]["type"])
|
||||
require.Equal(t, "[Tool output media for call call_image]", parts[0]["text"])
|
||||
require.Equal(t, "input_image", parts[1]["type"])
|
||||
require.Equal(t, "data:image/png;base64,AQID", parts[1]["image_url"])
|
||||
}
|
||||
|
||||
func TestLiftResponsesToolOutputMediaLeavesPlainOutputUntouched(t *testing.T) {
|
||||
input := []any{
|
||||
map[string]any{
|
||||
"type": "function_call_output",
|
||||
"call_id": "call_text",
|
||||
"output": "plain output",
|
||||
},
|
||||
}
|
||||
|
||||
lifted, changed := LiftResponsesToolOutputMedia(input)
|
||||
require.False(t, changed)
|
||||
require.Equal(t, input, lifted)
|
||||
}
|
||||
@@ -834,6 +834,16 @@ func TestNormalizeDeepSeekResponsesRequestBody(t *testing.T) {
|
||||
require.False(t, gjson.GetBytes(adaptiveNormalized, "store").Bool())
|
||||
require.False(t, gjson.GetBytes(adaptiveNormalized, "previous_response_id").Exists())
|
||||
|
||||
mediaBody := []byte(`{"model":"deepseek-v4.1-flash","store":true,"input":[{"type":"function_call","call_id":"call_image","name":"view_image","arguments":"{}"},{"type":"function_call_output","call_id":"call_image","output":[{"type":"input_image","image_url":"data:image/png;base64,AQID"}]}]}`)
|
||||
mediaNormalized := normalizeDeepSeekResponsesRequestBody(deepseekResponses, mediaBody)
|
||||
require.False(t, gjson.GetBytes(mediaNormalized, "store").Bool())
|
||||
require.Equal(t, gjson.String, gjson.GetBytes(mediaNormalized, "input.1.output").Type)
|
||||
require.NotContains(t, gjson.GetBytes(mediaNormalized, "input.1.output").String(), "data:image/png")
|
||||
require.Equal(t, "message", gjson.GetBytes(mediaNormalized, "input.2.type").String())
|
||||
require.Equal(t, "user", gjson.GetBytes(mediaNormalized, "input.2.role").String())
|
||||
require.Equal(t, "input_image", gjson.GetBytes(mediaNormalized, "input.2.content.1.type").String())
|
||||
require.Equal(t, "data:image/png;base64,AQID", gjson.GetBytes(mediaNormalized, "input.2.content.1.image_url").String())
|
||||
|
||||
// 非 responses 协议(deepseek CC 账号)原样返回
|
||||
deepseekCC := &Account{Platform: PlatformDeepseek, Type: AccountTypeAPIKey}
|
||||
require.Equal(t, string(body), string(normalizeDeepSeekResponsesRequestBody(deepseekCC, body)))
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/apicompat"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/ctxkey"
|
||||
"github.com/Wei-Shaw/sub2api/internal/util/urlvalidator"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -141,7 +142,25 @@ func normalizeDeepSeekResponsesRequestBody(account *Account, body []byte) []byte
|
||||
if stripped, err := sjson.DeleteBytes(normalized, "previous_response_id"); err == nil {
|
||||
normalized = stripped
|
||||
}
|
||||
return normalized
|
||||
|
||||
var requestBody map[string]any
|
||||
if err := decodeOpenAIJSONUseNumber(normalized, &requestBody); err != nil {
|
||||
return normalized
|
||||
}
|
||||
input, exists := requestBody["input"]
|
||||
if !exists {
|
||||
return normalized
|
||||
}
|
||||
liftedInput, changed := apicompat.LiftResponsesToolOutputMedia(input)
|
||||
if !changed {
|
||||
return normalized
|
||||
}
|
||||
requestBody["input"] = liftedInput
|
||||
rebuilt, err := marshalOpenAIUpstreamJSON(requestBody)
|
||||
if err != nil {
|
||||
return normalized
|
||||
}
|
||||
return rebuilt
|
||||
}
|
||||
|
||||
func trimOpenAIEncryptedReasoningItems(reqBody map[string]any) bool {
|
||||
|
||||
Reference in new issue
Block a user