# RuntimeError: Working outside of request context.

- **ID:** `python/flask-runtimeerror-working-outside-of-request-context`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Attempting to use Flask's request object (e.g., request.args, request.form) outside of an active request context, such as in a background thread or during app initialization.

## Version Compatibility

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

## Workarounds

1. **Use app.test_request_context() to create a temporary context** (90% success)
   ```
   with app.test_request_context():
    # access request here
    print(request.args)
   ```
2. **Pass request data explicitly to background tasks** (85% success)
   ```
   def background_task(user_id):
    # use passed data, not request
    pass
# In route:
user_id = request.args.get('id')
Thread(target=background_task, args=(user_id,)).start()
   ```

## Dead Ends

- **Using app.request_context() manually without pushing context** — Creates context but doesn't push it, so request is still unavailable. (70% fail)
- **Accessing request in a Celery task without context** — Celery tasks run in separate threads, lacking Flask context. (85% fail)
