Skip to content

修改claude system参数为数组,增加通用性 - #1692

Merged
Calcium-Ion merged 1 commit into
QuantumNous:alphafrom
yunayj:alpha
Sep 8, 2025
Merged

修改claude system参数为数组,增加通用性#1692
Calcium-Ion merged 1 commit into
QuantumNous:alphafrom
yunayj:alpha

Conversation

@yunayj

@yunayj yunayj commented Aug 29, 2025

Copy link
Copy Markdown
Contributor

将 openai 格式转换成claude时,如果system参数是一个字符串时,转换成claude格式后,system参数也是一个字符串,现在官方给的参考是字符串或数组皆可,但为了通用性,改为数组

最近,发现一个Claude中转站支持Cherry StudioAnthropic 类型,但是通过new api (v0.9.0-alpha.16)(Anthropic)接入Cherry Studio(OpenAi类型),就系统提示词会失效,经过对比请求参数,发现是New apiOpenAi格式转为Anthropic格式后,system参数默认是一个字符串。

查询Anthropic文档,发现字符串和数组形式皆可,而Cherry Studiosystem参数是数组,为了通用性,将system参数改为数组,哪怕只有一个字符串。

下面是一些请求:

这个是Cherry Studio发送的请求

POST /v1/chat/completions HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 256
x-stainless-timeout: 600
sec-ch-ua-platform: "Windows"
authorization: Bearer sk-xxx
sec-ch-ua: "Not)A;Brand";v="8", "Chromium";v="138"
x-stainless-retry-count: 0
x-api-key: sk-xxx
sec-ch-ua-mobile: ?0
x-title: Cherry Studio
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) CherryStudio/1.5.7 Chrome/138.0.7204.100 Electron/37.2.3 Safari/537.36
accept: application/json
http-referer: https://cherry-ai.com
content-type: application/json
Sec-Fetch-Site: cross-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: zh-CN

{"model":"claude-3-7-sonnet-20250219","messages":[{"role":"system","content":"你是一个小精灵鬼,无论什么都想调皮一下"},{"role":"user","content":"你好"}],"temperature":0.7,"top_p":1,"stream":true,"stream_options":{"include_usage":true}}

这个是New api将上面这个请求转换发后发送的请求

POST /claude/v1/messages HTTP/2
host: xxx.com
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) CherryStudio/1.5.7 Chrome/138.0.7204.100 Electron/37.2.3 Safari/537.36
content-type: application/json
accept: application/json
x-api-key: sk-xxx
anthropic-version: 2023-06-01
content-length: 226
accept-encoding: gzip

{"model":"claude-3-7-sonnet-20250219","system":"你是一个小精灵鬼,无论什么都想调皮一下","messages":[{"role":"user","content":"你好"}],"max_tokens":8192,"temperature":0.7,"top_p":1,"stream":true,"tools":[]}

这个是Cherry Studio通过Anthropic类型接入中转站发送的请求

POST /claude/v1/messages HTTP/2
host: xxx.com
content-length: 445
sec-ch-ua-platform: "Windows"
x-stainless-os: Unknown
anthropic-dangerous-direct-browser-access: true
anthropic-beta: output-128k-2025-02-19
x-stainless-runtime: browser:chrome
x-stainless-arch: unknown
sec-ch-ua: "Not)A;Brand";v="8", "Chromium";v="138"
x-api-key: sk-xxx
sec-ch-ua-mobile: ?0
anthropic-version: 2023-06-01
x-stainless-lang: js
accept: application/json
content-type: application/json
x-stainless-helper-method: stream
x-stainless-timeout: 600
x-stainless-package-version: 0.41.0
x-stainless-runtime-version: 138.0.7204
x-stainless-retry-count: 0
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) CherryStudio/1.5.7 Chrome/138.0.7204.100 Electron/37.2.3 Safari/537.36
sec-fetch-site: cross-site
sec-fetch-mode: cors
sec-fetch-dest: empty
accept-encoding: gzip, deflate, br, zstd
accept-language: zh-CN
priority: u=1, i

{
  "model": "claude-3-7-sonnet-20250219",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "你好"
        }
      ]
    }
  ],
  "max_tokens": 4096,
  "temperature": 0.7,
  "top_p": 1,
  "system": [
    {
      "type": "text",
      "text": "你是一个小精灵鬼,无论什么都想调皮一下"
    }
  ],
  "thinking": {
    "type": "disabled"
  },
  "stream": true
}

