{
  "id": "python/fastapi-assertionerror-response-body-already-sent",
  "signature": "AssertionError: Response body already sent",
  "signature_zh": "断言错误：响应体已发送",
  "regex": "AssertionError:\\ Response\\ body\\ already\\ sent",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Trying to write to a response after it has already been sent to the client (e.g., in middleware or background task).",
  "root_cause_type": "generic",
  "root_cause_zh": "在响应已经发送给客户端后尝试写入响应（例如在中间件或后台任务中）。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Adding response headers after returning",
      "why_fails": "Response is immutable after being sent.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using yield in streaming response incorrectly",
      "why_fails": "Multiple yields after stream ends cause assertion error.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Set headers before returning the response",
      "success_rate": 0.9,
      "how": "response = JSONResponse(content=data, headers={'X-Custom': 'value'})\nreturn response",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use background tasks to modify response before send",
      "success_rate": 0.85,
      "how": "from fastapi import BackgroundTasks\nbackground_tasks.add_task(some_func)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-09-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}