python runtime_error ai_generated true

RuntimeError: Stream consumed

ID: python/starlette-middleware-body-consumed

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-12-04First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

Custom middleware read the request body via await request.body() and downstream handler tried to read it again; Starlette streams can only be consumed once.

generic

中文

自定义中间件通过 await request.body() 读取了请求体,下游处理程序再次读取;Starlette 流只能消费一次。

Workarounds

  1. 85% success
    In middleware: body = await request.body(); request._body = body (cache it)
  2. 90% success
    Use request.state to pass parsed data instead of re-reading body

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Second call raises Stream consumed.

  2. 75% fail

    Handler receives empty body and fails validation.