# RuntimeError: You cannot use async generator in sync context

- **ID:** `python/fastapi-async-sync-mixing-error`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Mixing async and sync code incorrectly, such as using an async generator in a synchronous route or vice versa.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   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
   ```
2. **** (80% success)
   ```
   For sync routes, use a sync generator or collect items with asyncio.run() in a separate function.
   ```

## Dead Ends

- **** — Adding asyncio.run() inside a sync route can cause event loop conflicts. (80% fail)
- **** — Converting an async generator to a list with list() works only in async context, not sync. (70% fail)
