OpenAI Responses WebSocket 与四框架对比分析
Related topics: [[websocket-streaming-support]], [[streaming-comparison]]
概述
OpenAI 于 2025 年推出的 Responses WebSocket API (responses_websockets=2026-02-06) 代表了 LLM 流式通讯的最新标准。本文分析其核心设计,并与 kosong、republic、litai、pydantic-ai 四个框架进行对比。
1. OpenAI Responses WebSocket 核心设计
1.1 连接模型
# OpenAI WebSocket 连接模型
from openai import OpenAI
client = OpenAI()
# 建立持久 WebSocket 连接
with client.responses.connect(
extra_headers={"OpenAI-Beta": "responses_websockets=2026-02-06"}
) as connection:
# 在连接内执行多次交互
for turn in demo_turns:
result = run_turn(connection, ...)
关键特性:
- 长连接复用:单个 WebSocket 连接支持多轮对话
- 状态保持:
previous_response_id链式关联上下文 - 双向通讯:可以在流式过程中发送中断/控制信号
1.2 事件流模型
# OpenAI 的事件流处理方式
connection.response.create(
model=model,
input=input_payload,
stream=True,
previous_response_id=previous_response_id,
tools=tools,
tool_choice=tool_choice,
)
for event in connection:
# 细粒度事件类型
if event.type == "response.output_text.delta":
text_parts.append(event.delta)
elif event.type == "response.output_item.done":
if event.item.type == "function_call":
function_calls.append(...)
elif event.type == "response.done":
response_id = event.response.id
break
事件类型体系:
| 事件类型 | 说明 | 对应框架概念 |
|---|---|---|
response.output_text.delta | 文本片段 | kosong TextPart / pydantic-ai TextPartDelta |
response.output_item.done | 输出项完成 | pydantic-ai PartEndEvent |
response.function_call | 工具调用 | republic tool_call / pydantic-ai ToolCallPart |
response.done | 响应完成 | republic final / pydantic-ai FinalResultEvent |
error | 错误 | 所有框架的错误类型 |
1.3 工具调用流式处理
# OpenAI 的流式工具调用循环
while True:
# 1. 发送请求(可能是文本或工具输出)
connection.response.create(...)
# 2. 迭代接收事件
for event in connection:
if event.type == "response.output_text.delta":
# 收集文本片段
elif event.type == "response.output_item.done" and event.item.type == "function_call":
# 3. 收集工具调用请求
function_calls.append(...)
elif event.type in ("response.completed", "response.done"):
response_id = event.response.id
break
# 4. 如果有工具调用,执行工具并循环
if function_calls:
tool_outputs = execute_tools(function_calls)
input_payload = tool_outputs # 下一轮输入是工具输出
tool_choice = "none" # 强制模型处理工具结果
continue
break # 没有工具调用,结束
关键设计:
- 同一连接内循环处理多轮(文本 → 工具调用 → 工具结果 → 文本)
previous_response_id自动维护对话上下文tool_choice控制模型行为(强制调用/禁止调用/自动)
2. 四框架与 OpenAI WebSocket 的对比
2.1 连接模型对比
┌─────────────────────────────────────────────────────────────────────┐
│ 连接模型对比 │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ OpenAI WebSocket │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ WS Connection │ │
│ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │
│ │ │ Turn 1 │→ │ Turn 2 │→ │ Turn 3 │ ... │ │
│ │ └─────────┘ └─────────┘ └─────────┘ │ │
│ │ ↑ previous_response_id 链式关联 │ │
│ └─────────────────────────────────────────────────────────┘ │
│ 长连接,状态保持 │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ kosong / pydantic-ai / republic / litai │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ HTTP 1 │ │ HTTP 2 │ │ HTTP 3 │ ... │
│ └─────────┘ └─────────┘ └─────────┘ │
│ ↑ history 数组传递上下文 │
│ 短连接,无状态 │
└─────────────────────────────────────────────────────────────────────┘
| 特性 | OpenAI WS | 四框架 HTTP |
|---|---|---|
| 连接方式 | 长连接 WebSocket | 短连接 HTTP/HTTPS |
| 状态管理 | previous_response_id | history 数组 |
| 上下文传递 | 服务端自动维护 | 客户端显式传递 |
| 中断能力 | 原生支持(发送 cancel 信号) | 依赖 HTTP 取消 |
| 延迟 | 低(无连接建立开销) | 高(每次握手) |