openai/openai-realtime-api-beta

How to handle reconnect / resume session?

Opened this issue · 6 comments

Any guidance on how to handle the reconnection to my relay server after the websocket connection dropped (for any reason). Is there an event on the client to listen when the connection drops and to somehow reconnect and resume the previous session?

Hi, how do you detect a disconnection in your app? I'm trying to follow the code. RealtimeAPI creates the websocket and listens for 'close' and 'error' events. Both cases dispatch a 'close' event via inherited RealtimeEventHandler. RealtimeClient has property "realtime" so listening to 'close' on realtime should work...

+1 I see the session_id, it sounds like something I could maybe use to reconnect, but I haven't found how.

Hi, @MarkDaoust where did you find the session_id? I can't find it in my logs.

It's in the session.created message:

with rt.connect(model="gpt-4o-mini-realtime-preview") as connection:
    print_responses(connection, 'session.created')
0 - session.created --------------------------------------------------------------------------------
    {
        "event_id": "event_Aznl8PWOyeGoJOsJtDvN1",
        "session": {
            "id": "sess_Aznl7licPP4C2IjraIyJz",
  ...

Has anyone tried to log the conversation history, and in case of disconnect, start a new session feeding it the previous session's conversation history?

@jarrilla I just save every response. And then analyze the table every 24 hours and feed back the insights in the prompt before every session.