python runtime_error ai_generated true

RuntimeError: File response is already started

ID: python/fastapi-file-response-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2026-03-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

尝试在响应已经开始发送后再次修改或发送文件。

generic

中文

尝试在响应已经开始发送后再次修改或发送文件。

Workarounds

  1. 95% success
    确保只返回一次 `FileResponse`,不要在返回后修改。
  2. 85% success
    使用 `StreamingResponse` 并正确管理生成器生命周期。

Dead Ends

Common approaches that don't work:

  1. 70% fail

    尝试在返回 `FileResponse` 后继续写入响应,导致错误。

  2. 60% fail

    尝试在流式响应中使用 `yield` 但未正确管理生成器。