Repository navigation
test(realtime): cover typed reference input serialization #4223
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
Merged
markstuart-oai
merged 2 commits into
main
from
markstuart/realtime-reference-input-proof
Nov 2, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import asyncio | ||
| import threading | ||
|
|
||
| import httpx2 | ||
| import pytest | ||
| from websockets.sync.server import ServerConnection | ||
|
|
||
| from openai import OpenAI, AsyncOpenAI | ||
| from openai.types.realtime.conversation_item_param import ConversationItemParam | ||
| from openai.types.realtime.response_item_reference_param import ResponseItemReferenceParam | ||
| from openai.types.realtime.response_create_event_with_references_param import ResponseCreateEventWithReferencesParam | ||
| from openai.types.realtime.realtime_response_create_params_with_references_param import ( | ||
| RealtimeResponseCreateParamsWithReferencesParam, | ||
| ) | ||
|
|
||
| from .responses.test_websocket_session import script_server | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def reference_response() -> RealtimeResponseCreateParamsWithReferencesParam: | ||
| reference: ResponseItemReferenceParam = {"id": "item_existing", "type": "item_reference"} | ||
| message: ConversationItemParam = { | ||
| "type": "message", | ||
| "role": "user", | ||
| "content": [{"type": "input_text", "text": "new input 東京"}], | ||
| } | ||
| return { | ||
| "conversation": "none", | ||
| "input": [reference, message], | ||
| "instructions": "Use both inputs.", | ||
| "metadata": {"case": "reference-input"}, | ||
| "output_modalities": ["text"], | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("mode", ["sync", "async"]) | ||
| @pytest.mark.parametrize("operation", ["create", "send"]) | ||
| async def test_realtime_reference_inputs_preserve_mixed_wire_payload( | ||
| mode: str, | ||
| operation: str, | ||
| reference_response: RealtimeResponseCreateParamsWithReferencesParam, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| monkeypatch.setenv("NO_PROXY", "127.0.0.1") | ||
| monkeypatch.setenv("no_proxy", "127.0.0.1") | ||
| event: ResponseCreateEventWithReferencesParam = { | ||
| "type": "response.create", | ||
| "event_id": "fake-reference-request", | ||
| "response": reference_response, | ||
| } | ||
|
|
||
| validated = threading.Event() | ||
|
|
||
| def script(socket: ServerConnection) -> None: | ||
| wire = socket.recv(timeout=5) | ||
| assert isinstance(wire, str) | ||
| assert json.loads(wire) == { | ||
| "type": "response.create", | ||
| "event_id": "fake-reference-request", | ||
| "response": { | ||
| "conversation": "none", | ||
| "input": [ | ||
| {"id": "item_existing", "type": "item_reference"}, | ||
| { | ||
| "type": "message", | ||
| "role": "user", | ||
| "content": [{"type": "input_text", "text": "new input 東京"}], | ||
| }, | ||
| ], | ||
| "instructions": "Use both inputs.", | ||
| "metadata": {"case": "reference-input"}, | ||
| "output_modalities": ["text"], | ||
| }, | ||
| } | ||
| validated.set() | ||
|
|
||
| with script_server(script) as url: | ||
| if mode == "sync": | ||
| with OpenAI( | ||
| api_key="fake-reference-key", base_url=url, http_client=httpx2.Client(trust_env=False) | ||
| ) as client: | ||
| with client.realtime.connect(model="gpt-realtime") as connection: | ||
| if operation == "create": | ||
| connection.response.create(event_id="fake-reference-request", response=reference_response) | ||
| else: | ||
| connection.send(event) | ||
| assert validated.wait(5), "Peer did not validate the request" | ||
| else: | ||
| async with AsyncOpenAI( | ||
| api_key="fake-reference-key", base_url=url, http_client=httpx2.AsyncClient(trust_env=False) | ||
| ) as async_client: | ||
| async with async_client.realtime.connect(model="gpt-realtime") as async_connection: | ||
| if operation == "create": | ||
| await asyncio.wait_for( | ||
| async_connection.response.create( | ||
| event_id="fake-reference-request", response=reference_response | ||
| ), | ||
| timeout=5, | ||
| ) | ||
| else: | ||
| await asyncio.wait_for(async_connection.send(event), timeout=5) | ||
| assert await asyncio.to_thread(validated.wait, 5), "Peer did not validate the request" | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.