# 错误：流式响应块顺序不匹配 - 期望索引 5 但收到 7

- **ID:** `llm/streaming-chunk-order-mismatch`
- **领域:** llm
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

在使用 SSE 流式传输进行并行处理或负载均衡时，由于网络延迟或服务器端并发，块可能乱序到达，导致客户端错误地重新组装响应。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| openai==1.23.0 | active | — | — |
| gpt-4-turbo-2024-04-09 | active | — | — |
| gpt-4o-2024-05-13 | active | — | — |

## 解决方案

1. ```
   实现一个缓冲区，根据序列号对块进行重新排序后组装：`buffer[chunk.index] = chunk; while buffer[next_index] is not None: yield buffer[next_index]; next_index++`
   ```
2. ```
   使用单线程 SSE 客户端，或确保 API 端点不在会重新排序请求的负载均衡器后面。
   ```
3. ```
   如果使用代理或 CDN，请绕过流式端点以减少重新排序的风险。
   ```

## 无效尝试

- **** — Timeout doesn't fix ordering; out-of-order chunks will still be out of order regardless of wait time. (60% 失败率)
- **** — This works as a workaround but defeats the purpose of streaming for real-time applications. (30% 失败率)
