Skip to content

feat: add Claude API compatible endpoint (/v1/messages)#56

Merged
bbbugg merged 1 commit intoiBUHub:mainfrom
zhifu1996:feature/claude-api-support
Feb 3, 2026
Merged

feat: add Claude API compatible endpoint (/v1/messages)#56
bbbugg merged 1 commit intoiBUHub:mainfrom
zhifu1996:feature/claude-api-support

Conversation

@zhifu1996
Copy link
Contributor

Summary

This PR adds support for Claude/Anthropic API format, allowing clients using Anthropic SDK to connect directly to this proxy.

New Features

  • New endpoint: POST /v1/messages
  • Request conversion: Claude format → Gemini format
  • Response conversion: Gemini format → Claude format (both stream and non-stream)
  • System prompt: Support for Claude's separate system field
  • Tool use: Support for tool_use and tool_result blocks
  • Thinking mode: Support for thinking blocks
  • SSE events: Proper Claude streaming format (message_start, content_block_delta, message_stop, etc.)

Usage Example

import anthropic

client = anthropic.Anthropic(
    api_key="your-api-key",
    base_url="http://localhost:7860"
)

# Non-streaming
message = client.messages.create(
    model="gemini-2.5-flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)
print(message.content[0].text)

# Streaming
with client.messages.stream(
    model="gemini-2.5-flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Files Changed

  • src/core/FormatConverter.js - Added Claude ↔ Gemini conversion methods
  • src/core/RequestHandler.js - Added processClaudeRequest() handler
  • src/core/ProxyServerSystem.js - Added /v1/messages route

Testing

All tests passed:

  • ✅ Non-stream request
  • ✅ Request with system prompt
  • ✅ Multi-turn conversation
  • ✅ Stream request with proper SSE events

Add support for Claude/Anthropic API format, allowing clients using
Anthropic SDK to connect directly.

Features:
- New endpoint: POST /v1/messages
- Claude to Gemini request format conversion
- Gemini to Claude response format conversion (stream & non-stream)
- Support for system prompt (separate 'system' field)
- Support for tool_use and tool_result
- Support for thinking blocks
- Proper SSE event format (message_start, content_block_delta, etc.)

Usage:
```python
import anthropic
client = anthropic.Anthropic(
    api_key="your-key",
    base_url="http://localhost:7860"
)
message = client.messages.create(
    model="gemini-2.5-flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)
```
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds Claude/Anthropic API compatibility to the Gemini proxy, enabling clients using Anthropic's SDK to connect to Gemini models through a Claude-compatible interface at /v1/messages.

Changes:

  • New Claude API endpoint with bidirectional format conversion between Claude and Gemini formats
  • Support for streaming and non-streaming modes with proper SSE event formatting
  • Handling of Claude-specific features including system prompts, tool use/results, and thinking blocks

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 13 comments.

File Description
src/core/ProxyServerSystem.js Adds POST route for /v1/messages endpoint
src/core/RequestHandler.js Implements processClaudeRequest handler with streaming/non-streaming support and Claude-specific error formatting
src/core/FormatConverter.js Adds bidirectional conversion methods: translateClaudeToGoogle, translateGoogleToClaudeStream, and convertGoogleToClaudeNonStream

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bbbugg
Copy link
Member

bbbugg commented Feb 3, 2026

claude 开启思考应该直接判断顶层的 claudeBody.thinking.enabled
https://platform.claude.com/docs/zh-CN/build-with-claude/extended-thinking#

@zhifu1996
Copy link
Contributor Author

已根据 review 反馈进行修复,提交 4c399b5

已修复的问题

  1. 思考模式判断 - 修正为检查 claudeBody.thinking?.type === "enabled",符合 Claude API 官方文档

  2. tool_use_id 映射 - 新增 toolUseIdToName 映射表,正确解析 Gemini functionResponse.name

  3. 图片下载安全加固

    • URL 协议验证(仅 http/https)防止 SSRF
    • 30 秒超时
    • 20MB 大小限制

待讨论

  • Client disconnect handler: RequestHandler 已有 res.on("close") 处理,是否需要额外逻辑?
  • Empty googleContents: 是否需要对不支持的消息类型添加 fallback?

@bbbugg
Copy link
Member

bbbugg commented Feb 3, 2026

辛苦了大佬。Copilot review我们来改就行,他很多说的不对。修正的只有思考模式判断就可以了。

@bbbugg bbbugg merged commit 2620e67 into iBUHub:main Feb 3, 2026
6 checks passed
@github-actions
Copy link

github-actions bot commented Feb 5, 2026

🎉 此 PR 的修改已在版本 v0.6.0 中发布。
🎉 The changes in this PR have been released in version v0.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants