OpenAI API Timeout: Diagnose Slow or Interrupted Streams
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
OpenAI API Timeout: Diagnose Slow or Interrupted Streams
Originally published by Tokenhot Team on 2026-09-29. The DEV edition was formatted with AI assistance. An openai api timeout says your client stopped waiting; it does not prove whether OpenAI received, accepted, or completed the POST. Diagnose it by saving the status and request ID if headers arrived, recording the last Responses API event seen, and separating HTTP success from application-level completion. If no documented completion event arrived, preserve the result as incomplete or unknown and do not automatically replay the POST.
由 Tokenhot 团队于 2026 年 9 月 29 日首次发布。DEV 版本经过 AI 辅助格式化。OpenAI API 超时意味着您的客户端停止了等待;但这并不能证明 OpenAI 是否接收、接受或完成了该 POST 请求。诊断方法是:如果收到了响应头,请保存状态码和请求 ID,记录最后看到的 Responses API 事件,并将 HTTP 成功与应用层面的完成区分开来。如果没有收到已记录的完成事件,请将结果保留为“不完整”或“未知”,且不要自动重发该 POST 请求。
This guide uses a direct HTTP request to OpenAI’s Responses API, so it can show the actual response and transport boundaries without relying on methods that vary between SDK releases. The same diagnosis applies conceptually to other clients, but their timeout controls, error objects, request IDs, and stream events can differ. OpenAI-compatible gateways also need their own endpoint and event documentation; the script below is for api.openai.com only.
本指南使用对 OpenAI Responses API 的直接 HTTP 请求,因此它可以显示实际的响应和传输边界,而无需依赖在不同 SDK 版本间存在差异的方法。同样的诊断逻辑在概念上也适用于其他客户端,但它们的超时控制、错误对象、请求 ID 和流事件可能会有所不同。兼容 OpenAI 的网关也需要其各自的端点和事件文档;下方的脚本仅适用于 api.openai.com。
What a timeout tells you
Timeouts describe a wait limit. A configured limit is not a measured phase duration, and a timeout after the request may have been written does not prove the service did no work. Keep three kinds of evidence separate:
超时告诉了你什么
超时描述的是一种等待限制。配置的限制并非测量的阶段持续时间,且在请求可能已写入后发生的超时,并不能证明服务没有进行任何工作。请将以下三类证据分开:
| Boundary | Example setting or observation | What it establishes |
|---|---|---|
| Connection-pool wait and connection setup | aiohttp connect limit; optional transport tracing | A connection could not be acquired or established before the configured wait expired. connect covers both pool waiting and connection establishment; timestamps alone do not separate those subphases. |
| Socket connection | sock_connect limit | Time allowed to connect to a peer for a new connection. It is a limit, not an observed DNS/TCP/TLS breakdown. |
| Request write | Transport tracing, if available | This example has no distinct write-timeout field. The total application deadline still bounds the whole operation. A write failure can leave the server outcome uncertain. |
| Response headers | headers_received timestamp, HTTP status, x-request-id if supplied | An HTTP response arrived. It does not prove the stream or model response completed. |
| Incoming bytes | sock_read limit | aiohttp waited too long for the next data portion. Bytes may arrive without completing an SSE event, so this is not an “event idle” timer. |
| Total operation | Separate asyncio.timeout deadline | Your application ended the operation after a wall-clock budget. The deadline is not an HTTP status. |
| Responses API terminal event | Last event type and time | response.completed documents a completed response; response.incomplete, response.failed, or error records a different application outcome. |
| 边界 | 示例设置或观察 | 确立的内容 |
|---|---|---|
| 连接池等待与连接建立 | aiohttp connect limit; 可选的传输追踪 | 在配置的等待时间过期前,无法获取或建立连接。connect 涵盖了池等待和连接建立;仅凭时间戳无法区分这两个子阶段。 |
| Socket 连接 | sock_connect limit | 允许连接到对等方以建立新连接的时间。这是一个限制,而非观察到的 DNS/TCP/TLS 故障。 |
| 请求写入 | 传输追踪(如果可用) | 本示例没有单独的写入超时字段。总的应用截止时间仍然限制了整个操作。写入失败可能会导致服务器结果不确定。 |
| 响应头 | headers_received 时间戳,HTTP 状态码,x-request-id(如果提供) | HTTP 响应已到达。这并不能证明流或模型响应已完成。 |
| 传入字节 | sock_read limit | aiohttp 等待下一个数据片段的时间过长。字节可能在未完成 SSE 事件的情况下到达,因此这不是一个“事件空闲”计时器。 |
| 总操作 | 独立的 asyncio.timeout 截止时间 | 您的应用程序在挂钟时间预算后结束了操作。截止时间不是 HTTP 状态。 |
| Responses API 终止事件 | 最后事件类型和时间 | response.completed 记录了已完成的响应;response.incomplete、response.failed 或 error 记录了不同的应用结果。 |
The aiohttp timeout documentation defines connect as covering connection establishment or waiting for an available pool connection, sock_connect as the new peer connection wait, and sock_read as the wait between data portions. Use TraceConfig or your transport’s supported hooks if you need observed pool, DNS, TLS, or write timings. Do not infer them from timeout settings.
aiohttp 超时文档将 connect 定义为涵盖连接建立或等待可用池连接,将 sock_connect 定义为新的对等连接等待,将 sock_read 定义为数据片段之间的等待。如果您需要观察到的池、DNS、TLS 或写入时间,请使用 TraceConfig 或您的传输层支持的钩子。不要从超时设置中推断这些时间。
Read HTTP status and stream completion separately
A response can have HTTP status 200 and still fail to produce a complete answer. The OpenAI streaming guide describes the Responses API as server-sent events (SSE). Its event reference documents response.completed, response.incomplete, and response.failed; the stream can also emit error. Use these distinctions when triaging:
分别读取 HTTP 状态和流完成情况
响应可能具有 HTTP 200 状态码,但仍未能产生完整的答案。OpenAI 流式传输指南将 Responses API 描述为服务器发送事件 (SSE)。其事件参考文档记录了 response.completed、response.incomplete 和 response.failed;流也可能发出 error。在分类诊断时,请使用这些区别:
-
No headers: there is no observed status or response request ID. If the POST might have been written, the server-side outcome is unknown.
-
Non-2xx headers: keep the HTTP status and request ID. The example does not read or log an error body, because that body can include private details. Follow the endpoint’s error documentation separately.
-
2xx headers, then response.completed: the API reported completion. A later local cleanup or connection error does not erase that observed event; record both facts.
-
response.incomplete, response.failed, or error: record the explicit event type. Do not treat it as a completed answer.
-
EOF or transport error without a terminal event: retain any partial output in your application’s protected state, but treat the result as incomplete or unknown. A broken connection cannot establish what happened after the last event you received.
-
无响应头: 没有观察到状态码或响应请求 ID。如果 POST 可能已写入,则服务器端的结果是未知的。
-
非 2xx 响应头: 保留 HTTP 状态码和请求 ID。本示例不读取或记录错误正文,因为该正文可能包含隐私细节。请单独遵循端点的错误文档。
-
2xx 响应头,随后是 response.completed: API 报告已完成。后续的本地清理或连接错误不会抹除该已观察到的事件;请记录这两个事实。
-
response.incomplete、response.failed 或 error: 记录明确的事件类型。不要将其视为已完成的答案。
-
EOF 或无终止事件的传输错误: 在应用程序的受保护状态中保留任何部分输出,但将结果视为不完整或未知。断开的连接无法确定在您收到的最后一个事件之后发生了什么。
An SSE event is dispatched only when its blank-line separator arrives. If EOF occurs after a data: line but before that separator, discard the pending frame; it is not an observed response.completed event. The HTML Standard’s SSE processing rules specify that pending data is discarded at end of file. The diagnostic records an unterminated_sse_frame flag without promoting that frame to the last event. A direct connection close after text deltas is not the same thing as a documented terminal event. Avoid parsing partial JSON, invoking tools from an unfinished response, or automatically replaying a POST with potentially duplicated side effects. Check the endpoint’s documented reconciliation or idempotency mechanism before deciding whether a retry is safe.
SSE 事件仅在到达其空行分隔符时才会被分发。如果 EOF 发生在 data: 行之后但在该分隔符之前,请丢弃挂起的帧;它不是观察到的 response.completed 事件。HTML 标准的 SSE 处理规则规定,挂起的数据在文件末尾会被丢弃。诊断程序会记录一个 unterminated_sse_frame 标志,而不会将该帧提升为最后一个事件。文本增量后的直接连接关闭与已记录的终止事件不是一回事。避免解析部分 JSON、从未完成的响应中调用工具,或自动重发可能具有重复副作用的 POST 请求。在决定重试是否安全之前,请检查端点文档中关于协调或幂等性的机制。
Run a single controlled reproduction
The runnable example below targets the OpenAI Responses API directly. It makes one POST, disables redirects, does not implement retries, and logs event names rather than response text. Running it contacts OpenAI and can incur usage charges. Use an authorized key and an exact model ID from your account; the code itself has not been run against a live API. Download timeout_probe.py. Use Python 3.11 or later and the exact aiohttp version validated for this script.
运行一次受控的复现
下方可运行的示例直接针对 OpenAI Responses API。它执行一次 POST 请求,禁用重定向,不实现重试,并记录事件名称而非响应文本。运行它会联系 OpenAI 并可能产生使用费用。请使用您账户中的授权密钥和确切的模型 ID;代码本身尚未在实时 API 上运行。下载 timeout_probe.py。请使用 Python 3.11 或更高版本以及为此脚本验证过的确切 aiohttp 版本。
Save the following code as timeout_probe.py:
python3.11 -m venv .venv
. .venv/bin/activate
python -m pip install 'aiohttp==3.13.5'
Set OPENAI_API_KEY through your shell’s secret manager or secure environment injection, and set OPENAI_MODEL to an available model ID. Avoid putting the key literal in shell history, source control, logs, or screenshots. Then run python timeout_probe.py.
将以下代码保存为 timeout_probe.py:
python3.11 -m venv .venv
. .venv/bin/activate
python -m pip install 'aiohttp==3.13.5'
通过 shell 的密钥管理器或安全环境变量注入设置 OPENAI_API_KEY,并将 OPENAI_MODEL 设置为可用的模型 ID。避免将密钥明文放入 shell 历史记录、源代码管理、日志或截图中。然后运行 python timeout_probe.py。
"""One-request OpenAI Responses stream diagnostic. See article setup and limits.""" import as """单次请求 OpenAI Responses 流诊断。请参阅文章设置和限制。""" import as