Realtime API - session_expired?

{"type": "error", "event_id": "event_AGvaHyTGxxeq2s6GtXhVY", "error": {"type": "invalid_request_error", "code": "session_expired", "message": "Your session hit the maximum duration of 15 minutes.", "param": null, "event_id": null}}

i get this once every minute or so after leaving main.py running for 15 minutes, doesn’t matter if websocket connection is open or closed. Not sure if there’s an easy fix or if it’s something to be concerned about. I can still make calls to the assistant while continuing to get the error periodically.

Yep everyone is hitting this. I added logic to just reset the session every 10 minutes.

It’s likely a token expiring somewhere along the chain. I’m sure they’ll get it fixed soon.

How do you reset the session while retaining the previous session & conversation context?

You cannot maintain a chat session in “realtime” for later.

You are not allowed to place arbitrary audio messages or lead-up, and especially not assistant messages that could train AI in context to produce different voices.

What is the best proposed way to continue a session past 15 mins then?

Solution: Have a similar session on chat completions using the voice preview model.

For past assistant turns that are generated in voice modality, you have to provide a message.audio.id of the voice response content maintained server-side as assistant content instead of your own audio. How long that is maintained before expiration is in the response object, but not explicitly stated as a service level.

You can replace older assistant turns past audio ID expiration with a transcript, but there is the possibility of the AI dropping out of voice mode with too much text as input context.

1 Like

Yeah I tried that passing conversation history along with instructions and it just errors out.

I don’t want to go back to completions API because its higher latency. Trying to find a solution with RealTime API. I wonder if there’s plan to life the 15-mins restriction any time soon.

@stevenic how are you resetting the session? I’m not worried about context (I feed context to the API via messages stored in the DB and then doing session.update).

But what event or events are you calling to “restart” the session?

my code is just prototype code so I just restart the client with a new session. Not super elegant…

2 Likes

I do this @karanjain

Thanks @hagen.rode . 2 challenges in doing this for my use case -
1/ Calling session.update regularly doesn’t update the expiry time, it still stays at 15 mins from the session create time.
2/ Over time my previous conversation becomes too lengthy that session.update starts failing due to message length.

Just stumbled upon this issue again, if possible can anyone give clarity on why this is happening? Any help is appreciated. it’s completely random.
Here is the error:

2025-02-18 18:38:17,302 - ERROR livekit.plugins.openai.realtime - OpenAI S2S error {'type': 'error', 'event_id': 'event_B2Hcfhd3qZdmO9sMwEf1h', 'error': {'type': 'invalid_request_error', 'code': 'session_expired', 'message': 'Your session hit the maximum duration of 30 minutes.', 'param': None, 'event_id': None}} {"session_id": "sess_B2H9dDhDkVdZ5d5NRqe34", "pid": 74633, "job_id": "AJ_QYjknpQEHY4o"}
2025-02-18 18:38:17,303 - ERROR livekit.plugins.openai.realtime - Error in _recv_task
Traceback (most recent call last):
  File "/home/jeel/workspace/fortitude/fortitude-agent-service/venv/lib/python3.13/site-packages/livekit/agents/utils/log.py", line 16, in async_fn_logs
    return await fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jeel/workspace/fortitude/fortitude-agent-service/venv/lib/python3.13/site-packages/livekit/plugins/openai/realtime/realtime_model.py", line 1103, in _recv_task
    raise Exception("OpenAI S2S connection closed unexpectedly")
Exception: OpenAI S2S connection closed unexpectedly {"pid": 74633, "job_id": "AJ_QYjknpQEHY4o"}

I can also provide the code snippet.