Realtime API - how to create multiple successive responses without running into errors?

i would like to be able to do something like this:

        await openai_ws.send(json.dumps({
            "event_id": "event_234",
            "type": "response.create",
            "response": {
                "modalities": ['audio', 'text'],
                "instructions": f"THIS IS RESPONSE ONE",
                "voice": "alloy",
                "output_audio_format": "g711_ulaw",
                "temperature": 0.7,
                "max_output_tokens": 150
            }
        }))    

        # ** SOME OTHER COMPUTATIONS HERE **
        await openai_ws.send(json.dumps({
            "event_id": "event_234",
            "type": "response.create",
            "response": {
                "modalities": ['audio', 'text'],
                "instructions": f"THIS IS RESPONSE TWO",
                "voice": "alloy",
                "output_audio_format": "g711_ulaw",
                "temperature": 0.7,
                "max_output_tokens": 150
            }
        }))      

but this results in an error:

{'type': 'error', 'event_id': 'event_AJY4PPmCD57vhGHLtGLXE', 'error': {'type': 'invalid_request_error', 'code': None, 'message': 'Conversation already has an active response', 'param': None, 'event_id': None}}

i can stick a

await asyncio.sleep(10)

in between the two create response blocks and it clears the error, but doesn’t give the desired behaviour (i.e. it sleeps 10 seconds BEFORE even the first response is streamed) and even if it slept X seconds between the two responses, it wouldn’t be very robust (first response can take longer or shorter than X)

My guess is that I need to monitor the server events and only submit the second message after a certain response.* event has come through, but it’s unclear to me how exactly to approach it.

Any help is appreciated.

You guessed right. You open your websocket and then you need to listen and handle incoming events. Using the javascript websocket api in the browser it would look like this:

// Handle incoming messages
  ws.onmessage = (event) => {
    try {
      const data = JSON.parse(event.data)
      console.log('[Realtime] Received event:', data)

      // Handle the incoming event
      handleWebsocketEvent(data)
    } catch (error) {
      console.error('[Realtime] Error parsing message:', error)
    }
  }

Handle it like this:

const handleWebsocketEvent = (event) => {
  switch (event.type) {
    case 'response.done':
      // use some logic ( processing a request queue etc.) to check if the correct response is done and to trigger the next request ...
      break
      // ... insert more handlers for other events
    default:
      console.warn('[Realtime] Unhandled event type:', event.type)
  }
}

You can probably also drop the “async” for the websocket calls since you get your response as websocket events.