Skip to main content

A Python library to get AI-powered explanations for exceptions using Hugging Face models.

Project description

Read this README in Russian

🚀 Error Narrator

PyPI version

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.

📦 Features

  • 🤖 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).

💾 Installation

pip install error-narrator

📝 How to Use

1. 🔑 Get an API Key

The library requires an API key for the chosen provider.

💡 Tip: It is recommended to set your API key as an environment variable:

  • HUGGINGFACE_API_KEY for Gradio.
  • OPENAI_API_KEY for OpenAI.

2. ⚙️ Basic Usage

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.

import traceback
from error_narrator import ErrorNarrator

# The narrator will automatically look for the HUGGINGFACE_API_KEY environment variable
# if no api_key is provided.
narrator = ErrorNarrator() 

try:
    # Some code that might raise an error
    result = 1 / 0
except Exception:
    # Get the traceback as a string
    traceback_str = traceback.format_exc()
    # Get the explanation and print it to the console
    narrator.explain_and_print(traceback_str)

3. 🌍 Getting Explanations in Russian

To get explanations in a different language, use the language parameter during initialization.

# ...
# Initialize with Russian language support
narrator = ErrorNarrator(language="ru")
# ...

4. 🏷️ Using a specific provider (e.g., OpenAI)

You can also specify a provider and pass the API key directly.

narrator = ErrorNarrator(
    provider="openai",
    api_key="your-openai-api-key"
)
# ...

🤝 Development

Contributions are welcome! Please feel free to submit a pull request or open an issue.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

error_narrator-0.2.2.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

error_narrator-0.2.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file error_narrator-0.2.2.tar.gz.

File metadata

  • Download URL: error_narrator-0.2.2.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for error_narrator-0.2.2.tar.gz
Algorithm Hash digest
SHA256 1f6087c2f8467ec593182ff1bfa2acb54c7bfbb3cf3891b250d17b84b6ba5d55
MD5 44f3b2fe7e22762441b68c2b231c8050
BLAKE2b-256 fe90bc6bfffe2d2292215a60eabc2649aeff4ebffffc3eacf8e93fda05b3f574

See more details on using hashes here.

File details

Details for the file error_narrator-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: error_narrator-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for error_narrator-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 72c748085364598ba7a43ae68a56da3d372dcbc69ec4a4941c34ce4845549355
MD5 e472ab1d73a0af6cbe207fcb910db19f
BLAKE2b-256 5b32a18efbf2e08f59b7c02b76c644c29c116d5d97ecb210137bd3b6f7cfd98d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page