Any clue what this error could be caused by? It's ...
# ask-ai
t
Any clue what this error could be caused by? It's only happening on one of our developers' devices, with identical hardware. Using docker compose as well, so it's rather strange. livekit-agent-be | {"message": "failed to handle event\nTraceback (most recent call last):\n File \"/usr/local/lib/python3.11/site-packages/livekit/plugins/openai/realtime/realtime_model.py\", line 770, in _recv_task\n self._handle_response_audio_delta(\n File \"/usr/local/lib/python3.11/site-packages/livekit/plugins/openai/realtime/realtime_model.py\", line 1356, in _handle_response_audio_delta\n item_generation = self._current_generation.messages[event.item_id]\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^\nKeyError: 'item_C1qpMgiAkcy28Mdb3m12m'", "level": "ERROR", "name": "livekit.plugins.openai", "event": {"type": "response.audio.delta", "event_id": "event_C1qpNsx21Apt5YZfrW2oi", "response_id": "resp_C1qpMwm2TY7YlY5UTUZDn", "item_id": "item_C1qpMgiAkcy28Mdb3m12m", "output_index": 0, "content_index": 0, "delta": "BAIDAv0B+A..."}, "pid": 27, "job_id": "AJ_VL8T54wMs2hh", "timestamp": "2025-08-07T090353.925790+00:00"}
t
Thanks for reaching out, Tim. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The error you're encountering is caused by a
KeyError
in the LiveKit OpenAI plugin's
realtime_model.py
. This happens when the
item_id
referenced in the
response.audio.delta
event is not found in the
messages
dictionary. Here are some potential causes: • Missing or failed event: The
response.output_item.added
event, which initializes the
item_id
, might not have been processed correctly. • Event ordering issues: The
response.audio.delta
event may have arrived before the corresponding
response.output_item.added
event due to network or API behavior. • Non-message item types: If the
item_id
corresponds to a non-message type (e.g., function calls), it won't be added to the dictionary. • Reconnection race conditions: If the session was recently reconnected, the
item_id
might reference a previous session's state. To resolve this, you can: • Ensure all events are processed in the correct order. • Log and monitor event sequences to identify any discrepancies. • Implement defensive programming to check for the existence of
item_id
before accessing it. • Verify that the session state is properly initialized after reconnections. Let me know if you need further assistance or specific debugging steps. Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/stt.py
t
Anything more helpful that you could share? Sounds like you want me to dive deep in the internals of livekit-agents & the openai plugin?
t
Hey there! To help you out without diving too deep into the internals, here are some practical debugging steps you can try: • Enable detailed logging: Try increasing the logging level for the LiveKit agent and OpenAI plugin. This way, you’ll get more context around what’s happening right before the error pops up. • Check event sequences: Keep an eye on the sequence of events (like
response.output_item.added
and
response.audio.delta
) to make sure they’re coming in the order you expect. • Validate session state: Double-check that the session and generation states are set up correctly and stay consistent, especially after any reconnections. • Reproduce the issue: Since this seems to happen only on one developer’s device, it might help to compare their setup (like Docker configuration or network conditions) with others to spot any differences. • Temporary workaround: You could add a quick check in the code to see if
item_id
exists in the dictionary before trying to access it, and log any missing IDs to investigate further. Hopefully, these steps help you get closer to the root cause without needing big changes to the code. If you want, I’m here to guide you through any of these—just let me know!
t
Where would you recommend taking these steps? Inside the livekit openai plugin?
t
Hey there! So, the steps should mainly zoom in on the LiveKit OpenAI plugin since that’s where the error is popping up—in the
realtime_model.py
file. Here’s what I’d suggest: • Enable detailed logging: Tweak the logging setup inside the LiveKit OpenAI plugin to get more detailed logs. This will help us see what’s really going on. • Check event sequences: Add some logging in the event handling methods like
_handle_response_output_item_added
and
_handle_response_audio_delta
so we can follow the order and details of the events. • Validate session state: Take a close look at how
self._current_generation
and its
messages
dictionary are initialized and updated inside the plugin. • Temporary workaround: You might want to update the plugin to check if
item_id
exists in the dictionary before trying to access it—and log any missing IDs so we can dig deeper later. It’s best to make these changes in a safe space like a dev or staging environment, just to keep your production system running smoothly. If you’d like, I’m happy to help you pinpoint exactly where to tweak or test in the plugin—just let me know!