Principle:AI Shell Team Aish API Dialect Dispatch
| Knowledge Sources | |
|---|---|
| Domains | LLM, API_Integration |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Pattern for routing LLM chat completion requests to the correct provider-specific HTTP adapter based on a resolved API dialect.
Description
API Dialect Dispatch is the mechanism that abstracts over the heterogeneity of LLM provider HTTP APIs. Different providers use different request/response formats: OpenAI completions, OpenAI responses, Anthropic messages, and Codex (ChatGPT) responses. The dispatch layer resolves which dialect to use based on the model name, API base URL, and API key, then delegates to the corresponding adapter.
Each adapter implements the same high-level interface — streaming chat completion with tools — but handles its provider's specific JSON format, authentication, and streaming protocol (SSE). The dispatch function is a single entry point that the session layer calls without knowing which provider it is talking to.
This pattern solves the problem of supporting multiple incompatible LLM API formats without leaking provider-specific logic into the session orchestration code.
Usage
Use this principle when an application must support multiple LLM providers with different HTTP API formats. The dialect resolver should be the single point where provider routing is decided, and each adapter should be independently testable.
Theoretical Basis
The dispatch is a strategy resolution + delegation pattern:
# Abstract algorithm (NOT real implementation)
def stream_simple(dialect, ctx, messages, tools, stream, temperature, max_tokens):
adapter = match dialect:
OpenAiCompletions -> openai_completions_adapter
OpenAiResponses -> openai_responses_adapter
AnthropicMessages -> anthropic_messages_adapter
ChatgptResponses -> chatgpt_responses_adapter
return adapter.stream(ctx, messages, tools, stream, temperature, max_tokens)
def resolve_dialect(model, api_base, api_key):
if is_codex_endpoint(api_base): return ChatgptResponses
if is_codex_model(model):
if has_api_key(api_key) and is_openai_base(api_base): return OpenAiResponses
if is_openai_base(api_base): return ChatgptResponses
if is_anthropic_endpoint(api_base): return AnthropicMessages
return OpenAiCompletions # default
Key design ideas:
- Dialect as an enum: the dialect is an explicit resolved value, not inferred per-request
- Codex routing logic: Codex models have complex routing (OAuth vs. API key determines the endpoint)
- Adapter isolation: each adapter is a self-contained module that handles its provider's protocol