Skip to documentation
Documentation navigation

Documentation navigation

Documentation / guides

Overview

Translate provider-neutral inference requests, responses, and streams at the wire boundary.

developer

Codecs are the only layer that knows a provider’s JSON and stream dialect. A codec maps the secret-free inference.Request and inference.Response types to and from bytes; transport, routing, authorization, and cancellation remain outside the codec.

Boundary

The public contracts are deliberately split. RequestEncoder and ResponseDecoder handle one-shot calls, while StreamDecoder is optional. ServerCodec is the inverse boundary used by the gateway. A codec is expected to be stateless and safe to share; only a returned StreamEncoder is request-scoped.

%%{init: {"theme":"base","themeVariables":{"background":"#111827","primaryColor":"#1f2937","primaryTextColor":"#f9fafb","primaryBorderColor":"#60a5fa","lineColor":"#94a3b8","secondaryColor":"#172033","tertiaryColor":"#0f172a","fontFamily":"Inter, ui-sans-serif, system-ui"}}}%%
flowchart LR
    R["inference.Request"] --> E["RequestEncoder"]
    E --> B["single-shot body"]
    B --> T["transport"]
    T --> D["ResponseDecoder"]
    D --> P["inference.Response"]
    T --> S["optional StreamDecoder"]
    S --> C["content.Chunk stream"]

Choose a codec

PackageNative endpoint shapeStreaming selectionDistinctive mapping
codec/openaiapiPOST /v1/chat/completionsJSON stream: true and SSEstring or multipart message content
codec/openairesponsesPOST /v1/responsesJSON stream: true and typed SSE eventsitems, function calls, reasoning items
codec/anthropicapiPOST /v1/messagesJSON stream: true and SSEtop-level system, content blocks, cache breakpoints
codec/geminiapi:generateContentroute changes to :streamGenerateContent?alt=ssecontents roles and function parts
codec/bedrockconverseConverse bodyroute and response framing changetagged Converse union, inline media

Use the matching route builder with the codec. The generic transport must not guess a path or replay a body.

Source and proof

Run the contract tests with go test ./codec/....

← back to documentation