Error while Trying to update the session in realtime webrtc

So i’m new to this and i’m building a application and trying to understand how openai’s realtime api with webrtc works.

here’s my frontend where i initiate the connection and create the session and get the ephemeral key.


          const tokenResponse = await fetch("/session");
          const tokenData = await tokenResponse.json();
          console.log("tokenData" + tokenData);
          const EPHEMERAL_KEY = tokenData.client_secret.value;
          console.log("Ephemeral key received:", EPHEMERAL_KEY);

and here’s the backend which will take that and send me my response.

# The /session endpoint
@app.route("/session", methods=["GET"])
def session_endpoint():
    openai_api_key = os.environ.get("OPENAI_API_KEY")
    if not openai_api_key:
        return jsonify({"error": "OPENAI_API_KEY not set"}), 500

    # Make a synchronous POST request to the OpenAI realtime sessions endpoint
    with httpx.Client() as client:
        r = client.post(
            "https://api.openai.com/v1/realtime/sessions",
            headers={
                "Authorization": f"Bearer {openai_api_key}",
                "Content-Type": "application/json",
            },
            json={
                "model": "gpt-4o-realtime-preview-2024-12-17",
                "voice": "verse",
                "instructions": "You are a English Tutor Ria"
            },
        )
        data = r.json()
        print(data)
        return jsonify(data)

and in my frontend i’m passing the sdp and creating the webrtc connection and getting the message response from the datachannel

          // 5. Set up a data channel for events.
          const dc = pc.createDataChannel("oai-events");

          dc.addEventListener("message", (e) => {
            console.log("Data Channel message:", e.data);
            try {


              // Parse the incoming string data to a JavaScript object
              const data = JSON.parse(e.data);


              // Check if the message type is "session.created"
              if (data.type === "session.created") {
                // Log the event ID
                console.log("Event ID:", data.event_id);
                
                // Send the event_id to your backend to update the session


                fetch('/update_session', {
                  method: 'POST',
                  headers: {
                    'Content-Type': 'application/json',
                  },
                  body: JSON.stringify({ event_id: data.event_id }),
                })
                .then(response => response.json())
                .then(data => console.log('Session updated:', data))
                .catch(error => console.error('Error updating session:', error));
              }
            } catch (error) {
              console.error("Error parsing JSON:", error);
            }
          });


and here’s the backend that’s supposed to update my session after i get my session is created i’m trying to hit the update_session endpoint and update my session but i don’t know why it’s not working .

@app.route("/update_session", methods=["POST"])
def update_session_endpoint():
    # Get the event_id from the request
    request_data = request.get_json()
    event_id = request_data.get("event_id")
    
    if not event_id:
        return jsonify({"error": "event_id is required"}), 400
    
    openai_api_key = os.environ.get("OPENAI_API_KEY")
    if not openai_api_key:
        return jsonify({"error": "OPENAI_API_KEY not set"}), 500
    
    # Make a synchronous POST request to the OpenAI realtime sessions endpoint
    with httpx.Client() as client:
        try:
            r = client.post(
                "https://api.openai.com/v1/realtime/sessions",
                headers={
                    "Authorization": f"Bearer {openai_api_key}",
                    "Content-Type": "application/json",
                },
                json={
                    "type": "session.update",
        "session": {
            "instructions": (
                "your a math tutor alex"
            ),
            "turn_detection": {
                "type": "server_vad",
                "threshold": 0.5,
                "prefix_padding_ms": 300,
                "silence_duration_ms": 500
            },
            "voice": "alloy",
            "temperature": 1,
            "max_response_output_tokens": 4096,
            "modalities": ["text", "audio"],
            "input_audio_format": "pcm16",
            "output_audio_format": "pcm16",
            "input_audio_transcription": {
                "model": "whisper-1"
            },
            "tool_choice": "auto",
            "tools": [
            ]
        }
                }
            )
            r.raise_for_status()  # Raise an exception for HTTP errors
            data = r.json()
            print("Session update response:", data)
            return jsonify({"success": True, "data": data})
        except httpx.HTTPStatusError as e:
            print(f"HTTP error occurred: {e}")
            return jsonify({"error": f"HTTP error: {e.response.status_code}", "details": e.response.text}), e.response.status_code
        except httpx.RequestError as e:
            print(f"Request error occurred: {e}")
            return jsonify({"error": f"Request error: {str(e)}"}), 500
        except Exception as e:
            print(f"Unexpected error: {e}")
            return jsonify({"error": f"Unexpected error: {str(e)}"}), 500