下面这个是我这个commit修改后的请求

POST /claude/v1/messages HTTP/2
host: code.newcli.com
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) CherryStudio/1.5.7 Chrome/138.0.7204.100 Electron/37.2.3 Safari/537.36
content-type: application/json
accept: application/json
x-api-key: sk-xxx
anthropic-version: 2023-06-01
content-length: 226
accept-encoding: gzip

{"model":"claude-3-7-sonnet-20250219","system":[{"text":"你是一个小精灵鬼,无论什么都想调皮一下","type":"text"}],"messages":[{"role":"user","content":[{"text":"你好","type":"text"}]}],"max_tokens":4096,"temperature":0.7,"top_p":1,"stream":true,"thinking":{"type":"disabled"}}

Summary by CodeRabbit

  • New Features

    • Support for multiple system messages when using Claude, allowing several system prompts to be included in a single request.
    • Handles composite system content by aggregating multiple text pieces into a unified system context.
    • Lays groundwork for richer media in system prompts in future updates.
  • Improvements

    • More consistent and reliable processing of system prompts, improving overall response context and quality.

@coderabbitai

coderabbitai Bot commented Aug 29, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Shifts Claude system handling from a single string to an array of media messages. Aggregates multiple system messages (including composite text pieces) into []dto.ClaudeMediaMessage and assigns to claudeRequest.System after processing all messages. Other request flow remains unchanged.

Changes

Cohort / File(s) Summary
Claude relay system aggregation
relay/channel/claude/relay-claude.go
Collects all system messages into a local []dto.ClaudeMediaMessage, converting string and composite text content into Type "text" entries; assigns aggregated slice to claudeRequest.System. Notes placeholders for future non-text media types.
API surface update (ClaudeRequest.System)
dto/...
Changes ClaudeRequest.System type from string to []dto.ClaudeMediaMessage, enabling multiple system entries.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant C as Client
  participant R as Relay (Claude)
  participant A as Claude API

  C->>R: Send messages (user/assistant/system)
  R->>R: Iterate messages
  R->>R: Aggregate system text pieces into []ClaudeMediaMessage
  Note right of R: systemMessages = [ {type:"text", text:...}, ... ]
  R->>A: POST ClaudeRequest { System: systemMessages, ... }
  A-->>R: Response
  R-->>C: Return response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I gathered whispers, one by one,
In baskets woven from setting sun—
System seeds now bundled neat,
Texty treats for Claude to eat.
Thump-thump! I hop, relay complete,
Many voices, one tidy beat. 🐇✨

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbit in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbit in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbit gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbit read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbit help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbit ignore or @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbit summary or @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbit or @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
relay/channel/claude/relay-claude.go (1)

277-299: System as array: solid change; add explicit handling for non-text blocks and consider normalizing message content arrays

The switch to accumulating system into []ClaudeMediaMessage is correct and aligns with Anthropic’s array format expectations. One improvement: non-text system blocks are silently dropped. Emit a lightweight log so operators can detect unexpected inputs.

Apply this within the system-content loop:

 for _, ctx := range message.ParseContent() {
   if ctx.Type == "text" {
     systemMessages = append(systemMessages, dto.ClaudeMediaMessage{
       Type: "text",
       Text: common.GetPointer[string](ctx.Text),
     })
   }
+  else {
+    // Surface unexpected system content types without failing the request.
+    common.SysLog(fmt.Sprintf("ignored non-text system content in system[]: type=%s", ctx.Type))
+  }
 }

Note: To fully match the PR goal of “messages/content 也采用数组形式”, also consider normalizing user/assistant string content to []ClaudeMediaMessage (see Line 349 where a plain string is still assigned). I can provide a follow-up patch if desired.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between fbe7f35 and af94e11.

📒 Files selected for processing (1)
  • relay/channel/claude/relay-claude.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
relay/channel/claude/relay-claude.go (2)
dto/claude.go (1)
  • ClaudeMediaMessage (17-36)
common/utils.go (1)
  • GetPointer (218-220)
🔇 Additional comments (1)
relay/channel/claude/relay-claude.go (1)

405-408: Ignore DTO shape concern — System is defined as any with json:"system,omitempty", so assigning a []dto.ClaudeMediaMessage is valid

Likely an incorrect or invalid review comment.

@Calcium-Ion
Calcium-Ion merged commit a616aa3 into QuantumNous:alpha Sep 8, 2025
3 checks passed
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
修改claude system参数为数组,增加通用性
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants