> ## Documentation Index
> Fetch the complete documentation index at: https://hastekit.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Speech generation

> Generate audio and consume provider-supported audio streams.

`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`:

```go theme={null}
model := client.Model("OpenAI/tts-1")
response, err := model.NewSpeech(ctx, &speech.Request{
    Input: "Welcome to the application.",
    Voice: "alloy",
})
if err != nil { return err }
if err := os.WriteFile("speech.mp3", response.Audio, 0600); err != nil { return err }
```

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.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.