Error Narrator is a Python library that uses AI to provide clear, human-readable explanations for Python exceptions and tracebacks. Instead of just getting a stack trace, you get a structured, educational breakdown of what went wrong, right in your console.
The library is multilingual, currently supporting English (default) and Russian.
- 🤖 AI-Powered Explanations: Uses language models from Gradio or OpenAI to explain errors.
- 📝 Structured Output: Provides a clear, markdown-formatted explanation with:
- 🎯 Root Cause: What caused the error.
- 📍 Error Location: Pinpoints the exact file and line.
- 🛠️ Suggested Fix: Offers a code diff for a potential solution.
- 🎓 A Learning Moment: Explains the underlying concepts to prevent future mistakes.
- 🎨 Rich Console Output: Uses the rich library to print beautiful, colorized output in the terminal.
- ⚡ Async Support: Provides asynchronous methods (*_async) for non-blocking operations.
- 💾 Caching: Caches explanations for identical tracebacks to speed up repeated runs and reduce API calls.
- 🌐 Multilingual: Supports explanations in English (en) and Russian (ru).
The library supports two providers: gradio (default) and openai.
- Gradio (Default & Free): This provider uses public, community-hosted models on Hugging Face Spaces. An API key (Hugging Face User Access Token) is optional for most public models but may be required for private models or to increase rate limits. You can get a token from your Hugging Face account settings.
- OpenAI (Higher Quality & Paid): This provider uses official OpenAI models. An API key from your OpenAI dashboard is mandatory.
💡 Tip: The library will automatically detect API keys set as environment variables:
- HUGGINGFACE_API_KEY for Gradio (optional).
- OPENAI_API_KEY for OpenAI (required).
You can set these variables for your current terminal session or add them to your shell's profile file (e.g., .bashrc, .zshrc, or your system's environment variable settings) for them to be permanent.
On macOS/Linux:
On Windows (Command Prompt):
On Windows (PowerShell):
Here is a simple example of how to use ErrorNarrator. The library will automatically catch exceptions within a try...except block and explain them.
By default, the explanation will be in English.
To get explanations in a different language, use the language parameter during initialization.
You can also specify a provider and pass the API key directly.
By default, the gradio provider uses a predefined list of public models and tries them in a random order for each request. This rotation and retry system increases the chances of getting a successful response.
The best way to customize the model list is to pass your own list directly during initialization using the models parameter. This gives you full control without editing any package files.
If you want to disable rotation and use only one model, you can use the model_id parameter as a convenient shortcut.
If you want to change the default behavior for all your projects, you can edit the gradio_models.py file directly in your Python site-packages directory. This is generally not recommended unless you know what you are doing.
Contributions are welcome! Please feel free to submit a pull request or open an issue.