Skip to main content

xAI STT

XaiSTT streams raw audio to xAI’s real-time Speech-to-Text WebSocket (wss://api.x.ai/v1/stt) and yields Patter transcript events. Configuration is carried entirely in the URL query string — there is no setup message — and audio is sent as raw binary frames (no base64). The adapter waits for the server’s transcript.created event before it declares the connection ready, then maps xAI’s is_final / speech_final flags onto Patter’s interim / chunk-final / utterance-final semantics, the same two-flag scheme Deepgram uses. The module also exports a one-shot batch helper — xai_transcribe (Python) / xaiTranscribe (TypeScript) — that wraps the REST endpoint (POST https://api.x.ai/v1/stt) for file or URL transcription with word timestamps.
Beta. The adapter is validated against the xAI STT API spec; it has not yet been exercised against a live phone call.

Install

The Python adapter uses aiohttp (pulled in by the [xai] extra). The TypeScript adapter uses the ws package and the platform fetch — no vendor SDK.

Authentication

Both the streaming and batch endpoints authenticate with an Authorization: Bearer <key> header. XAI_API_KEY is read automatically when api_key / apiKey is omitted.

Usage

Use the namespaced import (getpatter.stt.xai) or the flat re-export (XaiSTT). Both auto-resolve XAI_API_KEY from the environment when api_key is omitted.
Plug it into an agent:

Streaming transcript semantics

xAI emits transcript.partial events whose is_final / speech_final flags map onto the transcript states the pipeline reacts to:
is_finalspeech_finalPatter meaningEmitted when
falsefalseInterim — a live partial for barge-inonly with interim_results on (~every 500 ms)
truefalseChunk final — a locked segment (~3 s)continuous long speech
truetrueUtterance final — end of turnsilence / Smart Turn / finalize()
A trailing transcript.done (after close() sends audio.done) surfaces any remaining text as a final. error frames are logged and do not close the connection. The adapter exposes finalize(), which sends {"type": "finalize"} to force the current utterance to speech_final immediately. The pipeline’s VAD speech-end fast-path duck-types this — without it every turn waits out the full endpointing delay.

Smart Turn end-of-turn detection

Set smart_turn / smartTurn to a confidence threshold in [0.0, 1.0] to enable xAI’s Smart Turn ML end-of-turn model instead of relying on silence-based endpointing alone. The threshold trades responsiveness against interruption safety:
ThresholdBehaviour
Low (~0.1–0.3)Ends the turn eagerly on weak end-of-turn signals — snappiest, but more likely to cut a speaker off mid-thought.
Medium (~0.4–0.6)Balanced default for most conversational agents.
High (~0.7–0.9)Waits for a strong end-of-turn signal — safest against premature cut-offs, slightly higher latency.
Pair it with smart_turn_timeout_ms / smartTurnTimeoutMs ([1, 5000]) to force speech_final after that much silence even when the model is undecided.

Keyterms and diarization

Bias transcription toward domain vocabulary (product names, proper nouns) with up to 100 key terms (≤50 chars each), and turn on speaker diarization so each word carries a speaker index:

Languages

Pass a language code to language to enable server-side text formatting (Inverse Text Normalization) for that language. xAI supports 25 codes for the streaming endpoint:
CodeLanguageCodeLanguageCodeLanguage
arArabicidIndonesianroRomanian
csCzechitItalianruRussian
daDanishjaJapaneseesSpanish
nlDutchkoKoreansvSwedish
enEnglishmkMacedonianthThai
filFilipinomsMalaytrTurkish
frFrenchfaPersianviVietnamese
deGermanplPolish
hiHindiptPortuguese

Encodings and sample rate

encoding accepts pcm (default), mulaw, or alaw. The default pcm @ sample_rate=16000 matches what Patter’s pipeline feeds every STT stage (the carrier bridge decodes inbound μ-law to linear PCM16 @ 16 kHz on both Twilio and Telnyx), so XaiSTT drops in without transcoding. Set mulaw / alaw @ 8000 only when feeding raw carrier audio directly.

Rates

xAI bills STT by audio duration. Patter meters the streaming rate on the pipeline (provider key xai):
PathRate
Streaming (WebSocket)0.20/hr(0.20 / hr (0.003333 / min)
Batch (REST / xai_transcribe)$0.10 / hr
Override the metered rate per-project via Patter(pricing={"xai": {"price": ...}}). See Metrics for the full rate table.

Options

PythonTypeScriptDefaultNotes
api_keyapiKeyReads XAI_API_KEY when omitted.
languagelanguageBCP-47 code enabling server-side text formatting. Auto when unset.
encodingencoding"pcm"pcm / mulaw / alaw.
sample_ratesampleRate16000Input sample rate (Hz).
interim_resultsinterimResultsTrueEmit is_final=false partials (~every 500 ms).
endpointing_msendpointingMsNoneSilence (ms, [0, 5000]) before an utterance is final. None keeps the xAI default (10); 0 = any VAD boundary.
smart_turnsmartTurnNoneSmart Turn end-of-turn confidence threshold [0.0, 1.0]. None leaves it off.
smart_turn_timeout_mssmartTurnTimeoutMsNoneMax silence (ms, [1, 5000]) before forcing speech_final when Smart Turn is on.
keytermskeyterms()Up to 100 bias terms (≤50 chars each).
diarizediarizeFalseSpeaker diarization — words gain a speaker index.
filler_wordsfillerWordsFalseInclude “uh” / “um” in the transcript.
base_urlbaseUrlxAI STT WS endpointOverride for proxying or tests.

Batch transcription

For a one-shot file or URL (rather than a live stream), use the batch helper. Pass either raw audio bytes or a url for xAI to download server-side. Container formats (WAV/MP3/OGG/Opus/FLAC/AAC/MP4/M4A/MKV) are auto-detected; for raw headerless audio pass audio_format (pcm / mulaw / alaw) and sample_rate. Set format=True (with language) for Inverse Text Normalization (“one hundred dollars” → “$100”).
The helper returns a parsed result — XaiTranscription(text, language, duration, words) — where each XaiWord carries text, start, end, and (when diarize is on) speaker.

Low-level usage

The pipeline-mode wrapper adds XAI_API_KEY resolution on top of the underlying provider. To use the provider directly, import it from getpatter.providers.xai_stt:

What’s Next

STT

All STT providers side by side.

xAI TTS

Grok text-to-speech.

xAI Realtime

Grok Voice Agent engine.

Metrics

Cost tracking and rates.