5-07-28 12:21:35,514 - ERROR asyncio - Exception i...
# ask-ai
a
2025-07-28 122135,514 - ERROR asyncio - Exception in callback AgentActivity._create_speech_task.<locals>._mark_done_if_needed() at C\Users\Lenovo\Documents\ai backend migration\.venv\Lib\site packages\livekit\agents\voice\agent activity.py329 handle: <Handle AgentActivity._create_speech_task.<locals>._mark_done_if_needed() at C\Users\Lenovo\Documents\ai backend migration\.venv\Lib\site packages\livekit\agents\voice\agent activity.py329> Traceback (most recent call last): File "C:\Python313\Lib\asyncio\events.py", line 89, in _run self._context.run(self._callback, *self._args) ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Lenovo\Documents\ai-backend-migration\.venv\Lib\site-packages\livekit\agents\voice\agent_activity.py", line 331, in _mark_done_if_needed speech_handle._mark_done() ~~~~~~~~~~~~~~~~~~~~~~~~^^ File "C:\Users\Lenovo\Documents\ai-backend-migration\.venv\Lib\site-packages\livekit\agents\voice\speech_handle.py", line 210, in _mark_done self._mark_generation_done() ~~~~~~~~~~~~~~~~~~~~~~~~~~^^ File "C:\Users\Lenovo\Documents\ai-backend-migration\.venv\Lib\site-packages\livekit\agents\voice\speech_handle.py", line 201, in _mark_generation_done raise RuntimeError("cannot use mark_generation_done: no active generation is running.") RuntimeError: cannot use mark_generation_done: no active generation is running.
t
Thanks for reaching out, giri. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The error "RuntimeError: cannot use mark_generation_done: no active generation is running" occurs when the
_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:
Copy code
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:
Copy code
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:
Copy code
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:
Copy code
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