is session.say meant to only handle a single segme...
# ask-ai
m
is session.say meant to only handle a single segment? or can i send it multiple segments?
t
Thanks for reaching out, Shan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Regarding your question about `session.say`: it accepts either a single text string or an
AsyncIterable[str]
. So you can pass multiple segments by providing an async iterable of strings — each chunk will be streamed for speech synthesis. Sources: Agent speech and audio | LiveKit docs
m
When using ElevenLabs TTS with session.say(async_ite rable), if there’s a pause in the async iterable (e.g., 2-3 seconds while waiting for a tool call), does ElevenLabs close the context? How do I keep the stream alive during pauses in input?
t
You're asking whether ElevenLabs TTS will close the speech context if your
session.say(async_iterable)
pauses (2–3s) while waiting for a tool call, and how to keep the stream alive. Short answer: • ElevenLabs TTS does not automatically close the context for short pauses — the service will continue accepting chunks as long as the async iterable remains open. • However, long idle gaps or network/provider timeouts can interrupt streaming, so you should design to avoid prolonged silence. Practical ways to keep the stream reliable: • Send small placeholder chunks (e.g., "One moment…") during long waits to keep data flowing. • Emit partial/status updates from the iterable while the tool runs, then resume actual content. • If you can, pre-generate or buffer the full response and send it as a single stream when ready. • Tune streaming settings (e.g., lower
streaming_latency
) so synthesis starts sooner and behaves more predictably. If you want, tell me: • how long your typical pauses are, and • whether you control the async iterable code (so I can suggest a concrete pattern/snippet). Sources: External data and RAG | LiveKit Docs | ElevenLabs TTS plugin guide | LiveKit Docs
m
The issue isn’t a pause during the tool call. The issue is that ElevenLabs finishes processing segment 1's text and sends isFinal before segment 2's text even exists. This makes sense - the LLM is still generating when segment 1 finishes speaking. ElevenLabs has no way to know more text is coming. So the question becomes: Is our code supposed to keep the ElevenLabs context open while waiting for more text? Or should it create a new context for segment 2? It seems like the LiveKit framework expects one continuous stream, but ElevenLabs closes when it runs out of buffered text. Cartesia apparently buffers differently or waits for explicit close.
t
You're correct — keep the same ElevenLabs TTS context open across segments. What to do: • Open a single TTS stream and push each text chunk into it as the LLM produces them (use
push_text()
/ equivalent). • Do not create a new context for segment 2 — creating new contexts will break LiveKit’s expected continuous audio turn and produce separate streams. • Only call
end_input()
(or the stream-close API) once the LLM is fully finished and you have delivered the final text. Why this works: • LiveKit expects one continuous audio stream per response turn. ElevenLabs will synthesize whatever text is buffered, so you must keep the stream alive and keep pushing new text to avoid an early finalization. • If ElevenLabs starts synthesis early, tune
streaming_latency
or push brief interim text (e.g., status phrases) to avoid gaps while waiting for more LLM output. If you want, share a short snippet of how you currently create/send the stream and I’ll suggest an exact pattern to keep the context open and feed incremental text. Sources: Agents (Node.js) | livekit/livekit_composite | DeepWiki | ElevenLabs TTS plugin guide | LiveKit Docs | Text-to-speech (TTS) models | LiveKit Docs | Agents Framework | livekit/livekit_composite | DeepWiki | Text and transcriptions | LiveKit docs
m
We’re not keeping the ElevenLabs context open. When segment 1's text runs out (while waiting for more LLM output), ElevenLabs finalizes that context. Then segment 2 creates a NEW context, but by then the generator is dead. The issue is in how LiveKit’s tts_node works - it creates a new stream for each call: async with wrapped_ tts.stream(conn_opt ions=conn_options) as stream: Each _tts_node_inference call in the generation.py loop creates a new stream/context. But we should be using one stream for the entire response. Cartesia works because it processes all input in one stream (never hits FlushSentinel, finished=True after consuming everything). ElevenLabs fails because segment 1's stream closes when its text runs out, killing the generator before segment 2's text arrives. The fix should be: Don’t create new TTS streams per segment. Keep one stream open and push all text to it until the generator is truly exhausted. This is an architectural mismatch between: 1. How generation.py loops over segments (expecting multiple tts_node calls) 2. How ElevenLabs expects one continuous stream
t
You're right about the root cause and the correct fix: keep a single ElevenLabs TTS stream open for the entire response and push every text chunk into it until the generator is truly exhausted. What to change (concise): • Open one TTS stream for the whole response turn instead of opening a new stream per segment. • In generation.py, create or obtain the stream before the segment loop and pass the same stream into each
_tts_node_inference
invocation (or have the node call
push_text()
against the shared stream). • Only call the stream's
end_input()
(or close) after the LLM generator signals finished/exhausted. • Keep the generator alive across segments by avoiding per-segment stream finalization (no FlushSentinel/finished=True until final). Concrete pattern (pseudo-code):
Copy code
# create a single stream once per response
with wrapped_tts.stream(conn_options=conn_options) as stream:
    # start audio sender task once (sends audio from stream -> client)
    start_send_audio_task(stream)

    # drive generation and push text into same stream
    async for segment in generation_loop():   # yields incremental LLM output
        stream.push_text(segment.text)

    # when generator truly done:
    stream.end_input()
    await send_audio_task_finish()
