tts

AssemblyAI text-to-speech service implementation.

This module provides integration with AssemblyAI’s Streaming TTS WebSocket API, which synthesizes text into streamed PCM audio with per-word timings.

pipecat.services.assemblyai.tts.language_to_assemblyai_tts_language(language: Language) → str[source]

Convert a Language enum to an AssemblyAI Streaming TTS language.

The TTS API takes the full English name of the language ("spanish"), unlike the STT APIs, which take short codes.

Parameters:

language – The Language enum value to convert.

Returns:

The corresponding AssemblyAI TTS language name.

class pipecat.services.assemblyai.tts.AssemblyAITTSSettings(model: str | None | NotGiven = <factory>, extra: dict[str, Any]=<factory>, voice: str | None | NotGiven = <factory>, language: Language | str | None | NotGiven = <factory>)[source]

Bases: TTSSettings

Settings for AssemblyAITTSService.

voice is the API’s voice_id: any voice by its ID, an AssemblyAI voice by its library slug, such as aai-jane, or a cloned voice by the slug the AssemblyAI dashboard shows. It defaults to Audrey (_DEFAULT_VOICE_ID); None leaves the voice to the server. language is the language the session speaks, and model names the model, such as assemblyai-tts-1. Unset, the session speaks the voice’s own language and runs on that language’s default model.

class pipecat.services.assemblyai.tts.AssemblyAITTSService(*, api_key: str, url: str = 'wss://tts.us.assemblyai.com/v1/ws', sample_rate: int | None = None, settings: AssemblyAITTSSettings | None = None, **kwargs)[source]

Bases: WebsocketTTSService

AssemblyAI Streaming TTS service.

Streams text to AssemblyAI over a single WebSocket session and plays the audio as it arrives. Each sentence is sent as its own request (a Generate followed by a Flush). With TextAggregationMode.TOKEN the turn’s text streams as it is generated and is flushed when the turn ends. Interruptions send Cancel, which keeps the session open, and per-word timings drive word-level TTSTextFrame output.

The session is configured at connect time, so changing the voice, language or model reconnects. A session lasts at most an hour; the service replaces it between requests before it expires.

Example:

# Speaks in Audrey, the default voice.
tts = AssemblyAITTSService(api_key=os.getenv("ASSEMBLYAI_API_KEY"))

# Another voice, by its ID: Keegan.
tts = AssemblyAITTSService(
    api_key=os.getenv("ASSEMBLYAI_API_KEY"),
    settings=AssemblyAITTSService.Settings(voice="987db62e-a2c2-569a-9c4d-82460cdea1bd"),
)
Settings

alias of AssemblyAITTSSettings

__init__(*, api_key: str, url: str = 'wss://tts.us.assemblyai.com/v1/ws', sample_rate: int | None = None, settings: AssemblyAITTSSettings | None = None, **kwargs)[source]

Initialize the AssemblyAI TTS service.

Parameters:
  • api_key – AssemblyAI API key. A temporary token opens a single session, so it can’t carry the service’s reconnects and session renewals.

  • url – Streaming TTS WebSocket URL. Use wss://tts.eu.assemblyai.com/v1/ws to process text and audio in the EU. A cloned voice works only on the host of the zone it was created in.

  • sample_rate – Output sample rate in Hz. If None, uses the pipeline’s output rate.

  • settings – Runtime-updatable settings.

  • **kwargs – Additional arguments passed to the parent WebsocketTTSService.

can_generate_metrics() → bool[source]

Check if this service can generate processing metrics.

Returns:

True, as AssemblyAI TTS supports metrics generation.

language_to_service_language(language: Language) → str | None[source]

Convert a Language enum to an AssemblyAI TTS language name.

Parameters:

language – The language to convert.

Returns:

The AssemblyAI TTS language name.

async setup(setup: FrameProcessorSetup)[source]

Set up the service and connect.

Connecting here, rather than on the first sentence, keeps the connection setup off the first response’s latency.

Parameters:

setup – Configuration object containing setup parameters.

async flush_audio(context_id: str | None = None)[source]

Conclude the turn’s text so the context can close once it is spoken.

In sentence aggregation each sentence is already flushed by run_tts(). When streaming tokens, the turn’s text is flushed here.

Parameters:

context_id – The context whose turn has ended.

async on_audio_context_interrupted(context_id: str)[source]

Cancel the server’s in-flight synthesis when the bot is interrupted.

Cancel discards everything the server has not yet delivered and keeps the session open, so the next response needs no reconnect.

Parameters:

context_id – The ID of the audio context that was interrupted.

async on_audio_context_completed(context_id: str)[source]

Forget a context’s bookkeeping once it has finished playing.

A context that goes quiet mid-turn ends on the base class’s timeout and is recreated when more audio arrives, with word times measured from its new first audio, so its offset starts again from zero.

Parameters:

context_id – The ID of the audio context that finished playing.

async run_tts(text: str, context_id: str) → AsyncGenerator[Frame | None, None][source]

Send text to AssemblyAI for synthesis.

Audio arrives on the receive task. In sentence aggregation each call is flushed as its own request, since only a flush guarantees the server speaks all of the text.

Parameters:
  • text – The text to synthesize.

  • context_id – The audio context the text belongs to.

Yields:

Frame – None, as audio arrives via the WebSocket receive task.