{
  "id": "python/fastapi-async-sync-mixing-error",
  "signature": "RuntimeError: You cannot use async generator in sync context",
  "signature_zh": "运行时错误：不能在同步上下文中使用异步生成器",
  "regex": "RuntimeError:\\ You\\ cannot\\ use\\ async\\ generator\\ in\\ sync\\ context",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Mixing async and sync code incorrectly, such as using an async generator in a synchronous route or vice versa.",
  "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": "",
      "why_fails": "Adding asyncio.run() inside a sync route can cause event loop conflicts.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Converting an async generator to a list with list() works only in async context, not sync.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Define the route as async if you need to use async generators: @app.get('/')\\nasync def read_root():\\n    async for item in async_gen():\\n        yield item",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.8,
      "how": "For sync routes, use a sync generator or collect items with asyncio.run() in a separate function.",
      "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-08-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}