average-architect-28889
07/28/2025, 6:53 AMtall-airline-16756
07/28/2025, 6:53 AMtall-airline-16756
07/28/2025, 6:56 AM_mark_generation_done() method is called on a SpeechHandle that doesn't have any active generations. Here are steps to resolve this issue:
• Ensure Proper Generation Initialization: Before calling _mark_generation_done(), ensure that _authorize_generation() has been called. For example: speech._authorize_generation()
await speech._wait_for_generation()
• Check Generation State Before Marking Done: Verify that there are active generations before calling _mark_generation_done(). For example: if not self._current_generation or self._current_generation._done:
return
• Use Proper Error Handling: Wrap _mark_generation_done() in exception handling to manage invalid states gracefully: def _mark_done(self):
with contextlib.suppress(asyncio.InvalidStateError):
self._done_fut.set_result(None)
self._mark_generation_done()
• Follow the Scheduling Pattern: Ensure the proper sequence of operations: speech._authorize_generation()
await speech._wait_for_generation()
# ... generation work happens here ...
speech._mark_generation_done()
Common Causes and Solutions:
• Race Conditions: Use asyncio.shield() and proper synchronization to avoid race conditions.
• Interruption Handling: Account for cases where generations may be cancelled or interrupted, and handle them appropriately.
• Task Management: Ensure proper lifecycle management of tasks associated with speech handles.
By following these steps and best practices, you can resolve this error and ensure proper handling of speech generation lifecycles.
Sources: livekit-agents/livekit/agents/voice/generation.py