Skip to main content

Usage guide

Setup, environment variables, and Python/JavaScript/Go usage examples.

CartesiaTTSModel


CartesiaTTS

Cartesia Text-to-Speech provider. Synthesizes speech using Cartesia’s Sonic neural TTS models via streaming WebSocket, delivering low-latency audio in PCM format. Voices can be specified by catalog UUID or by a raw voice-embedding vector for voice-cloning use cases. Fine-grained generation controls (speed, emotion, volume) are available on sonic-3 and newer models.

Constructor

api_key
str | None
Cartesia API key for authentication. Falls back to the CARTESIA_API_KEY environment variable when omitted.
voice
Union[str, List[float], None]
Voice specification. Pass a str UUID from the Cartesia voice catalog (e.g. "f8f5f1b2-f02d-4d8e-a40d-fd850a487b3d"), or a list[float] embedding vector for voice cloning. Mutually exclusive with voice_id; voice takes precedence. Defaults to a built-in voice UUID when both are None.
voice_id
Union[str, List[float], None]
Alias for voice provided for backward compatibility. Accepts the same str | list[float] values. Ignored when voice is set.
model
CartesiaTTSModel | str
default:"sonic-2"
Cartesia TTS model identifier. Defaults to "sonic-2". Current models: "sonic-3.5", "sonic-3", "sonic-latest". Generation-config params (speed, emotion, volume) require sonic-3 or newer. Accepts a CartesiaTTSModel enum value or a plain string.
language
str
default:"en"
BCP-47 / ISO 639-1 language code for synthesis. Defaults to "en" (English). Cartesia Sonic supports 35+ languages including "fr", "de", "es", "pt", "zh", "ja", "hi", "it", "ko", "nl", "pl", "ru", "sv", "tr", and "ar".
sample_rate
int
default:"24000"
Requested PCM output sample rate in Hz. Cartesia supports 8000, 16000, 22050, 24000, 44100, and 48000 Hz. Defaults to 24000. Note: the output rate is fixed; a mismatch triggers a warning.
speed
Optional[float]
Playback speed multiplier in the range [0.6, 1.5]. 1.0 is normal speed. Overrides generation_config.speed when set. Defaults to None (provider default). Available on sonic-3 and newer.
volume
Optional[float]
Output volume multiplier in the range [0.5, 2.0]. 1.0 is normal volume. Overrides generation_config.volume when set. Defaults to None. Available on sonic-3 and newer.
emotion
Optional[str]
Emotion tag applied to the synthesized voice. Primary values: "neutral", "calm", "angry", "content", "sad", "scared". The full set contains 53 emotion variants. Overrides generation_config.emotion when set. Defaults to None. Available on sonic-3 and newer.
pronunciation_dict_id
Optional[str]
ID of a Cartesia pronunciation dictionary for custom phoneme rules. Overrides generation_config.pronunciation_dict_id when set. Defaults to None.
max_buffer_delay_ms
Optional[int]
Maximum buffering delay in milliseconds before audio is flushed, controlling the trade-off between streaming latency and audio quality. Overrides generation_config.max_buffer_delay_ms when set. Defaults to None.
enable_word_timestamps
Optional[bool]
When True, includes per-word start/end timing in the streamed response. Overrides generation_config.enable_word_timestamps when set. Defaults to False.
generation_config
Optional[GenerationConfig]
A GenerationConfig instance grouping all generation parameters as an alternative to flat kwargs. Flat parameters always take precedence over the corresponding GenerationConfig field. Defaults to None.

aclose

Release any resources held by the provider.

emit

Emit an event, invoking all registered handlers with the given arguments. Handlers are called in registration order. Coroutine handlers are scheduled on the running event loop. If the emitter is closed, the call is a no-op.
event
T
required
The event to emit.

interrupt

Interrupt and cancel the current synthesis for the active session, if any.

off

Remove a previously registered handler for an event. If the handler is not registered for the event, the call is a no-op.
event
T
required
The event the handler was registered for.
callback
Callable[..., Any]
required
The handler to remove.

on

Register a handler for an event. Can be used directly by passing a callback, or as a decorator when callback is omitted.
event
T
required
The event to listen for.
callback
Callable[..., Any] | None
The handler to invoke when the event is emitted. If None, a decorator is returned that registers the decorated function.
returns
Callable[..., Any]
The registered callback when callback is provided, otherwise a decorator that registers and returns the function it wraps.

on_first_audio_byte

Register the callback invoked when the first audio byte is produced.
callback
'Callable[[int, int], Awaitable[None] | None]'
required
A callable, optionally async, invoked with synthesis timing information when the first audio byte is emitted.

synthesize

Synthesize the given text into speech. Implementations should convert the text to audio. The default behavior forwards the text to the active session for playback when one is present.
text
Any
required
The text to synthesize; coerced to a string if needed.

GenerationConfig

Per-request generation settings for CartesiaTTS. An alternative to supplying individual flat parameters directly to CartesiaTTS.__init__. When both a flat parameter and the corresponding GenerationConfig field are set, the flat parameter takes precedence.

Fields

speed
Optional[float]
Playback speed multiplier in the range [0.6, 1.5]. 1.0 is normal speed. Defaults to None (provider default).
emotion
Optional[str]
Emotion tag applied to the synthesized voice. Primary options include "neutral", "calm", "angry", "content", "sad", "scared". Defaults to None.
volume
Optional[float]
Output volume multiplier in the range [0.5, 2.0]. 1.0 is normal volume. Defaults to None.
pronunciation_dict_id
Optional[str]
ID of a Cartesia pronunciation dictionary to apply custom phoneme rules. Defaults to None.
max_buffer_delay_ms
Optional[int]
Maximum buffering delay in milliseconds before audio is flushed, used to control streaming latency vs. quality. Defaults to None.
enable_word_timestamps
bool
default:"False"
When True, the response will include word-level start/end timing. Defaults to False.