Provider /v1/messages rejects role:"system" inside messages[] — Claude Code ≥ 2.1.154 incompatible

未关闭 适合新手
#1,085 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
api, backend

调研方向

找到 /v1/messages 的请求验证器及其针对 messages[].role 的模型;issue 指出角色限制是故障点。先阅读验证器及其请求测试,然后检查经过验证的消息如何转发到上游。当 user 轮次之后的 system 角色消息通过验证,且现有的 user 和 assistant 用例仍正常工作时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Provider /v1/messages rejects role:"system" inside messages[] — Claude Code ≥ 2.1.154 incompatible

Summary

The Provider Anthropic Messages endpoint validates messages[].role against only user/assistant and returns:

{"type":"error","error":{"type":"invalid_request_error","message":"Invalid input at messages.1.role"}}

This breaks vanilla Claude Code ≥ 2.1.154, which sends a system-role message inside messages[] (a still-open regression: anthropics/claude-code#63469). Your own command-code CLI sends the legacy shape and works — vanilla Claude Code against api.commandcode.ai/provider does not.

Repro (curl, max_tokens 2 — both uncharged/zero-cost)

Works (control, legacy shape):

curl https://api.commandcode.ai/provider/v1/messages \
  -H "Authorization: Bearer $CMD_PROVIDER_API_KEY" \
  -H "anthropic-version: 2023-06-01" -H "content-type: application/json" \
  -d '{"model":"claude-haiku-5-5","max_tokens":2,"messages":[{"role":"user","content":"hi"}]}'
# → HTTP 200, normal completion

Fails with 400 Invalid input at messages.0.role (system-role message):

curl https://api.commandcode.ai/provider/v1/messages \
  -H "Authorization: Bearer $CMD_PROVIDER_API_KEY" \
  -H "anthropic-version: 2023-06-01" -H "content-type: application/json" \
  -d '{"model":"claude-haiku-5-5","max_tokens":2,"messages":[{"role":"system","content":"You reply tersely."},{"role":"user","content":"hi"}]}'

Verified 2026-10-29: identical 400 on every Claude model in the catalog — claude-opus-5-5, claude-opus-5, claude-sonnet-5-5, claude-sonnet-4-6, claude-fable-5-1, claude-haiku-5-5 (so it's central validation, not per-upstream). The failure fires before upstream routing model-level plan checks fire earlier; the schema gate is the blocker.

Why this is valid per Anthropic's own API

Working with Messages API → System role in messages documents mid-conversation system messages on Opus 5/5.5, Sonnet 5/5.5, Haiku 5.5, Fable/Mythos 5.x, Opus 4.8 — legal after a user turn (only messages[0] is excluded, and Claude Code ≥ 2.1.154 places it after the first user message). First-party api.anthropic.com accepts this since Claude Code 2.1.154 onward.

References

  • anthropics/claude-code#63469 — regression thread: Claude Code started sending messages[].role="system" in 2.1.154; still unfixed upstream (closed stale 2026-08); broke DeepSeek/GLM/Kimi-compatible gateways for 5 months
  • free-claude-code#611 — a third-party Anthropic-compatible server fixed exactly this by widening its role validator to include "system" (then merging/forwarding)
  • Your docs example requests only ever use user messages, so the docs and validator are consistent today — but Claude Code (the highest-volume /v1/messages client) is not

Suggested fix

In the /v1/messages request validator, allow "system" alongside user|assistant for messages[].role (≈10 lines in the pydantic model: role: Literal["user","assistant","system"]), then either forward as-is to system-role-aware upstreams (Anthropic's newer API accepts it on the 5.x families) or pre-merge system-role messages into the top-level system param for legacy upstreams. The free-claude-code fix is the minimal version and has been in production for 5 months.

Environment: macOS, Claude Code 2.1.308 (Homebrew cask), Provider plan key. happy to provide more traces (request captured verbatim via local listener: anthropic-beta headers list included).

主要语言
没有语言数据
星标
4.2k
派生
377
平均合并
2 天 15 小时
30 天内合并 PR
7

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

CommandCodeAI/command-code 的其他 Issue

查看 CommandCodeAI/command-code 的全部 Issue

相似的 Issue

更多 Backend & API Design Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。