Practical tips: • Ensure
generation_loop()
does not treat each segment as a separate TTS call; it should yield text that you push into the shared stream. • If ElevenLabs begins synthesis early, tune
streaming_latency
or send brief interim text to avoid premature finalization while you wait for more LLM output. • If your current code structure tightly couples stream lifetime to each
_tts_node_inference
call, refactor to separate stream lifecycle (owned by the response turn) from per-segment inference logic. If you want, paste the minimal portion of your
generation.py
loop (the part that opens streams and calls
_tts_node_inference
) and I’ll suggest the exact refactor to reuse one stream.
m
this is what claude code is telling me: ⏺️ This is the answer. The LiveKit assistant is right - we need one stream for the entire response, not one per segment. But here’s the thing: this is a LiveKit framework issue, not something we should fix in our code. The generation.py loop and tts_node are part of LiveKit’s livekit-agents package, not our code. Options: 1. Report to LiveKit - This seems like a bug in how their ElevenLabs plugin interacts with the segment loop. Cartesia works by accident (consumes all input before segments matter), but ElevenLabs exposes the architectural issue. 2. Patch locally - We could modify generation.py to keep one stream open, but that’s fragile and would break on package updates. 3. Work around in our code - Don’t use segment boundaries that cause multiple tts_node calls. But we don’t control that - it comes from how the LLM generates. 4. Switch to Cartesia - It works. Pragmatic but doesn’t fix the underlying issue.
t
You’ve diagnosed this well — it’s an architectural mismatch: LiveKit’s current flow opens a new TTS stream per segment, while ElevenLabs expects a single continuous stream for the whole response. Your four options are accurate. My guidance on choosing a path: • Report to LiveKit (recommended): This is the correct long-term fix. File a bug/feature report describing that the ElevenLabs plugin (or tts_node) must support a single stream lifecycle per response turn (open once, push_text across segments, call end_input only when generator is exhausted). Include a minimal repro showing segmentized LLM output causing multiple streams so maintainers can reproduce and fix in the package. • Patch locally (if you need a dependable short-term fix): Refactor generation.py/tts_node locally to create one stream per response turn and pass it into per-segment inference. Keep the patch isolated and documented so you can reapply or migrate when the package is updated. This is brittle but effective while upstream fixes are pending. • Workaround in your code (limited utility): If you can control generation to avoid segmenting (e.g., buffer more LLM output before emitting segments), that can avoid multiple tts_node calls. You noted you don’t control this reliably, so this is usually not feasible. • Switch to Cartesia (pragmatic alternative): If Cartesia’s buffering model meets your needs now, it’s a valid stopgap. It avoids fixing LiveKit but doesn’t solve the framework bug. If helpful, I can: • Draft a concise bug report you can paste into LiveKit’s issue tracker (include repro steps and expected vs actual behavior). • Suggest the minimal local refactor pattern (code sketch) to keep one stream per response turn so you can patch safely. Which would you like?