Heuristic:3641397194 wq Gpt6 Astra Wire API Fallback
| Knowledge Sources | |
|---|---|
| Domains | Networking, AI_Agents |
| Last Updated | 2026-10-26 14:00 GMT |
Overview
When `wireApi` is set to `auto`, try the `/responses` endpoint first and fall back to `/chat/completions` on 404 or 405 to support both OpenAI Responses API and Chat Completions API.
Description
The relay adapter's OpenAI-compatible mode (`mode: 'openai'`) supports two wire APIs: the newer Responses API (`/responses`) and the legacy Chat Completions API (`/chat/completions`). When `wireApi` is set to `auto` (the default), the `_submitOpenAI` method first attempts the Responses API. If the server returns HTTP 404 or 405 — indicating the endpoint doesn't exist — it automatically falls back to the Chat Completions API with a reformatted request body (converting `input` to `messages` array). This allows the adapter to work with both modern OpenAI-compatible providers and legacy ones without requiring the user to know which API the server supports.
Usage
Use this heuristic when building clients for OpenAI-compatible APIs where the server's supported endpoints are unknown. The `auto` mode lets users connect without specifying `wireApi` explicitly. Set `wireApi: 'responses'` or `wireApi: 'chat'` to force a specific API and skip the fallback (useful when the 404/405 response adds latency).
The Insight (Rule of Thumb)
- Action: Try `/responses` first; on 404/405, fall back to `/chat/completions` with reformatted body
- Value: `wireApi: 'auto'` (default); `responses` or `chat` to force
- Trade-off: One extra round-trip on fallback (404/405 response); saves the user from having to know the server's API shape
- Compatibility: Only in `openai` mode; `remote` mode uses explicit endpoints
Reasoning
OpenAI introduced the Responses API as a successor to Chat Completions, but many third-party "OpenAI-compatible" providers only support Chat Completions. Forcing one API would break compatibility with the other. The `auto` mode provides a graceful degradation path: modern servers respond successfully to `/responses`, while legacy servers return 404/405 and the client falls back. The 404/405 status codes are specifically chosen because they indicate "endpoint not found" rather than a request error, making the fallback safe — a 400 or 500 would not trigger fallback.
Code Evidence
Wire API fallback from `relay-adapter.js:557-567`:
let response, responseApi = this.config.wireApi === 'chat' ? 'chat' : 'responses';
if (responseApi === 'chat') response = await this._requestOpenAI(this.config.chatCompletionsPath, { method: 'POST', body: chatBody });
else if (this.config.wireApi === 'responses') response = await this._requestOpenAI(this.config.responsesPath, { method: 'POST', body });
else {
try { response = await this._requestOpenAI(this.config.responsesPath, { method: 'POST', body }); }
catch (error) {
if (![404, 405].includes(error.details?.status)) throw error;
responseApi = 'chat';
response = await this._requestOpenAI(this.config.chatCompletionsPath, { method: 'POST', body: chatBody });
}
}
Request body reformatting from `relay-adapter.js:554-556`:
const body = { model: preflight.provider.model, input: prompt || '...', stream: false, metadata: { ... } };
if (preflight.provider.reasoning) body.reasoning = { effort: preflight.provider.reasoning };
const chatBody = { model: body.model, messages: [{ role: 'user', content: body.input }], stream: false, metadata: body.metadata };
Wire API config from `relay-adapter.js:176-177`:
const wireApi = ['auto', 'responses', 'chat'].includes(text(input.wireApi).toLowerCase())
? text(input.wireApi).toLowerCase() : 'auto';