@peron_js/oai2ant
v0.1.1
Published
Zero-dependency converters between the Anthropic Messages API and the OpenAI Chat Completions API.
Readme
oai2ant
Zero-dependency, pure-function converters between the Anthropic Messages API and the OpenAI Chat Completions API. It lets an Anthropic-Messages client talk to an OpenAI-compatible backend: translate the request to OpenAI, then translate the response (and stream) back to Anthropic.
antReqToOaiReq(req)— Anthropic request → OpenAI requestoaiResToAntRes(res)— OpenAI response → Anthropic messageoaiStreamToAntStream(chunks)— OpenAI chunk stream → Anthropic SSE events
Types come from the official @anthropic-ai/sdk and openai packages (dev
dependencies only); the published code has no runtime dependencies. Non-standard
fields used by OpenAI-compatible backends — reasoning_content/reasoning,
chat_template_kwargs, defer_loading, tool_reference — are modeled by the
OpenAI* types in src/types/openai.ts, which extend the official SDK types.
Usage
import { antReqToOaiReq, oaiResToAntRes, oaiStreamToAntStream } from "oai2ant";
// 1. Anthropic request -> OpenAI request, send to an OpenAI-compatible backend.
const oaiReq = antReqToOaiReq(anthropicRequest);
// 2. Non-streaming: OpenAI response -> Anthropic message.
const antRes = oaiResToAntRes(await openai.chat.completions.create(oaiReq));
// 3. Streaming: OpenAI chunks -> Anthropic SSE events.
const stream = await openai.chat.completions.create({ ...oaiReq, stream: true });
for await (const event of oaiStreamToAntStream(stream)) {
// event is an Anthropic RawMessageStreamEvent: write it to your SSE response.
}What it converts
- Messages: system prompt, multi-turn text, images (base64 → data URI, url),
tool calls (
tool_use↔tool_calls), and tool results (tool_result→toolrole messages). - Tools: definitions,
tool_choice(incl. parallel-use toggle), anddefer_loading/tool_referencefor chat-template deferred tool loading. - Reasoning:
thinkingconfig →reasoning_effort, and thinking blocks ↔reasoning_content/reasoning(withchat_template_kwargsthinking control). - Stop reasons, token usage, refusals, and the full streaming event lifecycle.
Development
bun install
bun test # 100% coverage is enforced
bun run typecheck
bun run lint