-
Notifications
You must be signed in to change notification settings - Fork 308
Add WebSocket transport for OpenAI Responses API streaming #2186
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
dgageot
wants to merge
4
commits into
docker:main
Choose a base branch
from
dgageot:ws
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a7599e5
Add WebSocket transport for OpenAI Responses API streaming
dgageot 88656e0
Address PR #2186 review feedback for WebSocket pool
dgageot 970b1ba
Simplify WebSocket pool code structure
dgageot 317b902
Fix data race on wsPool lazy init and minor issues
dgageot 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
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,42 @@ | ||
| #!/usr/bin/env docker agent run | ||
|
|
||
| # Example: WebSocket Transport for OpenAI Responses API | ||
| # | ||
| # This example demonstrates how to use WebSocket streaming instead of | ||
| # Server-Sent Events (SSE) for the OpenAI Responses API. | ||
| # | ||
| # WebSocket transport maintains a persistent connection across tool-call | ||
| # rounds, reducing per-turn overhead and improving end-to-end latency | ||
| # for agentic workflows with many tool calls. | ||
| # | ||
| # Benefits of WebSocket over SSE: | ||
| # - ~40% faster end-to-end execution for workflows with 20+ tool calls | ||
| # - Persistent connection reduces per-turn continuation overhead | ||
| # - Connection-local state caching on the server | ||
| # - Falls back to SSE automatically if WebSocket connection fails | ||
| # | ||
| # Requirements: | ||
| # - Works only with OpenAI Responses API models (gpt-4.1+, o-series, gpt-5) | ||
| # - Requires OPENAI_API_KEY environment variable (or use token_key) | ||
| # - NOT compatible with --gateway flag (automatically falls back to SSE) | ||
| # | ||
| # Run with: | ||
| # docker agent run websocket_transport.yaml | ||
|
|
||
| models: | ||
| gpt-ws: | ||
| provider: openai | ||
| model: gpt-4.1 | ||
| provider_opts: | ||
| transport: websocket # Use WebSocket instead of SSE | ||
|
|
||
| agents: | ||
| root: | ||
| model: gpt-ws | ||
| description: Assistant using WebSocket streaming | ||
| instruction: | | ||
| You are a helpful assistant. Answer questions concisely. | ||
| toolsets: | ||
| - type: shell # Real toolset for demonstrating multi-turn tool calls | ||
| commands: | ||
| demo: "List the files in the current directory, then count how many are YAML files" |
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,23 @@ | ||
| package openai | ||
|
|
||
| import "github.com/openai/openai-go/v3/responses" | ||
|
|
||
| // responseEventStream abstracts over SSE and WebSocket transports for | ||
| // streaming Responses API events. | ||
| // | ||
| // The ssestream.Stream[responses.ResponseStreamEventUnion] type already | ||
| // satisfies this interface, so it can be used directly. | ||
| type responseEventStream interface { | ||
| // Next advances the stream to the next event. | ||
| // Returns false when the stream is exhausted or an error occurred. | ||
| Next() bool | ||
|
|
||
| // Current returns the most recently decoded event. | ||
| Current() responses.ResponseStreamEventUnion | ||
|
|
||
| // Err returns the first non-EOF error encountered by the stream. | ||
| Err() error | ||
|
|
||
| // Close releases resources held by the stream. | ||
| Close() error | ||
| } |
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
Oops, something went wrong.
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.
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.
🔴 CRITICAL: Resource Leak - WebSocket connections never closed
The
wsPoolfield is initialized increateWebSocketStream()but theClientstruct has noClose()method. This means WebSocket connections are never properly closed and will leak.Impact:
Recommendation:
Add a
Close()method to theClientstruct that callswsPool.Close()if the pool exists.