Skip to main content

Overview

xAI provides text-to-speech synthesis via an HTTP API with support for multiple languages and audio encoding formats.

xAI TTS API Reference

Complete API reference for all parameters and methods

Example Implementation

Complete example with interruption handling

xAI Documentation

Official xAI TTS API documentation

Installation

pip install "pipecat-ai[xai]"

Prerequisites

  1. xAI Account: Sign up at xAI
  2. API Key: Generate an API key from your account dashboard (also works with Grok API keys)
Set the following environment variable:
export GROK_API_KEY=your_api_key

Configuration

XAIHttpTTSService

api_key
str
required
xAI API key for authentication.
base_url
str
default:"https://api.x.ai/v1/tts"
xAI TTS endpoint URL. Override for custom or proxied deployments.
sample_rate
int
default:"None"
Output audio sample rate in Hz. When None, uses the pipeline’s configured sample rate.
encoding
str
default:"pcm"
Output audio encoding format. Supported formats: "pcm", "mp3", "wav", "mulaw", "alaw".
aiohttp_session
aiohttp.ClientSession
default:"None"
Optional shared aiohttp session for HTTP requests. If None, the service creates and manages its own session.
settings
XAIHttpTTSService.Settings
default:"None"
Runtime-configurable settings. See Settings below.

Settings

Runtime-configurable settings passed via the settings constructor argument using XAIHttpTTSService.Settings(...). These can be updated mid-conversation with TTSUpdateSettingsFrame. See Service Settings for details.
ParameterTypeDefaultDescription
modelstrNoneModel identifier. (Inherited from base settings.)
voicestr"eve"Voice identifier. (Inherited from base settings.)
languageLanguage | strLanguage.ENLanguage code. (Inherited from base settings.)

Supported Languages

xAI TTS supports 20 languages. Use the Language enum from pipecat.transcriptions.language:
  • Arabic (Egyptian, Saudi, UAE): Language.AR, Language.AR_EG, Language.AR_SA, Language.AR_AE
  • Bengali: Language.BN
  • Chinese: Language.ZH
  • English: Language.EN
  • French: Language.FR
  • German: Language.DE
  • Hindi: Language.HI
  • Indonesian: Language.ID
  • Italian: Language.IT
  • Japanese: Language.JA
  • Korean: Language.KO
  • Portuguese (Brazil, Portugal): Language.PT, Language.PT_BR, Language.PT_PT
  • Russian: Language.RU
  • Spanish (Spain, Mexico): Language.ES, Language.ES_ES, Language.ES_MX
  • Turkish: Language.TR
  • Vietnamese: Language.VI

Usage

Basic Setup

import os
from pipecat.services.xai import XAIHttpTTSService

tts = XAIHttpTTSService(
    api_key=os.getenv("GROK_API_KEY"),
    settings=XAIHttpTTSService.Settings(
        voice="eve",
    ),
)

With Custom Language

from pipecat.transcriptions.language import Language

tts = XAIHttpTTSService(
    api_key=os.getenv("GROK_API_KEY"),
    settings=XAIHttpTTSService.Settings(
        voice="eve",
        language=Language.ES,
    ),
)

With Custom Encoding

tts = XAIHttpTTSService(
    api_key=os.getenv("GROK_API_KEY"),
    encoding="mp3",
    settings=XAIHttpTTSService.Settings(
        voice="eve",
    ),
)

With Shared HTTP Session

import aiohttp

async with aiohttp.ClientSession() as session:
    tts = XAIHttpTTSService(
        api_key=os.getenv("GROK_API_KEY"),
        aiohttp_session=session,
        settings=XAIHttpTTSService.Settings(
            voice="eve",
        ),
    )

Updating Settings at Runtime

Voice settings can be changed mid-conversation using TTSUpdateSettingsFrame:
from pipecat.frames.frames import TTSUpdateSettingsFrame
from pipecat.services.xai.tts import XAITTSSettings
from pipecat.transcriptions.language import Language

await task.queue_frame(
    TTSUpdateSettingsFrame(
        delta=XAITTSSettings(
            language=Language.FR,
        )
    )
)

Notes

  • HTTP-only: This service uses xAI’s HTTP API. The service requests raw PCM audio by default, which matches Pipecat’s downstream expectations without extra decoding.
  • Encoding options: When using non-PCM encodings (mp3, wav, mulaw, alaw), ensure your audio pipeline can handle the selected format.
  • Automatic session management: If you don’t provide an aiohttp_session, the service creates and manages its own session lifecycle automatically.