Documentation / guides
Request codecs
Encode the neutral Request with typed invoke and stream modes.
Request encoders translate the provider-neutral inference.Request after
feature validation. RequestModeInvoke and RequestModeStream are typed
values, not an unstructured boolean.
Mode
type RequestMode uint8
const (
RequestModeInvoke RequestMode = iota
RequestModeStream
)
func (Codec) EncodeRequest(
req inference.Request,
mode codec.RequestMode,
) (codec.EncodedRequest, error)
OpenAI Chat, OpenAI Responses, and Anthropic put streaming in the JSON body.
Gemini and Bedrock use the same JSON body in both modes; their route and
response framing select streaming. Every bundled encoder returns
Content-Type: application/json and a body reader.
Validation
Encoders call inference.ValidateRequestFeatures before marshaling. That check
rejects a transient-message count outside the message slice, a named tool
choice whose name matches no declared tool, required tool choice without tools,
unsupported image input, invalid structured-output schemas, duplicate tool
names, and capabilities the model does not advertise. Dialect encoders then reject blocks they cannot represent,
returning a typed UnsupportedBlockError or equivalent rather than dropping
data.
body, err := openaiapi.EncodeRequest(req, false)
if err != nil {
var unsupported *openaiapi.UnsupportedBlockError
if errors.As(err, &unsupported) {
// Decide whether to remove the block or choose another model.
}
return err
}
_ = body // JSON bytes are consumed once by the transport.
Source and proof
Run go test ./codec/openaiapi ./codec/openairesponses ./codec/anthropicapi ./codec/geminiapi ./codec/bedrockconverse.