Skip to content

feat(xai): configurable TTS output sample_rate#6509

Open
randyyu13 wants to merge 1 commit into
livekit:mainfrom
randyyu13:xai-tts-configurable-sample-rate
Open

feat(xai): configurable TTS output sample_rate#6509
randyyu13 wants to merge 1 commit into
livekit:mainfrom
randyyu13:xai-tts-configurable-sample-rate

Conversation

@randyyu13

Copy link
Copy Markdown

What

The xAI TTS plugin hardcodes its output to 24000 Hz PCM (SAMPLE_RATE in _connect_ws and the audio emitter). This PR adds a sample_rate constructor option so callers can pick any xAI-supported rate.

tts = xai.TTS(voice="carina", sample_rate=16000)

Why

When the agent pipeline runs at a different rate than 24 kHz — e.g. 16 kHz for telephony — the fixed 24 kHz output forces an extra resampling stage (24k → pipeline rate), which adds latency and can introduce artifacts. Matching the TTS output to the pipeline rate avoids that stage entirely. The xAI API already supports configurable output sample rate; the plugin just wasn't exposing it.

Changes

  • sample_rate: int = 24000 added to TTS.__init__ and _TTSOptions, threaded into super().__init__(...), the websocket request params, and the AudioEmitter init.
  • Validated against the xAI-supported set (8000, 16000, 22050, 24000, 44100, 48000) with a clear ValueError.
  • Codec stays pcm (LiveKit-native); this PR is scoped to sample rate only.
  • Default is unchanged (24000), so existing behavior is preserved.

Tests

Added to tests/test_plugin_xai_tts.py:

  • default is 24000
  • invalid rate raises ValueError
  • a configured rate reaches the websocket request URL

ruff check + ruff format clean.

The xAI TTS plugin hardcoded the output to 24000 Hz PCM, so pipelines that run
at a different rate (e.g. 16000 Hz for telephony) always incur an extra
resampling stage. Add a sample_rate constructor option (default 24000, one of
the xAI-supported rates) that is threaded into the websocket request and the
audio emitter, letting callers match their pipeline rate and avoid the resample.

Codec stays PCM (LiveKit-native). Validated against the supported-rate set with
a clear error.
@randyyu13
randyyu13 requested a review from a team as a code owner July 22, 2026 15:35
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Randy Yu seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

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