Expand description
Cross-shape translation Anthropic ↔ OpenAI (enterprise#16, feature shape-xlat).
Lets a Claude client (POST /v1/messages) transparently use an
OpenAI-shape upstream (Azure AI Foundry, vLLM, Ollama, Groq…): the request
is rewritten Messages→Chat-Completions before it leaves, and the response —
streaming or not — is rewritten back so the caller’s Anthropic SDK never
notices. Pure enabler for the router ([proxy.routing] cross-shape
targets), deliberately not a headline feature (scope gate 12 §6).
Mapping summary:
| Anthropic | OpenAI |
|---|---|
system (string/blocks) | leading role:"system" message |
tool_use block (assistant) | tool_calls[] entry |
tool_result block (user) | role:"tool" message |
image block (base64) | image_url part (data URL) |
tools[].input_schema | tools[].function.parameters |
tool_choice {auto,any,tool} | "auto", "required", function pick |
stop_sequences | stop |
metadata.user_id | user |
cache_control | stripped (OpenAI caches implicitly) |
response content[] / SSE events | choices[0].message / delta chunks |
usage.prompt_tokens(_details.cached) | input_tokens / cache_read_… |
Streaming is a stateful SSE-to-SSE rewrite (StreamXlat): OpenAI
chat.completion.chunk deltas become message_start /
content_block_start|delta|stop / message_delta / message_stop events.
Usage metering does NOT read the translated stream — the forward path scans
the raw upstream bytes with the OpenAI scanner before translation.
Structs§
- Stream
Xlat - Stateful OpenAI→Anthropic SSE translator. Feed raw upstream bytes, get
translated Anthropic event bytes; call
StreamXlat::finishat stream end to flush the closing events if the upstream never sent[DONE].
Functions§
- chat_
to_ messages - Translates a full OpenAI
chat.completionbody into an Anthropic message.None= body not recognizable (caller forwards it unchanged). - error_
to_ anthropic - Maps an OpenAI error envelope (
{"error": {...}}) to the Anthropic one ({"type":"error","error":{...}}) so the caller’s SDK renders the message. - messages_
to_ chat - Translates a parsed Anthropic Messages request into an OpenAI Chat
Completions body.
None= not translatable (caller must fail open and forward natively). - to_
anthropic_ stream - Wraps an (already usage-teed) OpenAI SSE byte stream into the translated Anthropic SSE stream. Chunks that translate to nothing are skipped; on upstream end the closing events are flushed.