python runtime_error ai_generated true

RuntimeError: Response content longer than Content-Length

ID: python/starlette-response-content-length

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2025-05-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 60% fail

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