Skip to main content
NewSpeech converts text into audio with a speech-capable model. Import pkg/gateway/llm/speech and configure an appropriate provider and voice. Given client and ctx:
This example uses an MP3-producing configuration. In an application, check response.ContentType and use the matching extension or HTTP content type. Audio contains bytes, not a base64 string.

Configure the request

The shared request contains Input, Voice, optional Language, Instruction, ResponseFormat, Speed, and StreamFormat. Provider/model support differs. The Go type of Speed is currently *int; do not copy a fractional speed value from a provider’s native API into this struct. Choose a supported voice and output format for the provider. The SDK does not convert arbitrary provider audio formats into a common codec.

Stream audio

Use NewStreamingSpeech for an adapter that supports streaming. The channel yields OfAudioDelta with base64-encoded audio chunks and OfAudioDone with terminal usage. Decode chunks with error checking and write them in order using the selected codec’s framing requirements. Pass a cancellable context and cancel it when playback or consumption stops. Not every speech adapter supports the streaming method; handle unsupported-operation and initial request errors. Do not assume the Responses text-stream error protocol also exists on the speech chunk type.