[GH-ISSUE #161] claude code cli 使用 api 出现 400 错误 #57

Open
opened 2026-10-26 09:44:23 +00:00 by bsakaguchi · 13 comments
Owner

Originally created by @Nine499 on GitHub (May 27, 2026).
Original GitHub issue: https://github.com/deepseek-ai/awesome-deepseek-agent/issues/161

Image

2.1.152 (Claude Code)

  "env": {
    "ANTHROPIC_AUTH_TOKEN": "sk-0",
    "ANTHROPIC_BASE_URL": "https://api.deepseek.com/anthropic",
    "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
    "CLAUDE_CODE_ATTRIBUTION_HEADER": "0",
    "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1",
    "CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS": "1",
    "ENABLE_TOOL_SEARCH": "true"
  },
Originally created by @Nine499 on GitHub (May 27, 2026). Original GitHub issue: https://github.com/deepseek-ai/awesome-deepseek-agent/issues/161 <img width="1866" height="1309" alt="Image" src="https://github.com/user-attachments/assets/71e91072-4bab-4d6b-ad0b-a5bbc829147d" /> 2.1.152 (Claude Code) ``` "env": { "ANTHROPIC_AUTH_TOKEN": "sk-0", "ANTHROPIC_BASE_URL": "https://api.deepseek.com/anthropic", "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1", "CLAUDE_CODE_ATTRIBUTION_HEADER": "0", "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1", "CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS": "1", "ENABLE_TOOL_SEARCH": "true" }, ```
Author
Owner

@KITWealth commented on GitHub (May 27, 2026):

https://blog.csdn.net/m0_52539320/article/details/161453394

<!-- gh-comment-id:4552766150 --> @KITWealth commented on GitHub (May 27, 2026): https://blog.csdn.net/m0_52539320/article/details/161453394
Author
Owner

@z125316840-code commented on GitHub (May 27, 2026):

我也遇到了,突然就这样了

<!-- gh-comment-id:4555146555 --> @z125316840-code commented on GitHub (May 27, 2026): 我也遇到了,突然就这样了
Author
Owner

@DillionApple commented on GitHub (May 28, 2026):

是新版本 claude code 引入的问题,建议各位先降级一下。

<!-- gh-comment-id:4560418397 --> @DillionApple commented on GitHub (May 28, 2026): 是新版本 claude code 引入的问题,建议各位先降级一下。
Author
Owner

@Nine499 commented on GitHub (May 28, 2026):

是新版本 claude code 引入的问题,建议各位先降级一下。

有没有计划修复时间?

<!-- gh-comment-id:4560841026 --> @Nine499 commented on GitHub (May 28, 2026): > 是新版本 claude code 引入的问题,建议各位先降级一下。 有没有计划修复时间?
Author
Owner

@wujunran commented on GitHub (May 28, 2026):

是新版本 claude code 引入的问题,建议各位先降级一下。

降级了2.1.148还是会报一样的错

<!-- gh-comment-id:4563342320 --> @wujunran commented on GitHub (May 28, 2026): > 是新版本 claude code 引入的问题,建议各位先降级一下。 降级了2.1.148还是会报一样的错
Author
Owner

@FalseLeonLai commented on GitHub (May 29, 2026):

我也一样,claude code现在修改了请求格式导致的
API Error: 400 Failed to deserialize the JSON body into the target type: messages[1].role: unknown variant system, expected user or assistant at line 1 column 30325

<!-- gh-comment-id:4570239669 --> @FalseLeonLai commented on GitHub (May 29, 2026): **我也一样,claude code现在修改了请求格式导致的** `API Error: 400 Failed to deserialize the JSON body into the target type: messages[1].role: unknown variant system, expected user or assistant at line 1 column 30325`
Author
Owner

@huangqinquan commented on GitHub (May 29, 2026):

是新版本 claude code 引入的问题,建议各位先降级一下。

降级了2.1.148还是会报一样的错

Image

测试这个方案可以解决

<!-- gh-comment-id:4570367474 --> @huangqinquan commented on GitHub (May 29, 2026): > > 是新版本 claude code 引入的问题,建议各位先降级一下。 > > 降级了2.1.148还是会报一样的错 <img width="1371" height="444" alt="Image" src="https://github.com/user-attachments/assets/04d1ed22-0352-41b0-a2ee-58d4976725c9" /> 测试这个方案可以解决
Author
Owner

@wujunran commented on GitHub (May 29, 2026):

我也一样,claude code现在修改了请求格式导致的 API Error: 400 Failed to deserialize the JSON body into the target type: messages[1].role: unknown variant system, expected user or assistant at line 1 column 30325

如果是用了vscode的插件,记得插件也要降级,2.1.145 我正常了

<!-- gh-comment-id:4570384570 --> @wujunran commented on GitHub (May 29, 2026): > **我也一样,claude code现在修改了请求格式导致的** `API Error: 400 Failed to deserialize the JSON body into the target type: messages[1].role: unknown variant system, expected user or assistant at line 1 column 30325` 如果是用了vscode的插件,记得插件也要降级,2.1.145 我正常了
Author
Owner

@Sanjeever commented on GitHub (May 29, 2026):

遇到同样的问题。根因是 Claude Code v2.1.154+ 改了请求结构,会在 messages 数组中间插入 role: "system" 的消息,而 DeepSeek 的 Anthropic 兼容层只接受 user / assistant,于是报错:

API Error: 400 Failed to deserialize the JSON body into the target type:
messages[1].role: unknown variant `system`, expected `user` or `assistant`

降级到 2.1.153 确实能用,但自动更新会反复拉回新版本,每次都要手动回滚,比较折腾。

另一个思路:不降级 CC、不改配置,在中间加一层本地代理把请求改写掉。我写了一个小工具专门干这个:

👉 deepseek-cc-proxy: https://github.com/Sanjeever/deepseek-cc-proxy

原理:把 messages 里所有的 role: "system" 剥离、合并进顶层 system 字段,再转发给 DeepSeek。流式 / 非流式响应都原样透传,鉴权头也完整透传。依赖只有 starlette + uvicorn + httpx,单文件实现。

用法:

# 1. 启动代理
cd deepseek-cc-proxy
uv run main.py   # 监听 127.0.0.1:8787

# 2. 验证
curl http://127.0.0.1:8787/__health
# {"ok":true,"upstream":"https://api.deepseek.com/anthropic"}

然后把 ~/.claude/settings.json 里的 ANTHROPIC_BASE_URL 从 https://api.deepseek.com/anthropic 改成 http://127.0.0.1:8787,token、模型映射等其他配置全部不动。之后正常使用 Claude Code,400 报错消失,不用跟自动更新斗智斗勇了。


Same issue here. Root cause: Claude Code v2.1.154+ inserts role: "system" messages into the messages array, but DeepSeek's Anthropic-compatible endpoint only accepts user / assistant.

Downgrading to 2.1.153 works, but auto-update keeps pulling you back to the broken version.

Alternative approach — a local proxy that rewrites the request on the fly, no downgrade needed:

👉 deepseek-cc-proxy: https://github.com/Sanjeever/deepseek-cc-proxy

It hoists all messages[].role: "system" entries into the top-level system field, then forwards to DeepSeek. Streaming (SSE) and non-streaming responses are relayed transparently. Auth headers pass through intact.

Quick start:

git clone https://github.com/Sanjeever/deepseek-cc-proxy
cd deepseek-cc-proxy
uv sync
uv run main.py  # listens on 127.0.0.1:8787

Then change ANTHROPIC_BASE_URL to http://127.0.0.1:8787 in ~/.claude/settings.json. Everything else (token, model mappings) stays the same. Works with 2.1.154+ through 2.1.156.

<!-- gh-comment-id:4571932486 --> @Sanjeever commented on GitHub (May 29, 2026): 遇到同样的问题。根因是 Claude Code v2.1.154+ 改了请求结构,会在 `messages` 数组中间插入 `role: "system"` 的消息,而 DeepSeek 的 Anthropic 兼容层只接受 `user` / `assistant`,于是报错: ``` API Error: 400 Failed to deserialize the JSON body into the target type: messages[1].role: unknown variant `system`, expected `user` or `assistant` ``` 降级到 2.1.153 确实能用,但自动更新会反复拉回新版本,每次都要手动回滚,比较折腾。 另一个思路:不降级 CC、不改配置,在中间加一层**本地代理**把请求改写掉。我写了一个小工具专门干这个: 👉 **deepseek-cc-proxy**: https://github.com/Sanjeever/deepseek-cc-proxy 原理:把 `messages` 里所有的 `role: "system"` 剥离、合并进顶层 `system` 字段,再转发给 DeepSeek。流式 / 非流式响应都原样透传,鉴权头也完整透传。依赖只有 `starlette` + `uvicorn` + `httpx`,单文件实现。 用法: ```powershell # 1. 启动代理 cd deepseek-cc-proxy uv run main.py # 监听 127.0.0.1:8787 # 2. 验证 curl http://127.0.0.1:8787/__health # {"ok":true,"upstream":"https://api.deepseek.com/anthropic"} ``` 然后把 `~/.claude/settings.json` 里的 `ANTHROPIC_BASE_URL` 从 `https://api.deepseek.com/anthropic` 改成 `http://127.0.0.1:8787`,token、模型映射等其他配置全部不动。之后正常使用 Claude Code,400 报错消失,不用跟自动更新斗智斗勇了。 --- Same issue here. Root cause: Claude Code v2.1.154+ inserts `role: "system"` messages into the `messages` array, but DeepSeek's Anthropic-compatible endpoint only accepts `user` / `assistant`. Downgrading to 2.1.153 works, but auto-update keeps pulling you back to the broken version. Alternative approach — a local proxy that rewrites the request on the fly, no downgrade needed: 👉 **deepseek-cc-proxy**: https://github.com/Sanjeever/deepseek-cc-proxy It hoists all `messages[].role: "system"` entries into the top-level `system` field, then forwards to DeepSeek. Streaming (SSE) and non-streaming responses are relayed transparently. Auth headers pass through intact. Quick start: ```bash git clone https://github.com/Sanjeever/deepseek-cc-proxy cd deepseek-cc-proxy uv sync uv run main.py # listens on 127.0.0.1:8787 ``` Then change `ANTHROPIC_BASE_URL` to `http://127.0.0.1:8787` in `~/.claude/settings.json`. Everything else (token, model mappings) stays the same. Works with 2.1.154+ through 2.1.156.
Author
Owner

@GuanLuoFu commented on GitHub (May 29, 2026):

Alternative workaround: full-featured Node.js proxy
▎
▎ The Python hoist-proxy above handles the system role issue, but there's another problem: thinking block signature validation.
▎ If your session history loses thinking blocks, you get another 400. I wrote a more complete proxy that covers both:
▎
▎ - 🔧 System messages merged into adjacent user messages (wrapped in <system_context>)
▎ - 💾 Thinking block caching & auto-injection with valid signature (fixes /resume sessions)
▎ - 📡 Streaming response capture for real-time thinking accumulation
▎ - 🛠 Rewrites thinking.type: "adaptive" → "enabled"
▎
▎ Usage:
▎ git clone https://github.com/GuanLuoFu/deepseek-claude-proxy
▎ cd deepseek-claude-proxy
▎ node proxy.js
▎ export ANTHROPIC_BASE_URL=http://127.0.0.1:8080
▎ # ANTHROPIC_MODEL / ANTHROPIC_AUTH_TOKEN unchanged
▎
▎ Zero dependencies (Node.js stdlib only). Works on Win/Mac/Linux.

<!-- gh-comment-id:4572134095 --> @GuanLuoFu commented on GitHub (May 29, 2026): Alternative workaround: full-featured Node.js proxy ▎ ▎ The Python hoist-proxy above handles the system role issue, but there's another problem: thinking block signature validation. ▎ If your session history loses thinking blocks, you get another 400. I wrote a more complete proxy that covers both: ▎ ▎ - 🔧 System messages merged into adjacent user messages (wrapped in <system_context>) ▎ - 💾 Thinking block caching & auto-injection with valid signature (fixes /resume sessions) ▎ - 📡 Streaming response capture for real-time thinking accumulation ▎ - 🛠 Rewrites thinking.type: "adaptive" → "enabled" ▎ ▎ Usage: ▎ git clone https://github.com/GuanLuoFu/deepseek-claude-proxy ▎ cd deepseek-claude-proxy ▎ node proxy.js ▎ export ANTHROPIC_BASE_URL=http://127.0.0.1:8080 ▎ # ANTHROPIC_MODEL / ANTHROPIC_AUTH_TOKEN unchanged ▎ ▎ Zero dependencies (Node.js stdlib only). Works on Win/Mac/Linux.
Author
Owner

@BenedictKing commented on GitHub (May 29, 2026):

重复讨论 解决方案在此 https://github.com/deepseek-ai/awesome-deepseek-agent/issues/167#issuecomment-4580661360

<!-- gh-comment-id:4580685046 --> @BenedictKing commented on GitHub (May 29, 2026): 重复讨论 解决方案在此 https://github.com/deepseek-ai/awesome-deepseek-agent/issues/167#issuecomment-4580661360
Author
Owner

@chansen2000 commented on GitHub (May 30, 2026):

The model issue has been resolved, no other methods are needed. Verified with version 2.1.157.

<!-- gh-comment-id:4581335977 --> @chansen2000 commented on GitHub (May 30, 2026): The model issue has been resolved, no other methods are needed. Verified with version 2.1.157.
Author
Owner

@Lemon7ProPlus commented on GitHub (May 30, 2026):

The model issue has been resolved, no other methods are needed. Verified with version 2.1.157.

But it has some bugs, i think.
The job always be interrupted without any condition.
When I switch back to 150, it works well.

<!-- gh-comment-id:4582540958 --> @Lemon7ProPlus commented on GitHub (May 30, 2026): > The model issue has been resolved, no other methods are needed. Verified with version 2.1.157. But it has some bugs, i think. The job always be interrupted without any condition. When I switch back to 150, it works well.
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
deepseek-ai/awesome-deepseek-agent#57
No description provided.