upstream stream ended without a terminal frame (no message_delta, no message_sto...
错误信息
API Error: upstream stream ended without a terminal frame (no message_delta, no message_stop); the response is truncated
AI 解决方案
原文:API Error: upstream stream ended without a terminal frame (no message_delta, no message_stop); the response is truncated
译文:API错误:上游流在没有终止帧(既无message_delta,也无message_stop)的情况下结束;响应被截断
🎯 原因分析
该错误表明 API 流式响应在未发送正常结束标记(message_delta 或 message_stop)时就被中断,导致客户端接收到不完整的响应,通常是由上游服务异常中断、网络连接不稳定或服务端主动终止连接所引发。
💡 解决方案
在调用端增加流式响应的完整性校验和重试逻辑,确保能识别异常截断并自动恢复,示例如下:
import httpx
def request_stream(url: str, payload: dict):
for attempt in range(3): # 最多重试 3 次
try:
with httpx.stream("POST", url, json=payload) as response:
response.raise_for_status()
received_end_frame = False
for line in response.iter_lines():
if line == "data: [DONE]" or line == "data: message_stop":
received_end_frame = True
break
# 未收到正常结束帧,主动重试
if not received_end_frame:
raise Exception("响应被截断,缺少终止帧")
return True
except Exception as e:
if attempt == 2: # 最后一次尝试仍失败,抛出异常
raise e
print(f"第{attempt + 1}次请求失败,正在重试:{e}")
🛡️ 预防措施
- 增加超时与重试机制:为网络请求设置合理的超时时间,并针对此类流式截断错误配置自动重试逻辑。
- 完善日志监控:在客户端记录截断的响应内容与请求 ID,便于结合服务端日志快速定位上游异常。