error while using preemptive_generation=True ``` ...
# ask-ai
k
error while using preemptive_generation=True
Copy code
2025-08-03 08:57:22,407 - DEBUG livekit.agents - received user transcript {"user_transcript": "No. We are just checking.", "language": "en", "pid": 10359, "job_id": "AJ_YWXovKTPLDET"}
2025-08-03 08:57:22,533 - DEBUG livekit.agents - received user transcript {"user_transcript": "First. Good.", "language": "en", "pid": 10359, "job_id": "AJ_YWXovKTPLDET"}
2025-08-03 08:57:22,543 - INFO livekit.agents - LLM metrics {"ttft": -1.0, "prompt_tokens": 0, "prompt_cached_tokens": 0, "completion_tokens": 0, "tokens_per_second": 0.0, "pid": 10359, "job_id": "AJ_YWXovKTPLDET"}
2025-08-03 08:57:22,545 - ERROR asyncio - Exception in callback AgentActivity._create_speech_task.<locals>._mark_done_if_needed(<Task finishe...> result=None>) at /.venv/lib/python3.12/site-packages/livekit/agents/voice/agent_activity.py:329
handle: <Handle AgentActivity._create_speech_task.<locals>._mark_done_if_needed(<Task finishe...> result=None>) at /.venv/lib/python3.12/site-packages/livekit/agents/voice/agent_activity.py:329>
Traceback (most recent call last):
  File "/Users/sauravsharma/.local/share/uv/python/cpython-3.12.11-macos-aarch64-none/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/.venv/lib/python3.12/site-packages/livekit/agents/voice/agent_activity.py", line 331, in _mark_done_if_needed
    speech_handle._mark_done()
  File "/.venv/lib/python3.12/site-packages/livekit/agents/voice/speech_handle.py", line 210, in _mark_done
    self._mark_generation_done()
  File "/.venv/lib/python3.12/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. {"pid": 10359, "job_id": "AJ_YWXovKTPLDET"}
t
Thanks for reaching out, Saurav. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The error occurs because the system attempts to mark a generation as done when no active generation is running. To resolve this issue when using
preemptive_generation=True
, ensure the following: • Check Active Generation: Modify the
_mark_generation_done
method in
SpeechHandle
to verify if a generation is active before marking it as done. For example:
Copy code
def _mark_generation_done(self):
    if not self._generations:
        raise RuntimeError("cannot use mark_generation_done: no active generation is running.")
    with contextlib.suppress(asyncio.InvalidStateError):
        self._generations[-1].set_result(None)
Handle Preemptive Generation Cancellation: Ensure that when preemptive generation is canceled, the state is reset properly. For instance, in the
_user_turn_completed_task
method of
AgentActivity
, include logic to cancel preemptive generation if the context changes:
Copy code
if preemptive := self._preemptive_generation:
    if preemptive.info.new_transcript != user_message.text_content or \
       not preemptive.chat_ctx.is_equivalent(temp_mutable_chat_ctx) or \
       preemptive.tools != self.tools or \
       preemptive.tool_choice != self._tool_choice:
        logger.warning("Preemptive generation canceled due to context change")
        preemptive.speech_handle._cancel()
    self._preemptive_generation = None
These steps ensure proper state management and prevent the error from occurring.
k
but is it can be related to turn_detection model or silo vad ? i am using livekit's multilingiual model for turn detection
t
The RuntimeError related to mark_generation_done pops up when there isn’t an active generation running. From what we know, this error doesn’t seem to be directly linked to the turn detection model or VAD (Voice Activity Detection), including LiveKit's multilingual model for turn detection. If you’re thinking the turn detection model or VAD might be causing some trouble, it could be helpful to take a closer look at their settings and logs to spot anything unusual. Feel free to reach out if you want a hand with this! Sources: livekit-agents/livekit/agents/voice/agent_session.py