How OmniRoute Transforms OpenAI Chat Completion Streams to Responses API Using TransformStream
OmniRoute converts OpenAI Chat Completions SSE streams to Responses API SSE format using a custom TransformStream in open-sse/transformer/responsesTransformer.ts that parses chunks, tracks mutable state, and emits properly sequenced events.
The OmniRoute repository implements a bidirectional API adapter that bridges OpenAI's legacy Chat Completions protocol with the newer Responses API format. This transformation is critical for providers that only support Chat Completions while clients expect the modern Responses API structure. The conversion happens through a carefully designed streaming pipeline that preserves real-time behavior while restructuring event payloads.
What is the Responses API TransformStream?
The core of this transformation is createResponsesApiTransformStream, exported from open-sse/transformer/responsesTransformer.ts. This factory function returns a standard web TransformStream that sits between the upstream provider and the downstream client.
// Conceptual structure based on open-sse/transformer/responsesTransformer.ts
createResponsesApiTransformStream({
model, // Target model identifier
logger, // Optional logging instance
enableReasoning // Whether to parse ` tags in incoming text
2. Strips these tags from the main content stream
3. Accumulates reasoning text in `reasoningBuffer`
4. Emits `response.reasoning` events separately from `response.output_text`
For production use, **`open-sse/utils/reasoningPlaceholder.ts`** provides utilities to strip internal reasoning placeholders that should not reach end users.
## Platform-Agnostic Module Loading
The transformer supports both **Node.js (Next.js)** and **Cloudflare Workers** environments through lazy conditional imports:
```typescript
// Only loads Node modules when available
const loadNodeModules = async () => {
if (typeof process !== 'undefined' && process.versions?.node) {
const { createWriteStream } = await import('fs');
const { resolve } = await import('path');
return { createWriteStream, resolve };
}
return null;
};
This pattern prevents runtime errors in edge environments that lack Node.js built-ins like fs and path.
Stream Pipeline Integration
In practice, the transformer composes into a standard web streams pipeline:
// Conceptual usage pattern
const response = await fetch('https://api.provider.com/v1/chat/completions', {
method: 'POST',
body: JSON.stringify({ stream: true, ...payload })
});
const transformer = createResponsesApiTransformStream({
model: 'gpt-4',
enableReasoning: true
});
return new Response(
response.body.pipeThrough(transformer),
{
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache'
}
}
);
The .pipeThrough() method connects the raw provider stream through the transformer to produce Responses API-compatible output without buffering the entire response.
File Structure and Dependencies
The transformation layer spans these source files:
| File | Responsibility |
|---|---|
open-sse/transformer/responsesTransformer.ts |
Core TransformStream implementation and createResponsesApiTransformStream |
open-sse/transformer/chatCompletionsTransformer.ts |
Reverse transformation (Responses → Chat Completions) for the bidirectional adapter |
open-sse/utils/reasoningPlaceholder.ts |
Utility functions for handling reasoning content placeholders |
Summary
- OmniRoute's
TransformStreaminopen-sse/transformer/responsesTransformer.tsperforms real-time Chat Completions to Responses API conversion - The transformer parses SSE chunks, maintains mutable state for incremental reconstruction, and emits sequenced events via the
emit()helper - Reasoning extraction through `` delimiters on each content delta. The operation is O(n) on the chunk size and does not block the stream. For maximum throughput in latency-sensitive applications, reasoning can be disabled by setting
enableReasoning: falsein the transformer options.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →