Cartesia
The Cartesia provider contains Sonic speech generation,
Ink-Whisper batch transcription, and Ink 2 realtime and streaming transcription
support.
Cartesia models are not currently available through AI Gateway, so the opening example uses @ai-sdk/cartesia directly. See Using AI Gateway below for supported alternatives.
Using AI Gateway
The current AI Gateway model catalog does not list Cartesia models. Use @ai-sdk/cartesia for the APIs on this page. To use other speech models through Gateway, install ai and configure a Gateway API key:
$ pnpm add ai
$ npm install ai
$ yarn add ai
$ bun add ai
Set an AI Gateway API key in your environment:
Vercel deployments can also use OIDC authentication. See the Gateway documentation for supported models and APIs.
Using @ai-sdk/cartesia
For direct access to Cartesia, install ai and @ai-sdk/cartesia:
$ pnpm add ai @ai-sdk/cartesia
$ npm install ai @ai-sdk/cartesia
$ yarn add ai @ai-sdk/cartesia
$ bun add ai @ai-sdk/cartesia
Set your Cartesia API credential in the CARTESIA_API_KEY environment variable:
The following sections document the direct @ai-sdk/cartesia provider,
including its model factories and settings. Gateway model and feature
availability can differ.
Provider Instance
You can import the default provider instance cartesia from @ai-sdk/cartesia:
If you need a customized setup, you can import createCartesia from @ai-sdk/cartesia and create a provider instance with your settings:
You can use the following optional settings to customize the Cartesia provider instance:
-
apiKey string | PromiseLike<string> | (() => string | PromiseLike<string>)
Pass a synchronous or asynchronous callback to resolve the key before each request. The callback is responsible for caching and refreshing credentials.
API key that is being sent using the
Authorizationheader. It defaults to theCARTESIA_API_KEYenvironment variable. -
version string
The Cartesia API version to use (sent via the
Cartesia-Versionheader). -
headers Record<string,string>
Custom headers to include in the requests.
-
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation. Defaults to the global
fetchfunction. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing. -
webSocket WebSocketConstructor
Custom WebSocket implementation for Ink 2 streaming transcription. Defaults to the global
WebSocketconstructor.
Speech Models
You can create models that call the Cartesia text-to-speech API
using the .speech() factory method.
The first argument is the model id, e.g. sonic-3.5.
You can use the model with the generateSpeech function. Cartesia requires a voice id:
You can also pass additional provider-specific options using the providerOptions argument:
The following provider options are available:
-
container string
Container format for the output audio. Supported values:
'raw','wav','mp3'. Optional. -
encoding string
Encoding type for the audio output. Supported values:
'pcm_f32le','pcm_s16le','pcm_mulaw','pcm_alaw'. Optional. -
sampleRate number
Sample rate for the output audio in Hz (e.g.
8000,16000,22050,24000,44100,48000). Optional. -
bitRate number
Bitrate for
mp3output in bits per second (e.g.32000,64000,128000,192000). Optional. -
speed number
Controls the speed of the generated speech between
0.6and1.5. Optional. -
volume number
Volume of the generated speech between
0.5and2. Defaults to1. Requires Sonic 3 or newer. Optional. -
emotion string
Emotion of the generated speech, for example
calmorsad. This option is in beta and works best with English. Requires Sonic 3 or newer. See the Cartesia docs for the list of values. Optional. -
language string
The language to generate speech in (ISO 639-1 code). Optional.
-
locale string
A regional locale such as
en-GBores-MX. Regional locales require Sonic 3.6 or later. Optional.Cannot be combined with
providerOptions.cartesia.language. When a genericlanguagecall option is also set,localetakes precedence and the ignored generic option produces an unsupported warning. -
normalization string
Controls how written text is prepared for speech. Use
auto(the default),off, or a language or locale code such asen-INto select reading conventions independently of the speech language. Optional.Cartesia validates the supported values. See the text normalization guide.
-
pronunciationDictionaryId string
ID of a pronunciation dictionary to apply to the text. Dictionaries are created with the Cartesia API, not with this provider. Sonic 3 and newer. Optional.
Model Capabilities
| Model |
|---|
sonic-3.5 |
sonic-3 |
sonic-2 |
sonic-turbo |
sonic-latest |
Realtime Transcription Models
You can create a model for Cartesia's realtime Ink 2
API using the .experimental_realtime() factory method:
Realtime sessions run in the browser and require a short-lived access token
created on your server with cartesia.experimental_realtime.getToken():
Ink 2 produces input transcription events and supports English audio. Turn
detection is enabled by default. To use manual finalization instead, set
turnDetection to null in the session config and call commitAudio() when
the current input is complete.
See Realtime for the complete browser session setup.
Model Capabilities
| Model | Streaming Transcription | Turn Detection |
|---|---|---|
ink-2 |
Streaming Transcription Models
Ink 2 is also available through the transcription streaming API. Create the
model with .transcription() and pass it to experimental_streamTranscribe:
The provider creates a short-lived, STT-scoped Cartesia access token before opening the WebSocket, so the API key is not included in the WebSocket URL. Send audio chunks at approximately their playback rate; Cartesia's realtime endpoint is intended for live audio rather than bulk file upload.
Ink 2 supports English audio and uses Cartesia's native turn detection by
default. To finalize only when the input audio stream ends, pass
providerOptions.cartesia.streaming.turnDetection: false.
By default, the provider maps audio/pcm, audio/pcmu, and audio/pcma to
16-bit PCM, G.711 μ-law, and G.711 A-law respectively. To stream another raw
PCM representation, set providerOptions.cartesia.streaming.encoding to
pcm_s32le, pcm_f16le, or pcm_f32le. For example:
To bias recognition toward product names or jargon, pass up to 100 terms
(up to 1,200 characters in total) in providerOptions.cartesia.streaming.keyterms
(keyterm prompting). Turn detection can be tuned with
turnStartThreshold, turnEagerEndThreshold, turnEndThreshold and
turnEndTimeoutMs; these are ignored, with a warning, when turnDetection is
false. See the Cartesia turn detection docs
for the accepted ranges.
See Transcription for more information about streaming transcription.
Model Capabilities
| Model | Streaming Transcription | Turn Detection | Keyterms |
|---|---|---|---|
ink-2 |
Batch Transcription Models
You can create models that call the Cartesia transcription API
using the .transcription() factory method.
The first argument is the model id e.g. ink-whisper.
You can use the model with the transcribe function:
You can also pass additional provider-specific options using the providerOptions argument:
The following provider options are available:
-
language string
The language of the audio (ISO 639-1 code). If not specified, it defaults to English. Optional.
-
timestampGranularities array of strings
The timestamp granularities to populate. Currently only
'word'is supported. Optional.
Model Capabilities
| Model | Transcription | Duration | Segments | Language |
|---|---|---|---|---|
ink-whisper |