{
  "id": "python/starlette-response-headers-set-after-start",
  "signature": "RuntimeError: Response headers cannot be modified after the response has started",
  "signature_zh": "运行时错误：响应开始后无法修改响应头",
  "regex": "RuntimeError:\\ Response\\ headers\\ cannot\\ be\\ modified\\ after\\ the\\ response\\ has\\ started",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Trying to set or modify response headers after the response has already been sent to the client (e.g., in a background task or after yielding in a streaming response).",
  "root_cause_type": "generic",
  "root_cause_zh": "在响应已发送给客户端后尝试设置或修改响应头（如在后台任务或流式响应中yield之后）。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Modifying headers in a `finally` block after the response is sent still raises the error.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Using a middleware to modify headers after the endpoint has returned doesn't work because the response is already committed.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Set all headers before returning the response object, e.g., `response.headers['X-Custom'] = 'value'` before returning.",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "If using streaming, set headers in the initial response object before streaming begins.",
      "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.83,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-20",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}