AI SDK Core

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.

import { generateSpeech } from 'ai';
import { openai } from '@ai-sdk/openai';

const result = await generateSpeech({
  model: openai.speech('gpt-4o-mini-tts'),
  text: 'Hello, world!',
  voice: 'alloy',
});

Telemetry

generateSpeech accepts the standard telemetry option. Registered integrations receive start, end, and error events with the provider and model, source text, generated audio byte length, media type and format, warnings, response metadata, provider metadata, and usage when the provider reports it. Raw generated audio is not included in telemetry events.

const result = await generateSpeech({
  model: openai.speech('gpt-4o-mini-tts'),
  text: 'Hello from the AI SDK!',
  telemetry: {
    functionId: 'speak-greeting',
  },
});

Use recordInputs: false or recordOutputs: false when the text or output metadata should not be recorded by telemetry integrations.

To access the generated audio:

const audioData = result.audio.uint8Array; // audio data as Uint8Array
// or
const audioBase64 = result.audio.base64; // audio data as base64 string

Settings

Provider-Specific settings

You can set model-specific settings with the providerOptions parameter.

import { generateSpeech } from 'ai';
import { openai } from '@ai-sdk/openai';

const audio = await generateSpeech({
  model: openai.speech('gpt-4o-mini-tts'),
  text: 'Hello, world!',
  providerOptions: {
    openai: {
      // ...
    },
  },
});

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.

import { openai } from '@ai-sdk/openai';
import { generateSpeech } from 'ai';

const audio = await generateSpeech({
  model: openai.speech('gpt-4o-mini-tts'),
  text: 'Hello, world!',
  abortSignal: AbortSignal.timeout(1000), // Abort after 1 second
});

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.

import { openai } from '@ai-sdk/openai';
import { generateSpeech } from 'ai';

const audio = await generateSpeech({
  model: openai.speech('gpt-4o-mini-tts'),
  text: 'Hello, world!',
  headers: { 'X-Custom-Header': 'custom-value' },
});

Warnings

Warnings (e.g. unsupported parameters) are available on the warnings property.

import { openai } from '@ai-sdk/openai';
import { generateSpeech } from 'ai';

const audio = await generateSpeech({
  model: openai.speech('gpt-4o-mini-tts'),
  text: 'Hello, world!',
});

const warnings = audio.warnings;

Error Handling

When generateSpeech cannot generate a valid audio, it throws a AI_NoSpeechGeneratedError.

This error can arise for any of 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.
import { generateSpeech, NoSpeechGeneratedError } from 'ai';
import { openai } from '@ai-sdk/openai';

try {
  await generateSpeech({
    model: openai.speech('gpt-4o-mini-tts'),
    text: 'Hello, world!',
  });
} catch (error) {
  if (NoSpeechGeneratedError.isInstance(error)) {
    console.log('AI_NoSpeechGeneratedError');
    console.log('Cause:', error.cause);
    console.log('Responses:', error.responses);
  }
}

Speech Models

ProviderModel
OpenAItts-1
OpenAItts-1-hd
OpenAIgpt-4o-mini-tts
Mistralvoxtral-mini-tts-2603
Azure Speechmai-voice-2.1-flash
Azure Speechmai-voice-2.1
Azure Speechmai-voice-2-flash
Azure Speechmai-voice-2
ElevenLabseleven_v3
ElevenLabseleven_multilingual_v2
ElevenLabseleven_flash_v2_5
ElevenLabseleven_flash_v2
ElevenLabseleven_turbo_v2_5
ElevenLabseleven_turbo_v2
Humedefault
Googlegemini-2.5-flash-preview-tts
Googlegemini-2.5-pro-preview-tts
Googlegemini-3.1-flash-tts-preview
Googlegemini-3.8-flash-tts
Googlegemini-3.8-flash-lite-tts
Google Vertexgemini-2.5-flash-tts
Google Vertexgemini-2.5-pro-tts
Google Vertexgemini-2.5-flash-lite-preview-tts
Google Vertexgemini-3.1-flash-tts-preview
xAIdefault
Cartesiasonic-3.5
Cartesiasonic-3
Cartesiasonic-2
Cartesiasonic-turbo
Fish Audios1
Fish Audios2-pro
Fish Audios2.1-pro

Above are a small subset of the speech models supported by the AI SDK providers. For more, see the respective provider documentation.