-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix: transform Omit values in pre-connect Realtime manager send() #3448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
C1-BA-B1-F3
wants to merge
1
commit into
openai:main
Choose a base branch
from
C1-BA-B1-F3:fix/realtime-preconnect-send-omit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| """Regression tests for issue #3402: Pre-connect Realtime manager.send crashes on Omit in dict events. | ||
|
|
||
| These tests verify that the pre-connect send() methods on both sync and async | ||
| RealtimeConnectionManager properly transform Omit values before JSON serialization, | ||
| matching the behavior of the connected send() methods. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from openai._types import omit | ||
| from openai.resources.realtime.realtime import ( | ||
| AsyncRealtimeConnectionManager, | ||
| RealtimeConnectionManager, | ||
| ) | ||
|
|
||
|
|
||
| def _make_sync_manager() -> RealtimeConnectionManager: | ||
| """Create a minimal RealtimeConnectionManager for testing.""" | ||
| return RealtimeConnectionManager( | ||
| client=MagicMock(), | ||
| call_id="test-call-id", | ||
| model="gpt-4o-realtime-preview", | ||
| extra_query={}, | ||
| extra_headers={}, | ||
| websocket_connection_options=MagicMock(), | ||
| ) | ||
|
|
||
|
|
||
| def _make_async_manager() -> AsyncRealtimeConnectionManager: | ||
| """Create a minimal AsyncRealtimeConnectionManager for testing.""" | ||
| return AsyncRealtimeConnectionManager( | ||
| client=MagicMock(), | ||
| call_id="test-call-id", | ||
| model="gpt-4o-realtime-preview", | ||
| extra_query={}, | ||
| extra_headers={}, | ||
| websocket_connection_options=MagicMock(), | ||
| ) | ||
|
|
||
|
|
||
| class TestRealtimeConnectionManagerSendOmit: | ||
| """Test that RealtimeConnectionManager.send() handles Omit values.""" | ||
|
|
||
| def test_send_dict_with_omit_strips_omit_values(self) -> None: | ||
| """Pre-connect send() should strip Omit values from dict events before JSON serialization.""" | ||
| manager = _make_sync_manager() | ||
|
|
||
| event = {"type": "response.cancel", "event_id": omit} | ||
|
|
||
| # This should NOT raise TypeError: Object of type Omit is not JSON serializable | ||
| manager.send(event) | ||
|
|
||
| # Verify the queued data has Omit stripped | ||
| items = manager._RealtimeConnectionManager__send_queue.drain() | ||
| assert len(items) == 1 | ||
| data = json.loads(items[0]) | ||
| assert data == {"type": "response.cancel"} | ||
| assert "event_id" not in data | ||
|
|
||
| def test_send_dict_without_omit_preserves_all_fields(self) -> None: | ||
| """Pre-connect send() should preserve all fields when no Omit values present.""" | ||
| manager = _make_sync_manager() | ||
|
|
||
| event = {"type": "response.cancel", "event_id": "evt_123"} | ||
| manager.send(event) | ||
|
|
||
| items = manager._RealtimeConnectionManager__send_queue.drain() | ||
| assert len(items) == 1 | ||
| data = json.loads(items[0]) | ||
| assert data == {"type": "response.cancel", "event_id": "evt_123"} | ||
|
|
||
| def test_send_dict_with_multiple_omit_fields(self) -> None: | ||
| """Pre-connect send() should strip all Omit values from dict events.""" | ||
| manager = _make_sync_manager() | ||
|
|
||
| event = {"type": "response.create", "event_id": omit, "response": {"modalities": omit}} | ||
| manager.send(event) | ||
|
|
||
| items = manager._RealtimeConnectionManager__send_queue.drain() | ||
| assert len(items) == 1 | ||
| data = json.loads(items[0]) | ||
| assert data == {"type": "response.create", "response": {}} | ||
|
|
||
|
|
||
| class TestAsyncRealtimeConnectionManagerSendOmit: | ||
| """Test that AsyncRealtimeConnectionManager.send() handles Omit values.""" | ||
|
|
||
| def test_send_dict_with_omit_strips_omit_values(self) -> None: | ||
| """Pre-connect send() should strip Omit values from dict events before JSON serialization.""" | ||
| manager = _make_async_manager() | ||
|
|
||
| event = {"type": "response.cancel", "event_id": omit} | ||
|
|
||
| # This should NOT raise TypeError: Object of type Omit is not JSON serializable | ||
| manager.send(event) | ||
|
|
||
| # Verify the queued data has Omit stripped | ||
| items = manager._AsyncRealtimeConnectionManager__send_queue.drain() | ||
| assert len(items) == 1 | ||
| data = json.loads(items[0]) | ||
| assert data == {"type": "response.cancel"} | ||
| assert "event_id" not in data | ||
|
|
||
| def test_send_dict_without_omit_preserves_all_fields(self) -> None: | ||
| """Pre-connect send() should preserve all fields when no Omit values present.""" | ||
| manager = _make_async_manager() | ||
|
|
||
| event = {"type": "response.cancel", "event_id": "evt_123"} | ||
| manager.send(event) | ||
|
|
||
| items = manager._AsyncRealtimeConnectionManager__send_queue.drain() | ||
| assert len(items) == 1 | ||
| data = json.loads(items[0]) | ||
| assert data == {"type": "response.cancel", "event_id": "evt_123"} | ||
|
|
||
| def test_send_dict_with_multiple_omit_fields(self) -> None: | ||
| """Pre-connect send() should strip all Omit values from dict events.""" | ||
| manager = _make_async_manager() | ||
|
|
||
| event = {"type": "response.create", "event_id": omit, "response": {"modalities": omit}} | ||
| manager.send(event) | ||
|
|
||
| items = manager._AsyncRealtimeConnectionManager__send_queue.drain() | ||
| assert len(items) == 1 | ||
| data = json.loads(items[0]) | ||
| assert data == {"type": "response.create", "response": {}} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This nested omit case still raises before the assertions because
modalitiesis not a field in the currentRealtimeResponseCreateParamsParam(the typed field isoutput_modalities), and_transform_typeddictpreserves unknown keys instead of stripping theiromitvalues. As written,maybe_transformleavesresponse.modalities = omit, sojson.dumps(...)fails withTypeErrorand the new regression test suite fails; the async copy below has the same issue.Useful? React with 👍 / 👎.