A CLI tool that explains Python errors in simple human language.
Project description
Error Translator
Translate raw Python tracebacks into clear explanations and actionable fixes. Built for local-first developer workflows: CLI, import-hook auto mode, Python API, and FastAPI.
✨ Key Features
- CLI-first workflow: run a script, paste an error, or pipe logs directly into
explain-error. - Auto mode:
import error_translator.autoinstalls a customsys.excepthookfor unhandled crashes. - HTTP API: expose the same engine through FastAPI for service integrations.
- Rules engine: regex-based matching from
rules.jsonfor deterministic, offline translations. - AST analysis hooks: error-type-specific handlers (via registry) can add deeper typo/context insight.
Installation
Install from PyPI
pip install error-translator-cli-v2
Local development setup
git clone https://github.com/gourabanandad/error-translator.git
cd error-translator
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activate
pip install -r requirements.txt
pip install -e .
pytest
Usage
1) CLI usage
Run a Python script and translate unhandled errors:
explain-error run script.py
Translate a raw error string directly:
explain-error "NameError: name 'usr_count' is not defined"
Translate piped traceback logs:
cat error.log | explain-error
2) Python auto-import mode
import error_translator.auto
# Any unhandled exception in this process is intercepted
# and rendered as a translated, colorized explanation.
def main():
total = "Users: " + 42
main()
3) FastAPI server usage
Start server:
uvicorn error_translator.server:app --host 127.0.0.1 --port 8000 --reload
Health check:
curl http://127.0.0.1:8000/
Translate via API:
curl -X POST http://127.0.0.1:8000/translate \
-H "Content-Type: application/json" \
-d "{\"traceback_setting\":\"Traceback (most recent call last):\\n File 'app.py', line 14, in <module>\\n total = 'Users: ' + 42\\nTypeError: can only concatenate str (not 'int') to str\"}"
Real-World Input → Output Examples
Example A: Type mismatch
Input traceback:
Traceback (most recent call last):
File "app.py", line 14, in <module>
total = "Users: " + 42
TypeError: can only concatenate str (not "int") to str
Translated output (CLI render):
Error Detected:
TypeError: can only concatenate str (not "int") to str
Location: app.py (Line 14)
Explanation:
You are trying to add a string to an int, which Python cannot do.
Suggested Fix:
Convert the int to a string first using str() before concatenating.
Example B: Missing variable typo
Input error line:
NameError: name 'usr_count' is not defined
Translated output:
Explanation:
You tried to use a variable or function named 'usr_count', but Python doesn't recognize it.
Suggested Fix:
Check if 'usr_count' is spelled correctly, or ensure you defined/imported it before using it.
Architecture
Error Translator uses one core engine with multiple entry points.
- Input ingestion: traceback text from CLI, API, or auto-hook.
- Rule matching:
core.pychecks the last error line against compiled regex rules fromrules.json. - Context extraction: file path, line number, and source line are extracted when available.
- Optional AST insight: for selected error types, handlers from
ast_handlers.pycan add extra hints. - Structured output: returns a stable dictionary (
explanation,fix,file,line,code,matched_error, optionalast_insight).
How It Works Internally
load_rules()readsrules.jsononce and caches it.compiled_rules()compiles regex patterns once for fast repeated matching.translate_error():- normalizes traceback input,
- finds the terminal error line,
- applies first matching rule,
- formats placeholders (
{0},{1}, ...), - enriches with file/line/code context,
- dispatches to
AST_REGISTRYby error type when applicable.
- If no rule matches, a safe fallback explanation/fix from
defaultis returned.
Project Structure
error-translator/
├─ error_translator/
│ ├─ core.py # Translation pipeline, rule loading, context extraction
│ ├─ rules.json # Regex patterns + explanations + fixes
│ ├─ ast_handlers.py # AST handler registry and insight plugins
│ ├─ cli.py # `explain-error` command and colorized terminal renderer
│ ├─ auto.py # Auto mode via sys.excepthook override
│ └─ server.py # FastAPI app and /translate endpoint
├─ tests/
│ └─ test_core.py # Core translation and regex extraction tests
└─ docs/
├─ ARCHITECTURE.md
└─ CONTRIBUTING.md
How To Add New Rules
Add a new object to error_translator/rules.json under rules:
{
"pattern": "ValueError: invalid literal for int\\(\\) with base 10: '(.*)'",
"explanation": "You tried to convert '{0}' to an integer, but it is not a valid whole number.",
"fix": "Ensure '{0}' contains only digits, or parse it as float if decimals are expected."
}
Guidelines:
- Keep regex patterns specific to avoid false positives.
- Use capture groups for dynamic values and reference them with
{0},{1}, etc. - Keep explanations beginner-friendly and fixes immediately actionable.
- Add/extend tests in
tests/test_core.pyfor each new rule.
Contributing
Contributions are welcome, from first-time contributors to maintainers.
Typical workflow:
- Fork and clone the repository.
- Create a feature branch.
- Add or improve rules/tests/docs.
- Run
pytest. - Open a PR with a sample traceback and expected translation.
Please see:
docs/CONTRIBUTING.mddocs/ARCHITECTURE.md
Maintainer
Built and maintained by Gourabananda Datta and contributors.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file error_translator_cli_v2-1.1.2.tar.gz.
File metadata
- Download URL: error_translator_cli_v2-1.1.2.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbab24abc9880d43d49fe306ec401503286530a150653c00f78b60bee51a528d
|
|
| MD5 |
51beb116056763627b7a43bc34b632ee
|
|
| BLAKE2b-256 |
43108ea8b60551624e209067b856f4abe790c5c33084d204c0927128895f287e
|
File details
Details for the file error_translator_cli_v2-1.1.2-py3-none-any.whl.
File metadata
- Download URL: error_translator_cli_v2-1.1.2-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75301d31ef59ce4e889c0bd59c207af9d083c68b18633e189b7b91660095b961
|
|
| MD5 |
5c7f22d9e38cb062c39c594eb8065697
|
|
| BLAKE2b-256 |
d8364c3c48b444a9f8cdff74083fd9d6bfea8ce1827f8aa216076c356b452254
|