feat(xai): add xAI TTS backend with Grok voices - #9187
Closed
Julientalbot wants to merge 4 commits into
Closed
Conversation
Julientalbot
force-pushed
the
feat/xai-tts-backend
branch
2 times, most recently
from
April 14, 2026 06:26
fc89104 to
476ae35
Compare
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
force-pushed
the
feat/xai-tts-backend
branch
from
April 14, 2026 09:40
476ae35 to
0e24dd0
Compare
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
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds xAI as a TTS provider in the existing
text_to_speechtool, alongside Edge, ElevenLabs, OpenAI, MiniMax, Mistral, and NeuTTS.Features
[pause],[laugh],[whisper], etc.config.yaml: voice, language, sample_rate undertts.xaiPricing
$4.20 per 1M characters. Max 15,000 characters per request.
Implementation
POST /v1/ttsendpoint directly viarequests(no SDK — xAI TTS is not part of the OpenAI-compatible API surface)Changes
tools/tts_tool.py— New_generate_xai_tts()function, provider routing, availability check (+101 lines)tests/tools/test_tts_mistral.py— Fixcheck_tts_requirements()test to mockXAI_API_KEY(otherwise the test fails when run in an environment where the key is set)Testing
test_tts_mistral.py: 17/17)2 files changed, 103 insertions, 2 deletions