new-api icon indicating copy to clipboard operation
new-api copied to clipboard

[BUG] Responses→Chat 转换器把 function_call_output 的图片内容拍平成 base64 文本,触发上游 prompt 超限

Open MOON-DREAM-STARS opened this issue 2 months ago • 3 comments

提交前必读

  • 文档:https://docs.newapi.ai/
  • 使用问题先看或先问:https://deepwiki.com/QuantumNous/new-api
  • 开启透传请勿反馈问题;透传会直接发送内容,不会经过 new-api 的逻辑处理。
  • 请以简练语言陈述事实与证据。Issue 正文及后续评论均不得直接粘贴未经人工过滤的大段 AI 文本。
  • 转发类问题必须先确认问题发生在 new-api 这一层:请使用等价的脱敏请求分别直连上游和经过 new-api,对比响应状态、错误体与服务端日志。未经对比、仅展示上游报错的反馈不接受 issue。
  • 不接受 Coding Plan、逆向渠道、第三方封装接口,以及将 Codex 接口反代为通用 API 后产生的兼容性问题;Codex API 自身特有的协议或行为也不应被当作标准 OpenAI API 行为,相关问题请先向渠道或接口提供方确认。
  • 本项目不提供官方托管服务,也不为第三方托管站、中转站或 API 服务提供技术支持;此类问题请联系对应运营方。
  • 警告:删除本模板、删除小节标题或随意清空内容的 issue,可能会被直接关闭;多次提交未经人工过滤的大段 AI 文本,或重复恶意提交者,可能会被 block。

部署来源

修改过的 fork(须先在本仓库原版复现)——已在未修改的本仓库 main 上复现,见「复现步骤」。

您当前的 newapi 版本

  • 部署为个人 fork 构建:镜像 OCI revision 9dd312a66a8eecdc0b2a9ac70ae9ff5b05b46f52,运行镜像 ghcr.io/moon-dream-stars/new-api@sha256:3494fcf271336bbe9c780a8b799a997d9b5dcb3f1a6d68a376337eeabce95290。
  • 缺陷所在文件与上游 main 一致;确定性复现在未修改的本仓库 upstream/main @ 9c293e8c02371bda844af79e3500ff2d516d1dda 上完成(relaykit/relayconvert/internal/oai_responses/to_oai_chat_req.go blob 5ec9eb924768afaae64edaf537e083c55e280913)。

提交确认

  • [x] 非重复 issue: 我已搜索现有 Issues,确认目前没有类似 issue。
  • [x] 提交前必读: 我已完整阅读上方“提交前必读”,并已查看文档 https://docs.newapi.ai/、项目 README 且向 AI 提问,确认这不是使用、配置或接入类问题。
  • [x] 受支持版本: 我已提供准确的版本号、commit 或镜像标签(不是 latest 或 unknown),并确认问题可在本仓库未修改的受支持版本中复现。
  • [x] 非第三方服务: 我确认这不是第三方托管站、中转站、API 服务或未经本仓库原版验证的 fork 所独有的问题;第三方实例问题应联系其运营方。
  • [x] 非透传: 我确认相关渠道未开启透传。开启透传会直接发送内容,不会经过 new-api 的逻辑处理,此类问题不接受 issue。
  • [x] 问题归属: 我已提供足以区分客户端、new-api 和上游的证据;转发问题已使用等价的脱敏请求对比直连上游与经过 new-api 的结果,确认错误由 new-api 引入或改变,而非仅原样转发上游报错。
  • [x] 渠道与协议边界: 我确认问题不是由 Coding Plan、逆向渠道、第三方封装接口、Codex 反代接口,或 Codex API 自身特有行为导致;若问题最初出现于此类接口,我已在本仓库支持的标准 API 协议上复现。
  • [x] 维护成本: 我理解项目维护者精力有限。Issue 正文及后续评论均不得直接粘贴未经人工过滤的大段 AI 文本,须先提炼可复核的要点;不遵循模板要求或缺少要点、无法复核的内容可能会被无视或直接关闭。多次提交此类内容可能会被 block。

