Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/openai/resources/beta/realtime/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ async def recv_bytes(self) -> bytes:

async def send(self, event: RealtimeClientEvent | RealtimeClientEventParam) -> None:
data = (
event.to_json(use_api_names=True, exclude_defaults=True, exclude_unset=True)
event.to_json(use_api_names=True, exclude_unset=True)
if isinstance(event, BaseModel)
else json.dumps(await async_maybe_transform(event, RealtimeClientEventParam))
)
Expand Down Expand Up @@ -467,7 +467,7 @@ def recv_bytes(self) -> bytes:

def send(self, event: RealtimeClientEvent | RealtimeClientEventParam) -> None:
data = (
event.to_json(use_api_names=True, exclude_defaults=True, exclude_unset=True)
event.to_json(use_api_names=True, exclude_unset=True)
if isinstance(event, BaseModel)
else json.dumps(maybe_transform(event, RealtimeClientEventParam))
)
Expand Down
12 changes: 4 additions & 8 deletions src/openai/resources/realtime/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ async def recv_bytes(self) -> bytes:

async def send(self, event: RealtimeClientEvent | RealtimeClientEventParam) -> None:
data = (
event.to_json(use_api_names=True, exclude_defaults=True, exclude_unset=True)
event.to_json(use_api_names=True, exclude_unset=True)
if isinstance(event, BaseModel)
else json.dumps(await async_maybe_transform(event, RealtimeClientEventParam))
)
Expand Down Expand Up @@ -603,9 +603,7 @@ def send(self, event: RealtimeClientEvent | RealtimeClientEventParam) -> None:
are automatically sent once the WebSocket connection opens.
"""
data = (
event.to_json(use_api_names=True, exclude_defaults=True, exclude_unset=True)
if isinstance(event, BaseModel)
else json.dumps(event)
event.to_json(use_api_names=True, exclude_unset=True) if isinstance(event, BaseModel) else json.dumps(event)
)
self.__send_queue.enqueue(data)

Expand Down Expand Up @@ -827,7 +825,7 @@ def recv_bytes(self) -> bytes:

def send(self, event: RealtimeClientEvent | RealtimeClientEventParam) -> None:
data = (
event.to_json(use_api_names=True, exclude_defaults=True, exclude_unset=True)
event.to_json(use_api_names=True, exclude_unset=True)
if isinstance(event, BaseModel)
else json.dumps(maybe_transform(event, RealtimeClientEventParam))
)
Expand Down Expand Up @@ -1073,9 +1071,7 @@ def send(self, event: RealtimeClientEvent | RealtimeClientEventParam) -> None:
are automatically sent once the WebSocket connection opens.
"""
data = (
event.to_json(use_api_names=True, exclude_defaults=True, exclude_unset=True)
if isinstance(event, BaseModel)
else json.dumps(event)
event.to_json(use_api_names=True, exclude_unset=True) if isinstance(event, BaseModel) else json.dumps(event)
)
self.__send_queue.enqueue(data)

Expand Down
83 changes: 83 additions & 0 deletions tests/lib/test_realtime_event_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from __future__ import annotations

import json
from unittest.mock import Mock, AsyncMock

import pytest

from openai.resources.realtime.realtime import (
RealtimeConnection,
AsyncRealtimeConnection,
)
from openai.resources.beta.realtime.realtime import (
RealtimeConnection as BetaRealtimeConnection,
AsyncRealtimeConnection as BetaAsyncRealtimeConnection,
)
from openai.types.realtime.session_update_event import SessionUpdateEvent
from openai.types.beta.realtime.session_update_event import (
Session as BetaSession,
SessionUpdateEvent as BetaSessionUpdateEvent,
)
from openai.types.realtime.realtime_session_create_request import RealtimeSessionCreateRequest


def test_sync_realtime_serializes_explicit_default_value() -> None:
websocket = Mock()
connection = RealtimeConnection(websocket)
event = SessionUpdateEvent(
type="session.update",
session=RealtimeSessionCreateRequest(type="realtime", instructions=None),
)

connection.send(event)

payload = json.loads(websocket.send.call_args.args[0])
assert payload["session"]["instructions"] is None
assert "event_id" not in payload


@pytest.mark.asyncio
async def test_async_realtime_serializes_explicit_default_value() -> None:
websocket = AsyncMock()
connection = AsyncRealtimeConnection(websocket)
event = SessionUpdateEvent(
type="session.update",
session=RealtimeSessionCreateRequest(type="realtime", instructions=None),
)

await connection.send(event)

payload = json.loads(websocket.send.call_args.args[0])
assert payload["session"]["instructions"] is None
assert "event_id" not in payload


def test_sync_beta_realtime_serializes_explicit_null_turn_detection() -> None:
websocket = Mock()
connection = BetaRealtimeConnection(websocket)
event = BetaSessionUpdateEvent(
type="session.update",
session=BetaSession(turn_detection=None),
)

connection.send(event)

payload = json.loads(websocket.send.call_args.args[0])
assert payload["session"]["turn_detection"] is None
assert "event_id" not in payload


@pytest.mark.asyncio
async def test_async_beta_realtime_serializes_explicit_null_turn_detection() -> None:
websocket = AsyncMock()
connection = BetaAsyncRealtimeConnection(websocket)
event = BetaSessionUpdateEvent(
type="session.update",
session=BetaSession(turn_detection=None),
)

await connection.send(event)

payload = json.loads(websocket.send.call_args.args[0])
assert payload["session"]["turn_detection"] is None
assert "event_id" not in payload