Skip to content

Commit ac8fc9d

Browse files
authored
docs: add responses websocket support (#2533)
1 parent e550663 commit ac8fc9d

7 files changed

Lines changed: 124 additions & 0 deletions

File tree

docs/examples.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Check out a variety of sample implementations of the SDK in the examples section
2323
- Agent lifecycle management
2424
- Dynamic system prompts
2525
- Streaming outputs (text, items, function call args)
26+
- Responses websocket transport with a shared session helper across turns (`examples/basic/stream_ws.py`)
2627
- Prompt templates
2728
- File handling (local and remote, images and PDFs)
2829
- Usage tracking

docs/models/index.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,45 @@ For lower latency, using `reasoning.effort="none"` with `gpt-5.2` is recommended
6161

6262
If you pass a non–GPT-5 model name without custom `model_settings`, the SDK reverts to generic `ModelSettings` compatible with any model.
6363

64+
### Responses WebSocket transport
65+
66+
By default, OpenAI Responses API requests use HTTP transport. You can opt in to websocket transport when using OpenAI-backed models.
67+
68+
```python
69+
from agents import set_default_openai_responses_transport
70+
71+
set_default_openai_responses_transport("websocket")
72+
```
73+
74+
This affects OpenAI Responses models resolved by the default OpenAI provider (including string model names such as `"gpt-5.2"`).
75+
76+
You can also configure websocket transport per provider or per run:
77+
78+
```python
79+
from agents import Agent, OpenAIProvider, RunConfig, Runner
80+
81+
provider = OpenAIProvider(
82+
use_responses_websocket=True,
83+
# Optional; if omitted, OPENAI_WEBSOCKET_BASE_URL is used when set.
84+
websocket_base_url="wss://your-proxy.example/v1",
85+
)
86+
87+
agent = Agent(name="Assistant")
88+
result = await Runner.run(
89+
agent,
90+
"Hello",
91+
run_config=RunConfig(model_provider=provider),
92+
)
93+
```
94+
95+
If you need prefix-based model routing (for example mixing `openai/...` and `litellm/...` model names in one run), use [`MultiProvider`][agents.MultiProvider] and set `openai_use_responses_websocket=True` there instead.
96+
97+
Notes:
98+
99+
- This is the Responses API over websocket transport, not the [Realtime API](../realtime/guide.md).
100+
- Install the `websockets` package if it is not already available in your environment.
101+
- You can use [`Runner.run_streamed()`][agents.run.Runner.run_streamed] directly after enabling websocket transport. For multi-turn workflows where you want to reuse the same websocket connection across turns (and nested agent-as-tool calls), the [`responses_websocket_session()`][agents.responses_websocket_session] helper is recommended. See the [Running agents](../running_agents.md) guide and [`examples/basic/stream_ws.py`](https://github.com/openai/openai-agents-python/tree/main/examples/basic/stream_ws.py).
102+
64103
## Non-OpenAI models
65104

66105
You can use most other non-OpenAI models via the [LiteLLM integration](./litellm.md). First, install the litellm dependency group:

docs/ref/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
- set_default_openai_key
88
- set_default_openai_client
99
- set_default_openai_api
10+
- set_default_openai_responses_transport
11+
- ResponsesWebSocketSession
12+
- responses_websocket_session
1013
- set_tracing_export_api_key
1114
- set_tracing_disabled
1215
- set_trace_processors
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `Responses WebSocket Session`
2+
3+
::: agents.responses_websocket_session

docs/release.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ We will increment `Z` for non-breaking changes:
1919

2020
## Breaking change changelog
2121

22+
### 0.10.0
23+
24+
This minor release does **not** introduce a breaking change, but it includes a significant new feature area for OpenAI Responses users: websocket transport support for the Responses API.
25+
26+
Highlights:
27+
28+
- Added websocket transport support for OpenAI Responses models (opt-in; HTTP remains the default transport).
29+
- Added a `responses_websocket_session()` helper / `ResponsesWebSocketSession` for reusing a shared websocket-capable provider and `RunConfig` across multi-turn runs.
30+
- Added a new websocket streaming example (`examples/basic/stream_ws.py`) covering streaming, tools, approvals, and follow-up turns.
31+
2232
### 0.9.0
2333

2434
In this version, Python 3.9 is no longer supported, as this major version reached EOL three months ago. Please upgrade to a newer runtime version.

docs/running_agents.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,71 @@ The runner then runs a loop:
4242

4343
Streaming allows you to additionally receive streaming events as the LLM runs. Once the stream is done, the [`RunResultStreaming`][agents.result.RunResultStreaming] will contain the complete information about the run, including all the new outputs produced. You can call `.stream_events()` for the streaming events. Read more in the [streaming guide](streaming.md).
4444

45+
### Responses WebSocket transport (optional helper)
46+
47+
If you enable the OpenAI Responses websocket transport, you can keep using the normal `Runner` APIs. The websocket session helper is recommended for connection reuse, but it is not required.
48+
49+
This is the Responses API over websocket transport, not the [Realtime API](realtime/guide.md).
50+
51+
#### Pattern 1: No session helper (works)
52+
53+
Use this when you just want websocket transport and do not need the SDK to manage a shared provider/session for you.
54+
55+
```python
56+
import asyncio
57+
58+
from agents import Agent, Runner, set_default_openai_responses_transport
59+
60+
61+
async def main():
62+
set_default_openai_responses_transport("websocket")
63+
64+
agent = Agent(name="Assistant", instructions="Be concise.")
65+
result = Runner.run_streamed(agent, "Summarize recursion in one sentence.")
66+
67+
async for event in result.stream_events():
68+
if event.type == "raw_response_event":
69+
continue
70+
print(event.type)
71+
72+
73+
asyncio.run(main())
74+
```
75+
76+
This pattern is fine for single runs. If you call `Runner.run()` / `Runner.run_streamed()` repeatedly, each run may reconnect unless you manually reuse the same `RunConfig` / provider instance.
77+
78+
#### Pattern 2: Use `responses_websocket_session()` (recommended for multi-turn reuse)
79+
80+
Use [`responses_websocket_session()`][agents.responses_websocket_session] when you want a shared websocket-capable provider and `RunConfig` across multiple runs (including nested agent-as-tool calls that inherit the same `run_config`).
81+
82+
```python
83+
import asyncio
84+
85+
from agents import Agent, responses_websocket_session
86+
87+
88+
async def main():
89+
agent = Agent(name="Assistant", instructions="Be concise.")
90+
91+
async with responses_websocket_session() as ws:
92+
first = ws.run_streamed(agent, "Say hello in one short sentence.")
93+
async for _event in first.stream_events():
94+
pass
95+
96+
second = ws.run_streamed(
97+
agent,
98+
"Now say goodbye.",
99+
previous_response_id=first.last_response_id,
100+
)
101+
async for _event in second.stream_events():
102+
pass
103+
104+
105+
asyncio.run(main())
106+
```
107+
108+
Finish consuming streamed results before the context exits. Exiting the context while a websocket request is still in flight may force-close the shared connection.
109+
45110
## Run config
46111

47112
The `run_config` parameter lets you configure some global settings for the agent run:

mkdocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ plugins:
9494
- ref/run.md
9595
- ref/run_config.md
9696
- ref/run_state.md
97+
- ref/responses_websocket_session.md
9798
- ref/run_error_handlers.md
9899
- ref/memory.md
99100
- ref/repl.md
@@ -118,6 +119,8 @@ plugins:
118119
- ref/models/interface.md
119120
- ref/models/openai_chatcompletions.md
120121
- ref/models/openai_responses.md
122+
- ref/models/openai_provider.md
123+
- ref/models/multi_provider.md
121124
- ref/mcp/server.md
122125
- ref/mcp/util.md
123126
- Tracing:

0 commit comments

Comments
 (0)