This Django middleware captures errors and exceptions, sends them to OpenAI for explanation, and prints the explanation to stdout when debug mode is enabled. It uses an environment variable to securely manage the OpenAI API key.
The middleware supports both synchronous (WSGI) and asynchronous (ASGI) views. It auto-detects the view chain at startup and routes requests through the matching sync or async path, so no extra configuration is required to use it under either server type.
- Captures Django errors and exceptions
- Uses OpenAI to explain the error
- Securely manages the OpenAI API key using environment variables
- Works with both sync (WSGI) and async (ASGI) views
- Install django-explain-errors by running:
pip install django-explain-errors-
Add the middleware to your Django project:
-
Open your
settings.pyfile and add the middleware to theMIDDLEWARElist. Ensure that the middleware is added last in the list:MIDDLEWARE = [ ... 'explain_errors.middleware.ExplainErrorsMiddleware', ]
-
-
Set up environment variables:
-
Create a
.envfile in your project's root directory and add your OpenAI API key. Alternatively, you can set the API key insettings.py:OPENAI_API_KEY=your_openai_api_key_here
-
-
Ensure DEBUG is set to True:
Open your
settings.pyfile and set:DEBUG = True
-
Trigger an error in your Django application:
The middleware will capture the error, send it to OpenAI for explanation, and print the explanation to stdout. When an exception is caught, it returns a JSON
500response containing the error message and the explanation.
The middleware exposes both sync_capable = True and async_capable = True. At initialization it inspects get_response to decide whether it is part of a sync or async chain:
- Under WSGI (for example
runserverwith sync views), requests flow through the synchronous handler. - Under ASGI (for example with async views), requests are awaited through the async handler. The blocking OpenAI call is offloaded with
asgiref.sync.sync_to_asyncso the event loop is not blocked.
No additional settings are needed. Place the middleware last in MIDDLEWARE for both modes.
| Setting / variable | Required | Description |
|---|---|---|
OPENAI_API_KEY (env or settings) |
Yes, when DEBUG=True |
API key used to authenticate with OpenAI. Read first from the environment, then from settings. |
DEBUG |
Yes | The middleware is only active when DEBUG=True. When False, requests pass through untouched. |
OPENAI_MODEL |
No | Model used for explanations. Defaults to gpt-4o-mini. |
OPENAI_MAX_TOKENS |
No | Maximum tokens in the explanation. Defaults to 150. |
OPENAI_TIMEOUT |
No | Request timeout in seconds for the OpenAI client. Defaults to 10. |
OPENAI_MAX_TRACEBACK_CHARS |
No | Traceback is trimmed to its last N characters before being sent. Defaults to 3000. |
Here is an example of how to use the middleware in a Django project:
# settings.py
DEBUG = True
MIDDLEWARE = [
...
'explain_errors.middleware.ExplainErrorsMiddleware',
]
# .env
OPENAI_API_KEY=your_openai_api_key_hereWhen an error occurs, you will see an explanation printed to stdout.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.