missing field content HTTP 400 - Codex
Quick fix
将 CC Switch 转换层中 content:null 替换为空字符串即可解决严格上游拒绝请求的问题。
Symptom
Section titled “Symptom”messages[1]: missing field `content` at line 1 column 34911当使用 cc-switch (v3.19.x - v3.20.0) 的 Responses→Chat 转换路径(apiFormat=openai_chat)转发给对 JSON Schema 解析严格的第三方上游(如 Agnes、云舟公益等)时,Codex Desktop 发起的标题生成子请求或多轮工具调用场景会触发 HTTP 400 错误。
根本原因在于 cc-switch 内部的转换逻辑存在缺陷: 1. 在 `responses_message_item_to_chat_message` 中,当输入消息项缺少 content 字段时,代码执行 `.unwrap_or(Value::Null)`,导致输出 JSON 中出现 `"content": null`。 2. 在 `collapse_system_messages_to_head` 中,对于 role 为 system 但 content 不是字符串(null 或数组)的消息,合并逻辑未命中,导致该消息原样保留并透传。 3. 在 `flush_pending_tool_calls` 中,固定产出包含 `"content": null` 的 assistant 消息。
虽然 OpenAI 规范允许 tool_calls 场景下 content 为 null,但许多非 OpenAI 上游(如基于 serde 的网关)在反序列化时会因遇到 null 值而直接报错 "missing field content" 或 "invalid content"。
修改 cc-switch 源码中的转换逻辑,将所有产生 content: null 的地方替换为空字符串。具体涉及三个位置: 1. `responses_message_item_to_chat_message`: 将 `unwrap_or(Value::Null)` 改为 `unwrap_or(Value::String(String::new()))`。 2. `collapse_system_messages_to_head`: 确保非字符串 content 的 system 消息被正确处理或丢弃,避免透传 null。 3. `flush_pending_tool_calls`: 将固定的 content: null 改为空字符串。
// src-tauri/src/proxy/providers/transform_codex_chat.rs// 修复点 1: responses_message_item_to_chat_messagelet content = item.get("content").map(|value| responses_content_to_chat_content(chat_role, value)).unwrap_or(Value::Null);let content = item.get("content").map(|value| responses_content_to_chat_content(chat_role, value)).unwrap_or(Value::String(String::new()));// 修复点 2 & 3: 确保 flush_pending_tool_calls 和 collapse_system_messages_to_head 不输出 null content如果你无法立即修改源码,可以使用本地反向代理(如 Nginx 或简单的 Node.js/Python 脚本)拦截 cc-switch 发出的出站请求,将 JSON body 中所有 `"content": null` 替换为 `"content": ""`,然后转发给上游。这可以作为一种临时缓解方案。
Affected Versions
Section titled “Affected Versions”Source Issues
Section titled “Source Issues”本页汇总自 2 个真实 issue
- 为什么 Claude Code 没有这个问题?
- Claude Code 通常使用 Anthropic 的原生 API 格式或通过不同的转换路径,其请求体结构对 null content 的处理更为宽容,或者其内部消息构建逻辑避免了产生空的 system message。此问题主要影响使用 Codex 且经过 Responses→Chat 转换的场景。
- 我的上游是 OpenAI 官方 API,也会报这个错吗?
- 不会。OpenAI 官方 API 对 `content: null` 有明确的兼容性处理(特别是在 tool_calls 场景中),因此只有那些对 JSON 反序列化非常严格、不支持 null 值的第三方网关或自建上游才会触发此错误。
- 除了改源码,还有其他办法吗?
- 是的,你可以使用本地反向代理作为中间件,自动将请求体中的 `content: null` 替换为 `content: ""`。这在 Issue #6033 中被提及为一种有效的临时缓解措施。
这是一个非官方社区 wiki,与 cc-switch 作者及项目本身无隶属关系。内容整理自项目公开的 GitHub issues。本站不分发任何软件。