# RuntimeError: ContextVar 'context' already exists

- **ID:** `python/fastapi-runtimeerror-starlette-context-already-exists`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Duplicate declaration of a ContextVar in the same scope, often due to middleware or dependency injection conflicts.

## Version Compatibility

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

## Workarounds

1. **Remove duplicate ContextVar declaration** (90% success)
   ```
   Ensure only one declaration: from contextvars import ContextVar
context = ContextVar('context', default=None)
   ```
2. **Use a single ContextVar across modules** (85% success)
   ```
   Create a shared module for ContextVar and import it.
   ```

## Dead Ends

- **Ignoring the error and retrying** — ContextVar conflict persists until code is fixed. (90% fail)
- **Using different variable names without understanding** — Root cause is duplicate declaration, not name. (60% fail)
