diff --git a/internal/translator/codex/openai/responses/codex_openai-responses_request.go b/internal/translator/codex/openai/responses/codex_openai-responses_request.go
index aee02e15a7..a1ad3fa732 100644
--- a/internal/translator/codex/openai/responses/codex_openai-responses_request.go
+++ b/internal/translator/codex/openai/responses/codex_openai-responses_request.go
@@ -74,6 +74,7 @@ func ConvertOpenAIResponsesRequestToCodex(modelName string, inputRawJSON []byte,
inputResult = convertSystemRoleToDeveloperDoc(inputResult, ensureDoc)
normalizeCodexBuiltinToolsDoc(rawJSON, ensureDoc)
normalizeEmptyFunctionCallArgumentsDoc(inputResult, ensureDoc)
+ translatorcommon.SanitizeOrphanResponsesToolOutputsInputDoc(inputResult, ensureDoc)
if doc != nil {
return doc.Bytes(), nil
diff --git a/internal/translator/codex/openai/responses/codex_openai-responses_request_test.go b/internal/translator/codex/openai/responses/codex_openai-responses_request_test.go
index bede0ae168..2769fa913d 100644
--- a/internal/translator/codex/openai/responses/codex_openai-responses_request_test.go
+++ b/internal/translator/codex/openai/responses/codex_openai-responses_request_test.go
@@ -1098,3 +1098,86 @@ func TestConvertOpenAIResponsesRequestToCodexParallelToolCalls(t *testing.T) {
}
})
}
+
+func TestConvertOpenAIResponsesRequestToCodexSanitizesOrphanToolOutputs(t *testing.T) {
+ raw := []byte(`{
+ "model":"gpt-5.4",
+ "input":[
+ {"type":"message","role":"user","content":[{"type":"input_text","text":"hello"}]},
+ {"type":"function_call_output","id":"fco_1","name":"codex_apps_open_page","namespace":"agent","output":"{\"page_id\":null}"},
+ {"type":"custom_tool_call_output","id":"fco_2","name":"custom_ctx","output":[{"type":"input_text","text":"part one"},{"type":"input_text","text":"part two"},{"type":"input_image","image_url":"https://example.com/page.png"}]},
+ {"type":"function_call","call_id":"call_ok","name":"shell","arguments":""},
+ {"type":"function_call_output","call_id":"call_ok","output":"ok"},
+ {"type":"function_call_output","call_id":" ","output":"blank id"},
+ {"type":"function_call_output","id":"fco_3","name":"create_thread","namespace":"codex_app","output":"task"},
+ {"type":"function_call","call_id":"call_pair","name":"shell","arguments":"{}"},
+ {"type":"function_call_output","output":"paired without call_id"}
+ ]
+ }`)
+
+ out, errConvert := ConvertOpenAIResponsesRequestToCodex("gpt-5.4", raw, false)
+ if errConvert != nil {
+ t.Fatalf("ConvertOpenAIResponsesRequestToCodex returned error: %v", errConvert)
+ }
+ input := ojson.GetBytesNoCopy(out, "input").Array()
+ if len(input) != 9 {
+ t.Fatalf("input len = %d, want 9", len(input))
+ }
+
+ if got := input[1].Get("type").String(); got != "message" {
+ t.Fatalf("input[1].type = %q, want %q", got, "message")
+ }
+ if got := input[1].Get("role").String(); got != "user" {
+ t.Fatalf("input[1].role = %q, want %q", got, "user")
+ }
+ want1 := "Tool output from codex_apps_open_page:\n{\"page_id\":null}"
+ if got := input[1].Get("content.0.text").String(); got != want1 {
+ t.Fatalf("input[1].content.0.text = %q, want %q", got, want1)
+ }
+
+ if got := input[2].Get("type").String(); got != "message" {
+ t.Fatalf("input[2].type = %q, want %q", got, "message")
+ }
+ want2 := "Tool output from custom_ctx:\npart one\npart two"
+ if got := input[2].Get("content.0.text").String(); got != want2 {
+ t.Fatalf("input[2].content.0.text = %q, want %q", got, want2)
+ }
+ if got := input[2].Get("content.1.type").String(); got != "input_image" {
+ t.Fatalf("input[2].content.1.type = %q, want %q", got, "input_image")
+ }
+ if got := input[2].Get("content.1.image_url").String(); got != "https://example.com/page.png" {
+ t.Fatalf("input[2].content.1.image_url = %q, want %q", got, "https://example.com/page.png")
+ }
+
+ if got := input[3].Get("arguments").String(); got != "{}" {
+ t.Fatalf("input[3].arguments = %q, want %q", got, "{}")
+ }
+ if got := input[4].Get("type").String(); got != "function_call_output" {
+ t.Fatalf("input[4].type = %q, want %q", got, "function_call_output")
+ }
+ if got := input[4].Get("call_id").String(); got != "call_ok" {
+ t.Fatalf("input[4].call_id = %q, want %q", got, "call_ok")
+ }
+
+ if got := input[5].Get("type").String(); got != "message" {
+ t.Fatalf("input[5].type = %q, want %q", got, "message")
+ }
+ want5 := "Tool output from tool:\nblank id"
+ if got := input[5].Get("content.0.text").String(); got != want5 {
+ t.Fatalf("input[5].content.0.text = %q, want %q", got, want5)
+ }
+
+ if got := input[6].Get("type").String(); got != "function_call_output" {
+ t.Fatalf("input[6].type = %q, want %q", got, "function_call_output")
+ }
+ if got := input[6].Get("namespace").String(); got != "codex_app" {
+ t.Fatalf("input[6].namespace = %q, want %q", got, "codex_app")
+ }
+
+ if got := input[8].Get("type").String(); got != "function_call_output" {
+ t.Fatalf("input[8].type = %q, want %q", got, "function_call_output")
+ }
+ if got := input[8].Get("call_id").String(); got != "call_pair" {
+ t.Fatalf("input[8].call_id = %q, want %q", got, "call_pair")
+ }
+}
diff --git a/internal/translator/common/responses.go b/internal/translator/common/responses.go
index 51e5e4cd2a..434d06b41b 100644
--- a/internal/translator/common/responses.go
+++ b/internal/translator/common/responses.go
@@ -1,6 +1,7 @@
package common
import (
+ "fmt"
"strings"
"github.com/MoeForge/moego/ojson"
@@ -131,7 +132,7 @@ func NormalizeResponsesToolCallOutputs(items []ojson.Result) []ojson.Result {
expectedName := pendingCallNames[pendingID]
if expectedName != "" {
for outIdx, out := range outputs {
- if !used[outIdx] && ExtractResponsesCallID(out) == "" {
+ if !used[outIdx] && ExtractResponsesCallID(out) == "" && !isCodexDelegationToolOutput(out) {
outName := strings.TrimSpace(out.Get("name").String())
if outName != "" && outName == expectedName {
used[outIdx] = true
@@ -150,7 +151,7 @@ func NormalizeResponsesToolCallOutputs(items []ojson.Result) []ojson.Result {
continue
}
for outIdx, out := range outputs {
- if !used[outIdx] && ExtractResponsesCallID(out) == "" {
+ if !used[outIdx] && ExtractResponsesCallID(out) == "" && !isCodexDelegationToolOutput(out) {
outName := strings.TrimSpace(out.Get("name").String())
expectedName := pendingCallNames[pendingID]
if outName == "" || expectedName == "" || outName == expectedName {
@@ -187,3 +188,189 @@ func NormalizeResponsesToolCallOutputs(items []ojson.Result) []ojson.Result {
return normalized
}
+
+// SanitizeOrphanResponsesToolOutputsDoc rewrites standalone function_call_output
+// and custom_tool_call_output items that carry no call_id into user message
+// items. Codex Desktop Companion Page context is injected as function_call_output
+// items without call_id, which strict Responses upstreams reject with HTTP 400
+// ("responses function_call_output call_id is required").
+func SanitizeOrphanResponsesToolOutputsDoc(doc *ojson.Doc) bool {
+ if doc == nil {
+ return false
+ }
+ return SanitizeOrphanResponsesToolOutputsInputDoc(doc.Get("input"), func() *ojson.Doc {
+ return doc
+ })
+}
+
+// SanitizeOrphanResponsesToolOutputsInputDoc pairs tool outputs missing call_id
+// with preceding unmatched tool calls when possible, and rewrites remaining
+// standalone function_call_output and custom_tool_call_output items without
+// call_id into user message items in-place on the document returned by ensureDoc.
+func SanitizeOrphanResponsesToolOutputsInputDoc(inputResult ojson.Result, ensureDoc func() *ojson.Doc) bool {
+ if !inputResult.IsArray() || ensureDoc == nil {
+ return false
+ }
+ inputItems := inputResult.Array()
+ if len(inputItems) == 0 {
+ return false
+ }
+
+ normalizedItems := NormalizeResponsesToolCallOutputs(inputItems)
+
+ changed := false
+ pendingMissingCallIDOutputType := ""
+ for i := 0; i < len(inputItems); i++ {
+ item := normalizedItems[i]
+ if !item.IsObject() {
+ pendingMissingCallIDOutputType = ""
+ continue
+ }
+ itemType := strings.TrimSpace(item.Get("type").String())
+ if itemType == "function_call" || itemType == "custom_tool_call" {
+ if ExtractResponsesCallID(item) == "" {
+ pendingMissingCallIDOutputType = itemType + "_output"
+ } else {
+ pendingMissingCallIDOutputType = ""
+ }
+ continue
+ }
+ if itemType != "function_call_output" && itemType != "custom_tool_call_output" {
+ pendingMissingCallIDOutputType = ""
+ continue
+ }
+ if isCodexDelegationToolOutput(item) {
+ pendingMissingCallIDOutputType = ""
+ continue
+ }
+ if resolvedCallID := responsesToolOutputExplicitCallID(item); resolvedCallID != "" {
+ pendingMissingCallIDOutputType = ""
+ origCallID := strings.TrimSpace(inputItems[i].Get("call_id").String())
+ if origCallID != resolvedCallID {
+ itemPath := fmt.Sprintf("input.%d.call_id", i)
+ if errSet := ensureDoc().Set(itemPath, resolvedCallID); errSet == nil {
+ changed = true
+ }
+ }
+ continue
+ }
+ if pendingMissingCallIDOutputType == itemType &&
+ strings.TrimSpace(item.Get("name").String()) == "" &&
+ strings.TrimSpace(item.Get("id").String()) == "" {
+ pendingMissingCallIDOutputType = ""
+ continue
+ }
+ pendingMissingCallIDOutputType = ""
+
+ itemPath := fmt.Sprintf("input.%d", i)
+ if setOrphanResponsesToolOutputMessageDoc(ensureDoc(), itemPath, item) {
+ changed = true
+ }
+ }
+ return changed
+}
+
+func responsesToolOutputExplicitCallID(item ojson.Result) string {
+ for _, key := range []string{"call_id", "tool_call_id", "callId"} {
+ val := item.Get(key)
+ if val.Type == ojson.String {
+ if s := strings.TrimSpace(val.String()); s != "" {
+ return s
+ }
+ }
+ }
+ return ""
+}
+
+func isCodexDelegationToolOutput(item ojson.Result) bool {
+ if strings.TrimSpace(item.Get("type").String()) != "function_call_output" {
+ return false
+ }
+ if strings.TrimSpace(item.Get("namespace").String()) != "codex_app" {
+ return false
+ }
+ switch strings.TrimSpace(item.Get("name").String()) {
+ case "create_thread", "send_message_to_thread":
+ return true
+ default:
+ return false
+ }
+}
+
+func setOrphanResponsesToolOutputMessageDoc(targetDoc *ojson.Doc, itemPath string, item ojson.Result) bool {
+ if targetDoc == nil {
+ return false
+ }
+ fullText, extraParts := buildOrphanResponsesToolOutputContent(item)
+ if errSet := targetDoc.SetRawBytes(itemPath, []byte(`{"type":"message","role":"user","content":[{"type":"input_text","text":""}]}`)); errSet != nil {
+ return false
+ }
+ if errSet := targetDoc.Set(itemPath+".content.0.text", fullText); errSet != nil {
+ return false
+ }
+ for idx, partRaw := range extraParts {
+ partPath := fmt.Sprintf("%s.content.%d", itemPath, idx+1)
+ if errSet := targetDoc.SetRawBytes(partPath, partRaw); errSet != nil {
+ return false
+ }
+ }
+ return true
+}
+
+func buildOrphanResponsesToolOutputContent(item ojson.Result) (string, [][]byte) {
+ name := strings.TrimSpace(item.Get("name").String())
+ if name == "" {
+ name = "tool"
+ }
+ outputText, extraParts := extractOrphanResponsesToolOutputParts(item.Get("output"))
+ if outputText == "" {
+ outputText = "(empty)"
+ }
+ return "Tool output from " + name + ":\n" + outputText, extraParts
+}
+
+func extractOrphanResponsesToolOutputParts(output ojson.Result) (string, [][]byte) {
+ if !output.Exists() || output.Type == ojson.Null {
+ return "", nil
+ }
+ if output.Type == ojson.String {
+ return strings.TrimSpace(output.String()), nil
+ }
+ if output.IsArray() {
+ items := output.Array()
+ parts := make([]string, 0, len(items))
+ var extraParts [][]byte
+ for i := 0; i < len(items); i++ {
+ part := items[i]
+ if part.Type == ojson.String {
+ if s := strings.TrimSpace(part.String()); s != "" {
+ parts = append(parts, s)
+ }
+ continue
+ }
+ if !part.IsObject() {
+ continue
+ }
+ matchedText := false
+ for _, key := range []string{"text", "input_text", "output_text"} {
+ val := part.Get(key)
+ if val.Type == ojson.String {
+ matchedText = true
+ if s := strings.TrimSpace(val.String()); s != "" {
+ parts = append(parts, s)
+ }
+ break
+ }
+ }
+ if !matchedText {
+ if raw := strings.TrimSpace(part.Raw); raw != "" {
+ extraParts = append(extraParts, []byte(raw))
+ }
+ }
+ }
+ if len(parts) > 0 || len(extraParts) > 0 {
+ return strings.Join(parts, "\n"), extraParts
+ }
+ }
+ return strings.TrimSpace(output.Raw), nil
+}
diff --git a/sdk/api/handlers/openai/openai_responses_handlers.go b/sdk/api/handlers/openai/openai_responses_handlers.go
index 6eb68e8916..0dd4aea02a 100644
--- a/sdk/api/handlers/openai/openai_responses_handlers.go
+++ b/sdk/api/handlers/openai/openai_responses_handlers.go
@@ -25,6 +25,7 @@ import (
"github.com/router-for-me/CLIProxyAPI/v8/internal/httpwire"
"github.com/router-for-me/CLIProxyAPI/v8/internal/interfaces"
"github.com/router-for-me/CLIProxyAPI/v8/internal/registry"
+ translatorcommon "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common"
sdkaccess "github.com/router-for-me/CLIProxyAPI/v8/sdk/access"
"github.com/router-for-me/CLIProxyAPI/v8/sdk/api/handlers"
"github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/usage"
@@ -935,7 +936,8 @@ func (h *OpenAIResponsesAPIHandler) prepareCodexRequestPayload(c *gin.Context, p
doc := ojson.NewDoc(payload)
toolsChanged := h.prepareCodexMultiAgentV2ToolsDoc(c, doc)
orphanChanged := h.prepareCodexOrphanDelegationDoc(c, doc)
- if len(payload) == 0 || (!toolsChanged && !orphanChanged) {
+ toolOutputChanged := translatorcommon.SanitizeOrphanResponsesToolOutputsDoc(doc)
+ if len(payload) == 0 || (!toolsChanged && !orphanChanged && !toolOutputChanged) {
return payload
}
return doc.Bytes()
@@ -986,6 +988,7 @@ func (h *OpenAIResponsesAPIHandler) Compact(c *gin.Context) {
doc := ojson.NewDoc(rawJSON)
orphanChanged := h.prepareCodexOrphanDelegationDoc(c, doc)
+ toolOutputChanged := translatorcommon.SanitizeOrphanResponsesToolOutputsDoc(doc)
streamResult := doc.Get("stream")
if streamResult.Type == ojson.True {
@@ -1002,7 +1005,7 @@ func (h *OpenAIResponsesAPIHandler) Compact(c *gin.Context) {
_ = doc.Delete("stream")
streamDeleted = true
}
- if orphanChanged || streamDeleted {
+ if orphanChanged || toolOutputChanged || streamDeleted {
rawJSON = doc.Bytes()
}
diff --git a/sdk/api/handlers/openai/openai_responses_multi_agent_test.go b/sdk/api/handlers/openai/openai_responses_multi_agent_test.go
index d1de0e41dd..fbdeb5a14b 100644
--- a/sdk/api/handlers/openai/openai_responses_multi_agent_test.go
+++ b/sdk/api/handlers/openai/openai_responses_multi_agent_test.go
@@ -330,3 +330,105 @@ func TestResponsesOrphanCodexDelegationCompatibility(t *testing.T) {
t.Fatalf("input.0.type = %q, want function_call_output without header; captured=%s", itemType, capturedNoHeader)
}
}
+
+func TestResponsesSanitizesOrphanToolOutputsThroughHTTPAndCompact(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ const modelID = "test-responses-orphan-tool-output-model"
+ authID := "test-responses-orphan-tool-output-auth"
+ reg := registry.GetGlobalRegistry()
+ reg.RegisterClient(authID, "codex", []*registry.ModelInfo{{ID: modelID}})
+ t.Cleanup(func() {
+ reg.UnregisterClient(authID)
+ })
+
+ coreManager := coreauth.NewManager(nil, nil, nil)
+ executor := &responsesMultiAgentCaptureExecutor{}
+ coreManager.RegisterExecutor(executor)
+ if _, errRegister := coreManager.Register(context.Background(), &coreauth.Auth{
+ ID: authID,
+ Provider: "codex",
+ Status: coreauth.StatusActive,
+ }); errRegister != nil {
+ t.Fatalf("register auth: %v", errRegister)
+ }
+
+ baseHandler := handlers.NewBaseAPIHandlers(&sdkconfig.SDKConfig{}, coreManager)
+ responsesHandler := NewOpenAIResponsesAPIHandler(baseHandler)
+
+ router := gin.New()
+ router.POST("/v1/responses", responsesHandler.Responses)
+ router.POST("/v1/responses/compact", responsesHandler.Compact)
+
+ payload := fmt.Sprintf(`{
+ "model": %q,
+ "stream": false,
+ "input": [
+ {"type":"message","role":"user","content":[{"type":"input_text","text":"hello"}]},
+ {"type":"function_call_output","id":"fco_1","name":"codex_apps_open_page","namespace":"agent","output":"{\"page_id\":null}"},
+ {"type":"custom_tool_call_output","id":"fco_2","name":"custom_ctx","output":[{"type":"input_text","text":"part one"},{"type":"input_text","text":"part two"},{"type":"input_image","image_url":"https://example.com/page.png"}]},
+ {"type":"function_call","call_id":"call_ok","name":"shell","arguments":"{}"},
+ {"type":"function_call_output","call_id":"call_ok","output":"ok"},
+ {"type":"function_call_output","call_id":" ","output":"blank id"},
+ {"type":"function_call_output","id":"fco_3","name":"create_thread","namespace":"codex_app","output":"task"},
+ {"type":"function_call","call_id":"call_pair","name":"shell","arguments":"{}"},
+ {"type":"function_call_output","output":"paired without call_id"}
+ ]
+ }`, modelID)
+
+ for idx, path := range []string{"/v1/responses", "/v1/responses/compact"} {
+ req := httptest.NewRequest(http.MethodPost, path, bytes.NewBufferString(payload))
+ rec := httptest.NewRecorder()
+ router.ServeHTTP(rec, req)
+ if rec.Code != http.StatusOK {
+ t.Fatalf("%s status = %d, want 200; body=%s", path, rec.Code, rec.Body.String())
+ }
+ payloads := executor.Payloads()
+ if len(payloads) != idx+1 {
+ t.Fatalf("%s captured payload count = %d, want %d", path, len(payloads), idx+1)
+ }
+ input := ojson.GetBytesNoCopy(payloads[idx], "input").Array()
+ if len(input) != 9 {
+ t.Fatalf("%s input len = %d, want 9", path, len(input))
+ }
+ if got := input[1].Get("type").String(); got != "message" {
+ t.Fatalf("%s input[1].type = %q, want message", path, got)
+ }
+ want1 := "Tool output from codex_apps_open_page:\n{\"page_id\":null}"
+ if got := input[1].Get("content.0.text").String(); got != want1 {
+ t.Fatalf("%s input[1].content.0.text = %q, want %q", path, got, want1)
+ }
+ if got := input[2].Get("type").String(); got != "message" {
+ t.Fatalf("%s input[2].type = %q, want message", path, got)
+ }
+ want2 := "Tool output from custom_ctx:\npart one\npart two"
+ if got := input[2].Get("content.0.text").String(); got != want2 {
+ t.Fatalf("%s input[2].content.0.text = %q, want %q", path, got, want2)
+ }
+ if got := input[2].Get("content.1.type").String(); got != "input_image" {
+ t.Fatalf("%s input[2].content.1.type = %q, want input_image", path, got)
+ }
+ if got := input[2].Get("content.1.image_url").String(); got != "https://example.com/page.png" {
+ t.Fatalf("%s input[2].content.1.image_url = %q, want https://example.com/page.png", path, got)
+ }
+ if got := input[4].Get("type").String(); got != "function_call_output" {
+ t.Fatalf("%s input[4].type = %q, want function_call_output", path, got)
+ }
+ if got := input[5].Get("type").String(); got != "message" {
+ t.Fatalf("%s input[5].type = %q, want message", path, got)
+ }
+ want5 := "Tool output from tool:\nblank id"
+ if got := input[5].Get("content.0.text").String(); got != want5 {
+ t.Fatalf("%s input[5].content.0.text = %q, want %q", path, got, want5)
+ }
+ if got := input[6].Get("type").String(); got != "function_call_output" {
+ t.Fatalf("%s input[6].type = %q, want function_call_output", path, got)
+ }
+ if got := input[8].Get("type").String(); got != "function_call_output" {
+ t.Fatalf("%s input[8].type = %q, want function_call_output", path, got)
+ }
+ if got := input[8].Get("call_id").String(); got != "call_pair" {
+ t.Fatalf("%s input[8].call_id = %q, want call_pair", path, got)
+ }
+ }
+}