","error":{"type":"server_error","code":"upstream_stream_truncated","message":"u...
错误信息
{"type":"error","error":{"type":"server_error","code":"upstream_stream_truncated","message":"upstream stream ended before a completion event; this turn may be incomplete","meta":{"cause":"eof","classification":"no-terminal-seen","protocol":"anthropic","events":2,"unparseableForwarded":0,"bytes":230,"lastEventTypes":["ping","error"],"eofBufferBytes":0,"visibleChars":0,"sawStrippedEcho":false,"retryZeroByteCutSpent":false,"retryDegenerateSpent":false,"upstreamStatus":200,"upstreamContentType":"text/event-stream"}}}
AI 解决方案
原文:upstream stream ended before a completion event; this turn may be incomplete 译文:上游流在完成事件前已结束;本次对话可能不完整
🎯 原因分析
该错误为服务器端错误,表示与上游服务(Anthropic API)的流式连接在收到完整响应前被异常中断(由EOF触发)。可能原因包括:网络不稳定、上游服务负载过高、网关超时,或请求触发了防护策略。
💡 解决方案
``
代码语言
// 重试实现示例(伪代码)
async function callWithRetry() {
const maxRetries = 3;
let retries = 0; while (retries < maxRetries) {
try {
const response = await fetch(yourUrl, { / 流式请求配置 / });
if (response.ok) return await processStream(response);
} catch (error) {
if (error.type === 'server_error' && retries < maxRetries) {
await delay(Math.pow(2, retries) * 1000);
retries++;
continue;
}
throw error;
}
}
}
``