问题描述

实际现象: relaykit/relayconvert/internal/oai_responses/to_oai_chat_req.go 的 responseToolOutputToChatContent 在 function_call_output.output 为数组时,把整个数组 JSON 序列化成 tool 消息的字符串 content。OpenAI Responses 规范允许该字段是内容块数组 (InputTextContentParam | InputImageContentParamAutoParam | InputFileContentParam), 因此当工具返回图片时,图片的 base64 会被内联进 Chat 请求体的纯文本,作为文本被计数与计费。 同一文件对普通消息内容的 input_image 已正确映射为 image_url 内容块,两者口径不一致。

影响范围: 图片不再作为图像内容传给上游,且被当作文本计入上下文。实测一张 1,309,020 字节的 PNG 使单次请求 prompt_tokens 达到 1,070,945,超过上游 1,048,576 上限,上游返回 HTTP 400 code 11115(extError.code=context_length_exceeded)。同一形态的成功请求 prompt_tokens 落在 823,561–912,487 区间(8 条),按输入 token 计费,成本与上下文占用被严重放大。 任何经 Responses→Chat 转换的渠道都可能命中。

发生频率: 对「function_call_output.output 为含图片的内容块数组」这一形态 100% 确定性复现; 生产日志中连续观测到 3 次超限与 8 次高 token 计费。

判断问题位于 new-api 的依据: 直接调用转换器即可复现,全程不涉及任何上游服务、数据库或 真实模型;触发项是标准 OpenAI Responses 输入项,与具体客户端实现无关。缺陷文件与上游 main 一致。

按类型补充(转发 / API):

  • 请求端点与方法:POST /v1/responses
  • 渠道类型:Advanced Custom(type 58),路由 converter = openai_responses_to_openai_chat_completions
  • 模型:CN 域 DeepSeek 系(cn:deepseek-v4.1-flash)
  • 转换格式:OpenAI Responses → OpenAI Chat Completions
  • 是否透传:否
  • 上游原生支持依据:不适用——缺陷发生在向上游发起请求之前的转换阶段
  • 等价脱敏请求直连上游:不适用(请求尚未到达上游)
  • 同一请求经 new-api:见「复现步骤」;转换结果中 base64 出现在 tool 消息的 content 字符串里,且没有后续消息承载该图片

复现步骤

  1. 检出未修改的本仓库 main @ 9c293e8c0。
  2. 在 relaykit/relayconvert/internal/oai_responses 新增下述测试。
  3. 运行: cd relaykit && GOWORK=off go test ./relayconvert/internal/oai_responses -run TestResponsesRequestToChatCompletionsRequestHoistsToolOutputMedia -count=1
  4. 当前结果:失败。got.Messages[1](tool 消息)的 StringContent() 是包含 data:image/png;base64,… 的 JSON 字符串,断言 NotContains "base64" 失败;且不存在承载图片的 后续 user 消息。
func TestResponsesRequestToChatCompletionsRequestHoistsToolOutputMedia(t *testing.T) {
	const dataURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="

	got, err := ResponsesRequestToChatCompletionsRequest(context.Background(), &dto.OpenAIResponsesRequest{
		Model: "gpt-test",
		Input: mustRawMessage(t, []map[string]any{
			{
				"type":      "function_call",
				"call_id":   "call_1",
				"name":      "view_image",
				"arguments": "{}",
			},
			{
				"type":    "function_call_output",
				"call_id": "call_1",
				"output": []map[string]any{
					{"type": "input_text", "text": "screenshot taken"},
					{"type": "input_image", "image_url": dataURL},
				},
			},
		}),
	})
	require.NoError(t, err)
	require.Len(t, got.Messages, 3)

	assert.Equal(t, "tool", got.Messages[1].Role)
	assert.Equal(t, "screenshot taken", got.Messages[1].StringContent())
	assert.NotContains(t, got.Messages[1].StringContent(), "base64")

	assert.Equal(t, "user", got.Messages[2].Role)
	parts := got.Messages[2].ParseContent()
	require.Len(t, parts, 1)
	assert.Equal(t, dto.ContentTypeImageURL, parts[0].Type)
	require.NotNil(t, parts[0].GetImageMedia())
	assert.Equal(t, dataURL, parts[0].GetImageMedia().Url)
}

