Skip to content

Realtime ​

Reference for realtime voice sessions. For concepts, audio formats, and turn handling, see Realtime Voice.

Experimental. The realtime API may change incompatibly while more providers are added.

OpenRealtimeSession ​

go
func OpenRealtimeSession(ctx context.Context, model provider.RealtimeModel, opts ...RealtimeOption) (*RealtimeSession, error)

Opens a provider connection, sends the initial configuration, and starts the event loop. ctx bounds the whole session. Invalid options fail with provider.ErrInvalidRealtimeConfig; unsupported audio formats with provider.ErrUnsupportedAudioFormat; unsupported behavior with provider.ErrRealtimeUnsupported.

RealtimeSession ​

All methods are safe for concurrent use. Input buffers are copied before sending.

MethodDescription
Events() <-chan provider.RealtimeEventNormalized events in provider order. Closed when the session ends.
InputAudio(ctx, audio []byte) errorAppends audio in the negotiated input format.
SendText(ctx, text string) errorSends text into the conversation.
StartInputActivity(ctx) error / EndInputActivity(ctx) errorMarks user speech in TurnModeClientVAD.
CommitInput(ctx) errorEnds the buffered input turn in TurnModeManual.
RequestResponse(ctx) errorAsks for a response explicitly.
Interrupt(ctx, cursor *provider.PlaybackCursor) errorCancels the active response; cursor reports how much audio was actually played.
Close() errorEnds the session and waits for internal goroutines. Idempotent.
Err() errorTerminal error after Events() closes; nil after a normal close.

After Close or a terminal error, sending returns provider.ErrRealtimeSessionClosed.

Options ​

OptionDescription
WithRealtimeSystem(string)System instructions.
WithRealtimeTools(...Tool)Tools offered to the model; those with Execute run automatically.
WithRealtimeToolChoice(string)Tool choice, where the provider supports it.
WithRealtimeAudio(input, output provider.AudioFormat)Audio formats; zero values use the model defaults.
WithRealtimeVoice(string)Provider voice name.
WithRealtimeTurnDetection(provider.TurnDetection)Turn mode and interruption behavior.
WithRealtimeInputTranscription(provider.TranscriptionConfig)Transcribe user audio.
WithRealtimeOutputTranscription(provider.TranscriptionConfig)Transcribe model audio.
WithRealtimeMaxOutputTokens(int)Response token limit.
WithRealtimeProviderOptions(map[string]any)Provider-specific session fields.
WithRealtimeEventBuffer(int)Size of the event queue (default 64).
WithRealtimeBackpressureTimeout(time.Duration)How long a full queue may wait before failing (default 1s).
WithRealtimeToolTimeout(time.Duration)Per-tool timeout (default 30s; ≤ 0 disables).
WithRealtimeMaxParallelTools(int)Concurrent tool limit (default 4).
WithRealtimeSequentialTools(bool)Run tools one at a time.
WithRealtimeOnEvent(func(provider.RealtimeEvent))Observe every event before it is queued.
WithRealtimeOnToolCallStart, WithRealtimeOnToolCallTool observability hooks.
WithRealtimeOnBeforeToolExecute, WithRealtimeOnAfterToolExecuteTool interceptor hooks (authorization, redaction).
WithRealtimeOnPanic(func(PanicInfo))Recovered panics from hooks and tools.

provider Contract ​

go
type RealtimeModel interface {
    ModelID() string
    OpenRealtime(ctx context.Context, params RealtimeParams) (RealtimeConnection, error)
}

type RealtimeConnection interface {
    Events() <-chan RealtimeEvent
    Send(ctx context.Context, event RealtimeClientEvent) error
    Close() error
    Err() error
}

type RealtimeCapableModel interface {
    RealtimeCapabilities() RealtimeCapabilities
}

provider.RealtimeCapabilitiesOf(model) returns the declared capabilities, or the zero value when the model does not implement RealtimeCapableModel.

Key Types ​

TypePurpose
AudioFormatEncoding (AudioPCM16, AudioG711U, AudioG711A), sample rate, channels.
RealtimeParamsInitial session configuration built from the options above.
TurnDetectionMode, DisableAutoResponse, DisableInterruption, provider options.
TranscriptionConfigEnabled, Language, Prompt, provider options.
RealtimeClientEventCommands: audio, activity, text, tool result, response create/cancel.
RealtimeEventEvents: see the event reference.
RealtimeToolCall / RealtimeToolResultTool call from the model and its JSON result.
PlaybackCursorResponse, item, and played duration for truncation on interrupt.
RealtimeCapabilitiesWhat a model supports: audio formats, turn modes, tools, resumption, and more.

Errors ​

ErrorWhen
ErrRealtimeUnsupportedThe model does not support the requested operation or setting.
ErrRealtimeSessionClosedSending after the session closed.
ErrUnsupportedAudioFormatThe audio format is invalid or not supported by the model.
ErrRealtimeBackpressureThe consumer stopped draining events.
ErrInvalidRealtimeConfigInconsistent configuration.

Released under the MIT License.