feat(ai): live speech-to-text sessions with seven provider adapters and a voice support agent example #1964
transcriptionSession() turns live audio into text on the server. Send audio chunks as they arrive, for example from a Twilio call, and iterate interim, final, turn-end, and error events. Then give each turn to chat(). It ships with seven providers: Deepgram (in the new @tanstack/ai-deepgram package), ElevenLabs, OpenAI, xAI, Gemini, Mistral, and Cloudflare Workers AI.
This PR now also contains #2048: a voice support agent example that uses the session on a call, plus the Gemini 3.8 TTS models and a logger change that the example needed.
🎯 Changes#
- Core (
@tanstack/ai).- The
transcriptionSession()activity and theLiveTranscriptionAdaptercontract:audioInputsplusconnect({ emit, end }). - If a provider does not take the input format, the session converts it: G.711 μ-law/A-law decode, plus PCM resampling.
createAudioConverter()is exported. openLiveSocket()in@tanstack/ai/adapter-internals, used by all the adapters. It rejectsconnectwhen the provider sends an error before it is ready, when the signal is already aborted, or afterconnectTimeoutMs(15 s). It also takes a socket that is already open, such as one from the Workers AI binding. If the peer never answers a close, the session ends 1 s later.send()afterclose()starts is dropped, so no audio goes out after the last commit.closeGraceMsandcloseHandshakeMs(default 1000 ms each) set how longclose()waits for late text and for the provider to answer the close.fakeWebSocket()in@tanstack/ai/testing.- The
errorsdebug category does not log a cancelled request. This includes a customErrorthat the caller gives toabort().
- The
- Adapters.
deepgramLiveTranscription(Nova-3 on/v1/listen, Flux on/v2/listen) andcloudflareLiveTranscription(Nova-3 and Flux over theenv.AIbinding).elevenlabsLiveTranscription,openaiLiveTranscription,grokLiveTranscription,geminiLiveTranscription,mistralLiveTranscription.- All use the standard
WebSocket, so they run on Node, Bun, and Workers. Auth goes through a subprotocol, a query param, or a minted token, never custom headers. - Gemini TTS:
gemini-3.8-flash-ttsandgemini-3.8-flash-lite-tts. On these models,systemInstructiongoes tospeechMetadata.style, andturnssetspeechMetadata.speaker.multiSpeakerVoiceConfigwithoutturnsthrows before the request. With Vertex AI or Google auth, a style orturnsthrows, because@google/genaidropsspeechMetadata. geminiLiveTranscriptionrejectsmode: 'SMART', because its finals repeat earlier text.
- Example (from #2048).
examples/react/voice-support-agent: click Start call, ask about TanStack AI, and hear the answer. A Twilio number can point at the same server. Each hop (listen, think, speak) runs on OpenAI, Gemini, or Grok, with your own keys or env keys. - Docs.
- New pages:
docs/media/live-transcription.md,docs/media/live-transcription-advanced.md,docs/adapters/deepgram.md, and the tutorialdocs/tutorials/voice-support-agent.md. - A "Live transcription" section on the ElevenLabs, OpenAI, Grok, and Gemini adapter pages. The
media-generationskill is updated.
- New pages:
- Not in this PR. Vertex, BytePlus Seed ASR, and fal (their sockets need an auth header), Cloudflare over REST outside a Worker, AssemblyAI, middleware and devtools events for sessions, and Deepgram KeepAlive.
Decisions on the open questions of the issue: events are an async iterator and a plain union (not AG-UI), conversion lives in @tanstack/ai, and there is no E2E test because aimock cannot mock live-audio WebSockets.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Testing#
Commands run.
- Earlier:
vitest runon the live transcription tests of the eight packages,test:typesandtest:oxlinton those packages, andkiira checkon the changed docs. In the example:tsc,oxlint,oxfmt --check,vite build, andpnpm test:docs. - For the review fixes in
3e02f1844,800f0054b,426efd222,3bad32abc,8d2c632db, andc18fcd35e:vitest runon the touched test files inai,ai-openai,ai-elevenlabs, andai-gemini. Each new test fails on the old code. Alsotest:typesandtest:oxlinton those packages, andtest:typeson the example. - Not run locally:
pnpm test:prand the E2E suite. CI runs them.
Live runs. A recorded sentence streamed in real time gave the correct text from OpenAI (gpt-transcribe, gpt-live-transcribe), ElevenLabs, xAI, Gemini, and Workers AI (Nova-3 and Flux). Deepgram direct and Mistral have unit tests only. The example had one live browser call on OpenAI. The review fixes in the example were not run live.
Manual test.
- Run
pnpm install && pnpm build. - Set
OPENAI_API_KEYinexamples/react/voice-support-agent/.env.local. - Run
pnpm --filter voice-support-agent dev, open http://localhost:3100, and click the green handset. - Say "How do I add a server tool?" and pause. Expect your words in the captions, then a spoken answer.
- Talk over a long reply. Expect playback to stop at once.
How this PR makes testing easy.
- Each adapter has a
tests/live-transcription.test.ts.fakeWebSocket()tests an adapter with no network. packages/ai/tests/transcription-session.test.tsandlive-socket.test.tscover the session and the socket.- The example app is the end-to-end path. Its README has the browser steps and the TwiML for a Twilio number.
Linked issues#
Closes #1944
Risk / rollback#
Low for existing users. The session, adapters, and example are new. The changed existing behavior is small: the logger skips cancelled requests, and Gemini 3.8 TTS throws for multiSpeakerVoiceConfig without turns. To undo, revert the PR.
The Twilio path of the example now needs TWILIO_STREAM_TOKEN, and /voice takes connections only from the page on localhost (same Origin). The README explains both.
Public API change#
Before
// Only finished files could be transcribed.
await generateTranscription({ adapter: openaiTranscription('gpt-4o-transcribe'), audio: file, threadId })
After
import { transcriptionSession } from '@tanstack/ai'
import { deepgramLiveTranscription } from '@tanstack/ai-deepgram'
const session = await transcriptionSession({
adapter: deepgramLiveTranscription('nova-3'),
audio: { encoding: 'mulaw', sampleRate: 8000 },
})
twilio.addEventListener('message', (e) => session.send(payloadOf(e.data)))
for await (const event of session) if (event.type === 'final') console.log(event.text)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Sign in to join this conversation.