> For the complete documentation index, see [llms.txt](https://ai.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai.ortusbooks.com/advanced/reference/built-in-functions/aispeakstream.md).

# aiSpeakStream

aiSpeakStream() streams synthesized speech to a callback as it is generated.

Stream text-to-speech audio from an AI provider as it is generated. See [Streaming Text-to-Speech](/main-components/audio/streaming-speech.md) for the full guide.

## Syntax

```javascript
summary = aiSpeakStream( text, callback, params={}, options={} )
```

## Parameters

| Parameter  | Type     | Required | Description                                                                                                                |
| ---------- | -------- | -------- | -------------------------------------------------------------------------------------------------------------------------- |
| `text`     | string   | ✅ Yes    | The text to synthesize. Must not be empty                                                                                  |
| `callback` | function | ✅ Yes    | Called with each event struct. Return `false` to stop early and close the provider connection                              |
| `params`   | struct   | No       | Provider parameters: `model`, `voice` (or `voice_id`), `sample_rate`, `language`, `add_timestamps`, `transport` (Cartesia) |
| `options`  | struct   | No       | `provider`, `apiKey`, `outputFormat`, `speed`, `timeout` (idle timeout in seconds, default 30), logging                    |

## Callback events

| `type`       | Fields                                              |
| ------------ | --------------------------------------------------- |
| `audio`      | `data` (binary), `format`, `sampleRate`, `sequence` |
| `timestamps` | `words`, `start`, `end`                             |
| `done`       | `chunks`, `bytes` (always last)                     |

## Returns

A struct: `{ provider, model, audioFormat, sampleRate, chunks, bytes, completed }`. `completed` is `false` when the callback stopped the stream.

## Throws

| Type                    | When                                                 |
| ----------------------- | ---------------------------------------------------- |
| `UnsupportedCapability` | The provider does not support streaming speech       |
| `InvalidArgument`       | Empty text, unsupported format, or invalid transport |
| `ProviderError`         | The provider returned an error                       |

## Examples

```javascript
// Restream pcm to a client
aiSpeakStream(
    "Hello!",
    ( event ) => { if ( event.type == "audio" ) socket.send( event.data ) },
    { voice: "female" },
    { provider: "cartesia", outputFormat: "pcm" }
)

// Fluent builder
summary = aiSpeak()
    .text( "Hello!" )
    .provider( "elevenlabs" )
    .asMP3()
    .stream( ( event ) => { /* ... */ } )
```

## Providers

Cartesia, ElevenLabs, OpenAI, Mistral and Gemini stream. Grok does not (WebSocket only). Check with `aiService( "cartesia" ).hasCapability( "speechStream" )`.

## See Also

* [aiSpeak](/advanced/reference/built-in-functions/aispeak.md)
* [Streaming Text-to-Speech](/main-components/audio/streaming-speech.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://ai.ortusbooks.com/advanced/reference/built-in-functions/aispeakstream.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
