sub2api icon indicating copy to clipboard operation
sub2api copied to clipboard

Responses API: metadata 字段透传导致上游 502

Open CPU-JIA opened this issue 8 months ago • 1 comments

问题描述

通过 /v1/responses 端点发送包含 metadata 字段的请求时,sub2api 将该字段原样透传给上游(ChatGPT 内部 codex API),导致上游拒绝请求,最终返回 502 "Upstream request failed"。

复现步骤

curl -X POST https://your-sub2api/v1/responses \
  -H "Authorization: Bearer sk-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "stream": true,
    "metadata": {},
    "input": [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "hi"}]}]
  }'

返回:

{"error":{"message":"Upstream request failed","type":"upstream_error"}}

去掉 "metadata": {} 后同样的请求正常返回 200。

原因分析

  1. metadata 是 OpenAI Responses API 的合法字段(用于请求标记/追踪)
  2. sub2api 的非 passthrough 路径有字段剥离逻辑(openai_codex_transform.go 和 openai_gateway_service.go),但 metadata 不在剥离名单中
  3. ChatGPT 内部 codex API (chatgpt.com/backend-api/codex/responses) 比公开 API schema 更严格,不接受 metadata 字段
  4. 上游报错后 failover 重试耗尽,返回统一 502

触发场景

  • Claude Code CLI 通过 Anthropic API 代理转发时,每个请求都携带 "metadata": {"user_id": "..."}
  • 任何使用 OpenAI SDK 且设置了 metadata 的客户端

建议修复

在非 passthrough 路径的字段剥离逻辑中添加 metadata:

方案 A:openai_gateway_service.go 约 1674 行,unsupportedFields 切片中添加:

unsupportedFields := []string{"prompt_cache_retention", "safety_identifier", "metadata"}

方案 B:openai_codex_transform.go 的 applyCodexOAuthTransform 函数中添加到剥离列表。

两处都加最保险。

环境

  • 请求格式:OpenAI Responses API (/v1/responses)
  • 即使 metadata 为空对象 {} 也会触发 502

CPU-JIA avatar Mar 06 '26 11:03 CPU-JIA

修复 24d19a5f 只覆盖 OAuth,APIKey + messages dispatch(/v1/messages→/v1/responses)路径也受影响,max_output_tokens 在 anthropic_to_responses.go 写入后无剥离

Thetaio-Technology avatar Jul 21 '26 18:07 Thetaio-Technology