Skip to main content

Module shape_xlat

Module shape_xlat 

Source
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:

AnthropicOpenAI
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_schematools[].function.parameters
tool_choice {auto,any,tool}"auto", "required", function pick
stop_sequencesstop
metadata.user_iduser
cache_controlstripped (OpenAI caches implicitly)
response content[] / SSE eventschoices[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§

StreamXlat
Stateful OpenAI→Anthropic SSE translator. Feed raw upstream bytes, get translated Anthropic event bytes; call StreamXlat::finish at stream end to flush the closing events if the upstream never sent [DONE].

Functions§

chat_to_messages
Translates a full OpenAI chat.completion body 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.