# RuntimeError: Response headers cannot be modified after the response has started

- **ID:** `python/starlette-response-headers-set-after-start`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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).

## Version Compatibility

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

## Workarounds

1. **** (95% success)
   ```
   Set all headers before returning the response object, e.g., `response.headers['X-Custom'] = 'value'` before returning.
   ```
2. **** (90% success)
   ```
   If using streaming, set headers in the initial response object before streaming begins.
   ```

## Dead Ends

- **** — Modifying headers in a `finally` block after the response is sent still raises the error. (90% fail)
- **** — Using a middleware to modify headers after the endpoint has returned doesn't work because the response is already committed. (85% fail)
