python runtime_error ai_generated true

运行时错误:响应内容长度超过 Content-Length

RuntimeError: Response content longer than Content-Length

ID: python/starlette-response-content-length

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
0证据数
2025-05-22首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

响应体大小与 Content-Length 头不匹配,通常是由于手动设置头导致。

English

The response body size doesn't match the Content-Length header, often due to manual header setting.

generic

解决方案

  1. 100% 成功率
    Don't set Content-Length manually; let Response handle it: return Response(content=body)
  2. 90% 成功率
    If you must set it, compute len(body) and set header accordingly

无效尝试

常见但无效的做法:

  1. 80% 失败

    If the body changes, the length becomes stale; better to let Starlette compute it.

  2. 60% 失败

    Starlette may still check; you need to ensure the stream sends exactly the right bytes.