Speech
Learn how to generate speech from text with the AI SDK.
The AI SDK provides the generateSpeech
function to generate speech from text using a speech model.
To access the generated audio:
Settings
Provider-Specific settings
You can set model-specific settings with the providerOptions parameter.
Abort Signals and Timeouts
generateSpeech accepts an optional abortSignal parameter of
type AbortSignal
that you can use to abort the speech generation process or set a timeout.
Custom Headers
generateSpeech accepts an optional headers parameter of type Record<string, string>
that you can use to add custom headers to the speech generation request.
Warnings
Warnings (e.g. unsupported parameters) are available on the warnings property.
Error Handling
When generateSpeech cannot generate a valid audio, it throws a AI_NoSpeechGeneratedError.
This error can arise for any the following reasons:
- The model failed to generate a response
- The model generated a response that could not be parsed
The error preserves the following information to help you log the issue:
responses: Metadata about the speech model responses, including timestamp, model, and headers.cause: The cause of the error. You can use this for more detailed error handling.
Speech Models
| Provider | Model |
|---|---|
| OpenAI | tts-1 |
| OpenAI | tts-1-hd |
| OpenAI | gpt-4o-mini-tts |
| Mistral | voxtral-mini-tts-2603 |
| ElevenLabs | eleven_v3 |
| ElevenLabs | eleven_multilingual_v2 |
| ElevenLabs | eleven_flash_v2_5 |
| ElevenLabs | eleven_flash_v2 |
| ElevenLabs | eleven_turbo_v2_5 |
| ElevenLabs | eleven_turbo_v2 |
| Hume | default |
Above are a small subset of the speech models supported by the AI SDK providers. For more, see the respective provider documentation.