# AssertionError: Response body already sent

- **ID:** `python/fastapi-assertionerror-response-body-already-sent`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Trying to write to a response after it has already been sent to the client (e.g., in middleware or background task).

## Version Compatibility

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

## Workarounds

1. **Set headers before returning the response** (90% success)
   ```
   response = JSONResponse(content=data, headers={'X-Custom': 'value'})
return response
   ```
2. **Use background tasks to modify response before send** (85% success)
   ```
   from fastapi import BackgroundTasks
background_tasks.add_task(some_func)
   ```

## Dead Ends

- **Adding response headers after returning** — Response is immutable after being sent. (80% fail)
- **Using yield in streaming response incorrectly** — Multiple yields after stream ends cause assertion error. (70% fail)
