Bug Description
Bug Report
Description
When using OpenAI Realtime model with AgentSession, early user interruptions during the agent's first speech (opening line via session.say()) cause a RuntimeError in _realtime_generation_task. The agent enters a loop, repeating its opening line multiple times before eventually recovering or the user hanging up.
Versions
- livekit-agents: 1.4.1
- livekit-plugins-openai: 1.4.1
- livekit (Python SDK): 1.0.25
- Python: 3.11
- OS: Debian (Docker container)
Steps to Reproduce
- Create an
AgentSession with openai.realtime.RealtimeModel
- Start the session and call
session.say(opening_line, allow_interruptions=True)
- Have the user speak/interrupt while the opening line is still being generated
- The error triggers intermittently — depends on exact timing of the interruption
Observed Behavior
- Agent repeats its opening line 2-3+ times
- Multiple
Error in _realtime_generation_task errors logged
- Sometimes the agent recovers after ~20-30 seconds, sometimes the user gives up and hangs up
- The bug is intermittent — same code works fine when the user waits before speaking
Expected Behavior
The agent should handle the interruption gracefully, stop the current generation, and respond to the user's input.
Stack Trace
ERROR:livekit.agents:Error in _realtime_generation_task Traceback (most recent call last): File ".../livekit/agents/utils/log.py", line 17, in async_fn_logs return await fn(*args, **kwargs) File ".../livekit/agents/voice/agent_activity.py", line 2313, in _realtime_generation_task await self._realtime_generation_task_impl( File ".../livekit/agents/voice/agent_activity.py", line 2630, in _realtime_generation_task_impl await tee.aclose() File ".../livekit/agents/utils/aio/itertools.py", line 95, in aclose await child.aclose() RuntimeError: aclose(): asynchronous generator is already running
Minimal Reproduction Code
from livekit.agents import AgentSession, Agent
from livekit.plugins import openai
realtime_model = openai.realtime.RealtimeModel(voice="coral")
session = AgentSession(llm=realtime_model, allow_interruptions=True)
agent = Agent(instructions="You are a helpful assistant.")
await session.start(agent, room=ctx.room)
await session.say("Hello, this is a test greeting.", allow_interruptions=True)
# User interrupts during this say() → triggers the bug intermittently
Root Cause (suspected)
Race condition in agent_activity.py:2630 — tee.aclose() is called on an async generator that is currently yielding, causing RuntimeError. This happens when the user's interruption triggers cancellation while the realtime generation stream is mid-yield.
Notes
Does NOT affect standard pipeline (STT + LLM + TTS) — only OpenAI Realtime
More likely to trigger on longer opening lines
The error can repeat 6+ times in a single call
### Expected Behavior
The agent should handle early user interruptions gracefully — stop the current generation, discard the incomplete output, and respond to the user's input without repeating the opening line.
### Reproduction Steps
```bash
1. Create an AgentSession with openai.realtime.RealtimeModel(voice="coral")
2. Start the session and call session.say(opening_line, allow_interruptions=True)
3. Have the user speak/interrupt while the opening line audio is still being streamed
4. The error triggers intermittently — depends on exact timing of the interruption relative to the async generator yield
Minimal code:
realtime_model = openai.realtime.RealtimeModel(voice="coral")
session = AgentSession(llm=realtime_model, allow_interruptions=True)
agent = Agent(instructions="You are a helpful assistant.")
await session.start(agent, room=ctx.room)
await session.say("Hello, this is a long greeting that takes a few seconds to stream.", allow_interruptions=True)
# User interrupts during say() → RuntimeError intermittently
Operating System
Debian (Docker container, python:3.11-slim)
Models Used
OpenAI Realtime (gpt-4o-realtime), voice="coral"
Package Versions
livekit==1.0.25
livekit-agents==1.4.1
livekit-plugins-openai==1.4.1
Session/Room/Call IDs
roomID: demo
SIP Call ID: CA6473d10d9b10a3328e96dcde61ea68db (Twilio)
Note: Self-hosted LiveKit, not LiveKit Cloud
Proposed Solution
Wrap the tee.aclose() call in agent_activity.py:2630 with a try/except for RuntimeError, or use a lock to prevent calling aclose() while the generator is mid-yield. The race condition is between the interruption handler and the active generation stream in _realtime_generation_task_impl.
Suspected fix location: livekit/agents/voice/agent_activity.py line 2630
Additional Context
- Bug is intermittent — same code works fine when user waits to speak
- More likely to trigger on longer opening lines (more time for overlap)
- Does NOT affect the standard pipeline (STT + LLM + TTS), only OpenAI Realtime
- When triggered, the error repeats 2-6+ times in a row, causing the agent to loop and repeat its opening line
- Sometimes the agent recovers after ~20-30s, other times the user hangs up
Screenshots and Recordings
No response
Bug Description
Bug Report
Description
When using OpenAI Realtime model with
AgentSession, early user interruptions during the agent's first speech (opening line viasession.say()) cause aRuntimeErrorin_realtime_generation_task. The agent enters a loop, repeating its opening line multiple times before eventually recovering or the user hanging up.Versions
Steps to Reproduce
AgentSessionwithopenai.realtime.RealtimeModelsession.say(opening_line, allow_interruptions=True)Observed Behavior
Error in _realtime_generation_taskerrors loggedExpected Behavior
The agent should handle the interruption gracefully, stop the current generation, and respond to the user's input.
Stack Trace
ERROR:livekit.agents:Error in _realtime_generation_task Traceback (most recent call last): File ".../livekit/agents/utils/log.py", line 17, in async_fn_logs return await fn(*args, **kwargs) File ".../livekit/agents/voice/agent_activity.py", line 2313, in _realtime_generation_task await self._realtime_generation_task_impl( File ".../livekit/agents/voice/agent_activity.py", line 2630, in _realtime_generation_task_impl await tee.aclose() File ".../livekit/agents/utils/aio/itertools.py", line 95, in aclose await child.aclose() RuntimeError: aclose(): asynchronous generator is already running
Minimal Reproduction Code
Operating System
Debian (Docker container, python:3.11-slim)
Models Used
OpenAI Realtime (gpt-4o-realtime), voice="coral"
Package Versions
Session/Room/Call IDs
roomID: demo
SIP Call ID: CA6473d10d9b10a3328e96dcde61ea68db (Twilio)
Note: Self-hosted LiveKit, not LiveKit Cloud
Proposed Solution
Additional Context
Screenshots and Recordings
No response