Skip to content

feat: OpenAI-to-Anthropic bridge for Claude models via /v1/chat/completions#8

Merged
prasadus92 merged 4 commits into
prasadus92:mainfrom
gnodet:feat/openai-anthropic-bridge
Jun 6, 2026
Merged

feat: OpenAI-to-Anthropic bridge for Claude models via /v1/chat/completions#8
prasadus92 merged 4 commits into
prasadus92:mainfrom
gnodet:feat/openai-anthropic-bridge

Conversation

@gnodet

@gnodet gnodet commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Add a translation layer that allows OpenAI Chat Completions clients to call Claude models through the /v1/chat/completions endpoint. Requests are automatically converted from OpenAI format to Anthropic Messages format, routed to Vertex AI, and responses are translated back to OpenAI format.

Depends on: #7 (models endpoint improvements), which itself depends on #6 (recent Claude models).

Motivation

Many tools and frameworks only support the OpenAI Chat Completions API format (e.g. LangChain, AutoGen, Cursor, Continue, various AI coding assistants). This bridge lets them use Claude models on Vertex AI without any client-side changes — just point them at http://proxy:8787/v1 and request a Claude model name.

Previously, Claude was only accessible via the /anthropic endpoint using Anthropic-native message format. Now both paths work:

  • /anthropic/v1/messages — native Anthropic format (unchanged)
  • /v1/chat/completions — OpenAI format with automatic translation

How it works

When /v1/chat/completions receives a request for a Claude model:

  1. Request translation (openai_to_anthropic_body):

    • Extracts system messages into Anthropic's top-level system field
    • Converts tool role messages to Anthropic tool_result blocks
    • Maps max_tokens/max_completion_tokens, stopstop_sequences, etc.
  2. Routing: Calls Vertex AI Anthropic endpoint (rawPredict/streamRawPredict)

  3. Response translation (anthropic_to_openai_response):

    • Maps content blocks back to OpenAI choices[].message.content
    • Translates stop_reasonfinish_reason
    • Converts input_tokens/output_tokensprompt_tokens/completion_tokens
  4. Streaming: SSE events are translated line-by-line from Anthropic format to OpenAI format

New files

  • vertex_proxy/openai_anthropic_bridge.py — all format conversion functions (pure, stateless, well-separated)

Testing

Tested with:

  • Python openai client (streaming + non-streaming)
  • curl with SSE streaming
  • Hermes Agent (which uses OpenAI-compatible clients internally)

All Claude model aliases work through both the /anthropic and /v1 endpoints.

gnodet added 4 commits June 5, 2026 02:22
…validation

Add model aliases for recently released Claude models:
- Claude Opus 4.6, 4.7, 4.8 (no @Date suffix)
- Claude Sonnet 4.6 (no @Date suffix)
- Claude Sonnet 4 (claude-sonnet-4-20250514)

Fix model validation in _handle_anthropic(): the previous check
`if "@" not in vertex_model` rejects models without a @Date suffix.
Replace with a lookup against known aliases so both formats work.
OpenAI-compatible clients expect 'created' and 'owned_by' fields in
model listings. Set owned_by to the actual provider (anthropic, google,
or the MaaS publisher name).
Clients that set base_url to /anthropic or /gemini now get
model listings from their respective prefix:
  GET /anthropic/v1/models → anthropic models only
  GET /gemini/v1/models    → gemini models only
  GET /gemini/v1beta/models → gemini models only

This fixes Hermes showing '0 models' for vertex-anthropic provider.
…etions

Add translation layer so OpenAI Chat Completions clients can call Claude
models through the /v1 endpoint. Requests are converted to Anthropic
Messages format, sent to Vertex AI, and responses translated back to
OpenAI format.

Supports both streaming (SSE) and non-streaming modes.
New module: openai_anthropic_bridge.py handles all format conversions.
@gnodet gnodet force-pushed the feat/openai-anthropic-bridge branch from 71bd493 to ccd7c45 Compare June 5, 2026 00:23
@prasadus92 prasadus92 merged commit ccd7c45 into prasadus92:main Jun 6, 2026
@prasadus92

Copy link
Copy Markdown
Owner

Thanks a lot for these, @gnodet - really appreciate it. Merged, with a few maintainer follow-ups on top: corrected the Vertex model IDs (the 4.6-gen ones are dateless, and I fixed a couple of pre-existing @Date mistakes on Opus/Haiku 4.5 that were 404'ing), dropped Sonnet 4 since it retires on the 15th, mapped Claude tool_use back to OpenAI tool_calls on the response side, and added tests + README/CHANGELOG. Your commits are on main with author + co-author credit. Thanks again!

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