Skip to main content

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

Project description

🚀 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.1.tar.gz (6.4 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.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: error_narrator-0.2.1.tar.gz
  • Upload date:
  • Size: 6.4 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.1.tar.gz
Algorithm Hash digest
SHA256 b5e3a0f0b38b253f81a427da60b899e13e9f30c10b008698a296fc19f25c557f
MD5 45e531486b7ae0421a819bfbf436863b
BLAKE2b-256 13146c040472ceffd9ec181708db822314cb5afa1201604a4af82505c1b79a8a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: error_narrator-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f38e08d9656bc7362e74a1cd4d5d9d5b3645fb4e10775ebe48b8f847ecf5dcd1
MD5 fa4107c2ac12a99102e8a39311f481f9
BLAKE2b-256 555ea01ed655da22e8f0d2891da1b204a3835f3321778ddc0623fb333faf008e

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