This message was deleted.
# ask-ai
s
This message was deleted.
t
Thanks for reaching out, Oliver. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The term
livekit.agents.tokenize.token_stream.BufferedSentenceStream
refers to a streaming tokenizer class used to process text input and output sentence-level tokens, which is particularly useful in text-to-speech systems like Cartesia TTS. Here’s how it works and its role in Cartesia TTS: • Initialization: The Cartesia TTS system creates a sentence tokenizer stream using
tts._sentence_tokenizer.stream()
. • Text Processing: Text is pushed into the stream using the
push_text()
method, where it gets tokenized into sentence-level chunks. • Real-time Streaming: The tokenized sentences are sent to Cartesia's WebSocket API for synthesis. Each token is processed and sent with a
continue: True
flag for real-time synthesis. • Completion: When input ends, a final packet is sent with
continue: False
to signal the end of the stream. This approach enables low-latency text-to-speech synthesis by allowing sentences to be synthesized and played back as they are processed, rather than waiting for the entire input to be tokenized. Sources: livekit-plugins/livekit-plugins-nltk/livekit/plugins/nltk/sentence_tokenize...
r
135209 livekit.1 | 2025-07-30 135209,534 - DEBUG asyncio - Using selector: KqueueSelector {“pid”: 12627} 135211 livekit.1 | 2025-07-30 135211,526 - WARNING livekit.agents - failed to synthesize speech, retrying in 2.0s 135211 livekit.1 | Traceback (most recent call last): 135211 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/plugins/cartesia/tts.py”, line 351, in _run 135211 livekit.1 | await asyncio.gather(*tasks) 135211 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/plugins/cartesia/tts.py”, line 309, in _input_task 135211 livekit.1 | self._sent_tokenizer_stream.end_input() 135211 livekit.1 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ 135211 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/tokenize/token_stream.py”, line 94, in end_input 135211 livekit.1 | self.flush() 135211 livekit.1 | ~~~~~~~~~~^^ 135211 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/tokenize/token_stream.py”, line 71, in flush 135211 livekit.1 | self._check_not_closed() 135211 livekit.1 | ~~~~~~~~~~~~~~~~~~~~~~^^ 135211 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/tokenize/token_stream.py”, line 103, in _check_not_closed 135211 livekit.1 | raise RuntimeError(f”{cls.module}.{cls.name} is closed”) 135211 livekit.1 | RuntimeError: livekit.agents.tokenize.token_stream.BufferedSentenceStream is closed 135211 livekit.1 | 135211 livekit.1 | The above exception was the direct cause of the following exception: 135211 livekit.1 | 135211 livekit.1 | Traceback (most recent call last): 135211 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/tts/tts.py”, line 311, in _main_task 135211 livekit.1 | await self._run(output_emitter) 135211 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/plugins/cartesia/tts.py”, line 361, in _run 135211 livekit.1 | raise APIConnectionError() from e 135211 livekit.1 | livekit.agents._exceptions.APIConnectionError: Connection error. (body=None, retryable=True) {“tts”: “livekit.plugins.cartesia.tts.TTS”, “attempt”: 3, “streamed”: true, “pid”: 12590, “job_id”: “AJ_Jqm68j7DhWJo”} 135213 livekit.1 | 2025-07-30 135213,968 - ERROR livekit.agents - Error in _inference_task 135213 livekit.1 | Traceback (most recent call last): 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/plugins/cartesia/tts.py”, line 351, in _run 135213 livekit.1 | await asyncio.gather(*tasks) 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/plugins/cartesia/tts.py”, line 309, in _input_task 135213 livekit.1 | self._sent_tokenizer_stream.end_input() 135213 livekit.1 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/tokenize/token_stream.py”, line 94, in end_input 135213 livekit.1 | self.flush() 135213 livekit.1 | ~~~~~~~~~~^^ 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/tokenize/token_stream.py”, line 71, in flush 135213 livekit.1 | self._check_not_closed() 135213 livekit.1 | ~~~~~~~~~~~~~~~~~~~~~~^^ 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/tokenize/token_stream.py”, line 103, in _check_not_closed 135213 livekit.1 | raise RuntimeError(f”{cls.module}.{cls.name} is closed”) 135213 livekit.1 | RuntimeError: livekit.agents.tokenize.token_stream.BufferedSentenceStream is closed 135213 livekit.1 | 135213 livekit.1 | The above exception was the direct cause of the following exception: 135213 livekit.1 | 135213 livekit.1 | Traceback (most recent call last): 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/utils/log.py”, line 16, in async_fn_logs 135213 livekit.1 | return await fn(*args, **kwargs) 135213 livekit.1 | ^^^^^^^^^^^^^^^^^^^^^^^^^ 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/voice/generation.py”, line 147, in _inference_task 135213 livekit.1 | async for audio_frame in tts_node: 135213 livekit.1 | audio_ch.send_nowait(audio_frame) 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/voice/agent.py”, line 385, in tts_node 135213 livekit.1 | async for ev in stream: 135213 livekit.1 | yield ev.frame 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/tts/tts.py”, line 474, in anext 135213 livekit.1 | raise exc # noqa: B904 135213 livekit.1 | ^^^^^^^^^ 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/agents/tts/tts.py”, line 311, in _main_task 135213 livekit.1 | await self._run(output_emitter) 135213 livekit.1 | File “/Users/olivernaaris/git-projects/polyglotpal/polyglotpal/backend/.venv/lib/python3.13/site-packages/livekit/plugins/cartesia/tts.py”, line 361, in _run 135213 livekit.1 | raise APIConnectionError() from e 135213 livekit.1 | livekit.agents._exceptions.APIConnectionError: Connection error. (body=None, retryable=True) {“pid”: 12590, “job_id”: “AJ_Jqm68j7DhWJo”} What does this error mean?
t
Hey there! Let's chat about the
BufferedSentenceStream is closed
error. It's like trying to write in a notebook that's already been closed – not gonna work, right? 😊 Here's the scoop: • What causes this error: Think of the stream like a conversation that's been wrapped up. Once it's closed (usually by calling
endInput()
or
close()
), you can't keep adding to it. Trying to do so will give you this heads-up. • When the stream gets closed: ◦ Manually, when you deliberately call
close()
. ◦ Automatically, when
endInput()
says "we're done here". ◦ During TTS completion, when the talking is over. • How to dodge this error: ◦ Don't try to keep chatting after you've said goodbye – no pushing text after closing! ◦ Do a quick check before sending anything:
if (!stream.closed) { stream.pushText(text); }
◦ Plan your code like you'd plan a conversation – timing is everything. ◦ In streaming TTS, make sure you're not trying to add words after the story's finished. Basically, this error is just the code's way of saying, "Hey, we're done here!" It happens when you're not quite syncing up with the stream's lifecycle. No worries – now you know how to handle it! 👍 Sources: BufferedSentenceStream | LiveKit Agents | livekit-plugins/livekit-plugins-resemble/livekit/plugins/resemble/tts.py | agents/src/tts/tts.ts