预期结果

  • tool 消息的 content 只包含文本块内容,不含任何 base64 媒体载荷;
  • 非文本内容块以对应内容块形态(input_image → image_url 等)落在紧随 tool 消息之后的 一条 user 消息中,仍可被视觉上游使用;
  • output 为字符串、对象、任意 JSON 数组、纯文本内容块数组时行为不变。

相关截图

无;复现为确定性单元测试,不依赖截图。

Agent 调查记录(按 .agents/github/ISSUE.md 保留)

Agent

  • Tool: Codex desktop app
  • Tool version: codex-cli 0.155.0-alpha.9
  • Model (full id): deepseek-v4.1-flash
  • Host: Codex desktop app (Windows)
  • Date (UTC): 2026-09-21

User request

  • Verbatim: 工具输出图片被拍平成 base64 文本,这个缺陷在哪里修?你先别着急,你看看我本地的Newapi worktree,应该有一个new-api-upstream-sync,这个worktree是专门对齐上游master的,用来生成issue和提交PR。我希望你先提交ISSUE(具体格式可以看我提交的那个issue),然后,从这个worktree中分出来一个fix/XXXX(issue)分支,专门修复这个bug。修复完成之后合并到main,并且从这个分支提交PR
  • Later constraints or corrections from the user (quote, or none): 首先按照上面说的,按「只改 Responses→Chat + 图片改放到后续 user 消息」这个方向,去走你原来那套流程(提 issue → fix/<issue#> 分支 → 修 → 合并本地 main → 从分支提 PR)

Out of scope — refuse

  • Matched: no
  • If yes, what was told to the user (stop here; do not file): 不适用

File gate — do not file unless all are satisfied

  • Out of scope (including pass-through): no
  • Usage / configuration / integration (answered instead of filing): no
  • Required facts present without invention (actual behavior, impact, frequency, evidence in new-api, applicable type fields): yes
  • Body is short and factual; no unfiltered AI-generated text: yes
  • File: yes

Kind

  • [x] Bug

Usage / configuration / integration check

  • https://docs.newapi.ai/ — 本机不可达(SSL 连接失败),未检索,不作判定依据
  • https://deepwiki.com/QuantumNous/new-api — 可达(HTTP 200),未作为判定依据
  • README / repo docs: relaykit/README.md(转换矩阵把 OpenAI Responses → OpenAI Chat 标为 Good;多模态需宿主配置 media resolver;工具损耗默认 allow 并以诊断形式返回)
  • Relevant code paths and conclusion: 见 Research / Code;缺陷在转换器自身
  • Can the current version already do this? (required for feature requests): 不适用
  • Verdict: product bug

Environment

  • new-api version / commit / image tag (not latest / unknown): OCI revision 9dd312a66a8eecdc0b2a9ac70ae9ff5b05b46f52;复现基线 upstream/main @ 9c293e8c0
  • Deploy source: 修改过的 fork,已在未修改的本仓库 main 复现
  • Database (sqlite / mysql / postgres): postgres(本缺陷不涉及数据库)

Problem facts

  • Actual behavior: function_call_output.output 为内容块数组(含 input_image data URL)时,转换器把整个数组 JSON 序列化为 tool 消息的字符串 content,图片以 base64 文本形式进入请求体
  • Impact: 图片不再作为图像内容传给上游,并被当作文本计数/计费;实测单张 1,309,020 字节 PNG 使 prompt_tokens 达 1,070,945,超过上游 1,048,576 上限并返回 400 code 11115;同形态成功请求 prompt_tokens 在 823,561–912,487 区间(8 条)
  • Frequency: 对该形态 100% 确定性复现;生产日志连续观测到 3 次超限与 8 次高 token 计费
  • Evidence that the problem is in new-api rather than the client or upstream: 直接调用 ResponsesRequestToChatCompletionsRequest 即可观察 base64 进入 tool 消息 content,不涉及上游;触发项是 OpenAI Responses 规范允许的标准输入项

Type-specific details

Relay / API

  • Request endpoint and method: POST /v1/responses
  • Channel type: Advanced Custom(type 58),converter openai_responses_to_openai_chat_completions
  • Model: cn:deepseek-v4.1-flash
  • Conversion format: OpenAI Responses → OpenAI Chat Completions
  • Pass-through enabled: 否
  • Evidence of upstream native support: 不适用(缺陷在发起上游请求之前的转换阶段;规范依据见 Research / Docs)
  • Equivalent redacted request sent directly upstream: 不适用
  • Same request through new-api: 见复现步骤与 Experiments

Billing

  • 不适用:本 issue 不主张计费公式错误;token 虚高是「二进制被文本化」的直接结果,已在 Problem facts 记录量级

Frontend

  • 不适用

Deployment / upgrade

  • 不适用:非升级引入;缺陷位于转换器对内容块数组的处理

Reproduction and expected result

  • Steps to reproduce: 见「复现步骤」
  • Expected result: 见「预期结果」
  • Related screenshots (optional): 无

Feature (feature requests only)

  • Feature description: 不适用
  • Use case: 不适用

Duplicate check

  • Search queries (issues, PRs, discussions): function_call_output、tool output base64、to_oai_chat_req、input_image、Responses 转 chat 图片、Advanced Custom responses 图片、function_call_output image
  • Closest existing threads: #6616(已关闭)、#7148(开启)、#6396(开启)
  • Why this is not a duplicate: #6616 讲的是 assistant 消息内嵌 function_call content part 与 developer 角色导致的 400,不涉及 function_call_output.output 的内容块数组;#7148 的缺陷发生在本地 token 元数据生成(GetTokenCountMeta,Claude tool_result 与 Responses compact),本 issue 是实际发出的报文形状;#6396 是 Responses→Chat 丢失 reasoning 历史。未检索到任何针对 function_call_output.output 内容块数组被字符串化、内联 base64 的报告。

Research

Docs

  • https://docs.newapi.ai/ : 本机不可达(SSL),未检索
  • https://deepwiki.com/QuantumNous/new-api : 可达,未作为判定依据
  • README / other repo docs: relaykit/README.md
  • Conclusions: 目标协议 Chat Completions 无法在 tool 消息中承载图片属于协议限制;但把二进制内联为文本并非协议要求。OpenAI OpenAPI 中 FunctionCallOutputItemParam.output 明确为 string | array(InputTextContentParam | InputImageContentParamAutoParam | InputFileContentParam),即该输入形态是标准协议的一部分。

Code

  • relaykit/relayconvert/internal/oai_responses/to_oai_chat_req.go — responsesInputItemToChatMessages 的 function_call_output 分支把 output 交给 responseToolOutputToChatContent;后者对非字符串值统一 kitutil.Marshal 成字符串
  • 同文件 responsesContentPartsToChatContent — 对普通消息内容已正确把 input_image 映射为 image_url,两者口径不一致
  • relaykit/dto/openai_request.go — Message.Content 为 any,可承载内容块数组,转换层具备表达手段
  • relay/advancedcustom/adaptor.go — 该转换器在生产路径的调用点
  • relaykit/relayconvert/internal/claude_messages/to_oai_chat_req.go — Claude tool_result 的非文本内容在 Chat 目标下同样被字符串化(同源问题,列为后续项)

Experiments

  • Command or redacted request: cd relaykit && GOWORK=off go test ./relayconvert/internal/oai_responses -run TestResponsesRequestToChatCompletionsRequestHoistsToolOutputMedia -count=1
  • Direct upstream result: 不适用(缺陷在发起上游请求之前)
  • Result through new-api: 未修改的 upstream/main 上失败——tool 消息 content 是含 data:image/png;base64,… 的 JSON 字符串,且无承载图片的后续消息
  • Conclusion: 缺陷稳定存在于未修改的 upstream/main,与客户端、数据库与真实上游响应无关

Working theory

  • What is broken or missing: function_call_output.output 为内容块数组时缺少与消息内容一致的内容块映射,整段退化为 JSON 字符串
  • Why: responseToolOutputToChatContent 只区分 string 与「其它」,其它一律序列化
  • What would falsify this: 若能构造出「输出为含图片的内容块数组、而请求体中不含 base64 文本」的转换结果,本结论即不成立

Scope

  • In scope for a later PR: 修正 Responses→Chat 对 function_call_output.output 内容块数组的处理(文本留在 tool 消息,非文本块移到紧随其后的 user 消息),并补回归测试
  • Out of scope / not this repo: Claude→Chat 的同类字符串化;客户端侧协议;上游渠道兼容性
  • Large or directional feature? 否

Proposed direction

验收标准:

  1. function_call_output.output 为内容块数组且含非文本块时,tool 消息 content 不含任何 base64 媒体载荷;
  2. 非文本块以对应内容块形态出现在紧随 tool 消息之后的 user 消息中;
  3. output 为字符串 / 对象 / 任意 JSON 数组 / 纯文本内容块数组时行为不变;
  4. 新增回归测试,既有 relaykit 测试保持通过。

Not verified

  • 未对真实上游重新发送请求(未使用付费模型调用)
  • 未验证各 OpenAI 兼容上游对「tool 消息后紧跟 user 消息」形态的接受度差异
  • 未覆盖 custom_tool_call_output 等其它 item 类型
  • 未在 macOS / Windows 复现;本缺陷不涉及数据库

Related

  • 规范依据:OpenAI OpenAPI FunctionCallOutputItemParam.output
  • #7495 / PR #7497:同一文件族的上一处修复与工作流先例
  • #7148:本地 token 估算把内联 base64 计入文本(不同代码路径)
  • #7407:Claude→Chat 的 tool_result 内嵌图片同样被序列化为 base64 文本(同一根因类别、不同转换路径,由 #7444 处理);本 issue/PR 覆盖 Responses→Chat 路径,不关闭 #7407

MOON-DREAM-STARS avatar Sep 21 '26 05:09 MOON-DREAM-STARS

[Bot Review]

感谢提交。格式检查通过,已包含版本信息、带复选框的提交确认、问题描述和复现步骤,且各部分都有具体内容。该报告与 #7407 都涉及图片内容转换为 Chat 后文本化,但本 issue 针对 Responses→Chat 的独立转换路径,因此不按重复 issue 处理。

补充收口:本问题已由 #7510(合并为 6e9de44a)修复并按 completed 关闭;我的 #7502 是同一问题的社区实现,维护者已在新 PR 中标注我为 co-author 并将其关闭。

上游实现与我原 PR 的差异:媒体块推迟到一个 assistant tool_calls 批次的全部 tool 消息之后再合并为一条 user 消息(保持 tool 消息连续),纯媒体时使用 [image] 样式占位,文本以换行连接。

感谢维护者,无后续动作。

MOON-DREAM-STARS avatar Sep 21 '26 14:09 MOON-DREAM-STARS

补充收口:本问题已由 #7510(合并为 6e9de44a)修复并按 completed 关闭;我的 #7502 是同一问题的社区实现,维护者已在新 PR 中标注我为 co-author 并将其关闭。

上游实现与我原 PR 的差异:媒体块推迟到一个 assistant tool_calls 批次的全部 tool 消息之后再合并为一条 user 消息(保持 tool 消息连续),纯媒体时使用 [image] 样式占位,文本以换行连接。

感谢维护者,无后续动作。

使用自动agent进行仓库维护,将被我们block

Calcium-Ion avatar Sep 21 '26 14:09 Calcium-Ion