python
runtime_error
ai_generated
true
RuntimeError: Response content longer than Content-Length
ID: python/starlette-response-content-length
80%Fix Rate
82%Confidence
0Evidence
2025-05-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
100% success
Don't set Content-Length manually; let Response handle it: return Response(content=body)
-
90% success
If you must set it, compute len(body) and set header accordingly
Dead Ends
Common approaches that don't work:
-
80% fail
If the body changes, the length becomes stale; better to let Starlette compute it.
-
60% fail
Starlette may still check; you need to ensure the stream sends exactly the right bytes.