Skip to content

feat(xai): add xAI TTS backend with Grok voices - #9187

Closed
Julientalbot wants to merge 4 commits into
NousResearch:mainfrom
Julientalbot:feat/xai-tts-backend
Closed

feat(xai): add xAI TTS backend with Grok voices#9187
Julientalbot wants to merge 4 commits into
NousResearch:mainfrom
Julientalbot:feat/xai-tts-backend

Conversation

@Julientalbot

Copy link
Copy Markdown
Contributor

Summary

Adds xAI as a TTS provider in the existing text_to_speech tool, alongside Edge, ElevenLabs, OpenAI, MiniMax, Mistral, and NeuTTS.

Features

  • 6 multilingual Grok voices: ara, eve, leo, rex, sal (default), una
  • Automatic language detection (or explicit language parameter)
  • Speech tags support: [pause], [laugh], [whisper], etc.
  • Automatic Opus conversion for Telegram voice bubble compatibility
  • Configurable via config.yaml: voice, language, sample_rate under tts.xai

Pricing

$4.20 per 1M characters. Max 15,000 characters per request.

Implementation

  • Uses xAI's POST /v1/tts endpoint directly via requests (no SDK — xAI TTS is not part of the OpenAI-compatible API surface)
  • Generates mp3, then converts to Opus for Telegram when needed (xAI doesn't support Opus natively)
  • Follows the same provider pattern as existing TTS backends (credential resolution, config structure, error handling)
  • Added to Opus conversion list for Telegram compatibility

Changes

  • tools/tts_tool.py — New _generate_xai_tts() function, provider routing, availability check (+101 lines)
  • tests/tools/test_tts_mistral.py — Fix check_tts_requirements() test to mock XAI_API_KEY (otherwise the test fails when run in an environment where the key is set)

Testing

  • Tested against live xAI API with French and English text
  • Verified Opus conversion and native Telegram voice bubble delivery
  • Verified all 6 voices produce output
  • Existing TTS tests pass (test_tts_mistral.py: 17/17)

2 files changed, 103 insertions, 2 deletions

@Julientalbot
Julientalbot force-pushed the feat/xai-tts-backend branch 2 times, most recently from fc89104 to 476ae35 Compare April 14, 2026 06:26
julientalbot-ergonomia and others added 2 commits April 14, 2026 13:39
Add xAI as a TTS backend in tools/tts_tool.py. xAI offers 6 multilingual
voices (ara, eve, leo, rex, sal, una) at $4.20 per 1M characters with
support for speech tags ([pause], [laugh], [whisper]).

Configuration in config.yaml:
  tts:
    provider: xai
    xai:
      voice: sal        # ara, eve, leo, rex, sal, una
      language: auto    # auto, en, fr, de, ja, etc.

Implementation:
- _generate_xai_tts() calls POST /v1/tts with raw audio response
- Credential pool support for managed instances
- MP3 output with automatic Opus conversion for Telegram voice bubbles
- Max 15,000 characters per request (vs 4,000 for the tool schema)
- Added to check_tts_requirements() for availability detection
- Fixed pre-existing test isolation issue in test_tts_mistral.py where
  MINIMAX_API_KEY/XAI_API_KEY env vars could leak into check tests

Docs: https://docs.x.ai/developers/model-capabilities/voice/tts
Truncate text exceeding 15,000 chars (xAI API hard limit) with a
warning log, matching the existing pattern used for the global
MAX_TEXT_LENGTH guard.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Julientalbot
Julientalbot force-pushed the feat/xai-tts-backend branch from 476ae35 to 0e24dd0 Compare April 14, 2026 09:40
Tests cover:
- Missing API key error
- Successful MP3 generation with default voice
- WAV codec selection
- OGG triggers MP3-to-Opus conversion
- Custom voice/language from config
- Text truncation at 15k character limit
- Empty audio response error
@Julientalbot

Copy link
Copy Markdown
Contributor Author

Closing as redundant — the xAI TTS provider was merged into main on 16/04 via #10783 (upgrade to Responses API + TTS provider, cherry-picked from @Jaaneek\s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants