AI-powered Python exception debugger โ instant explanations, root causes, and fixes for your errors
Project description
traceback-ai
AI-powered Python exception debugger. When your code crashes, get an instant explanation, root cause analysis, and a concrete fix, right in your terminal.
Before traceback-ai
Traceback (most recent call last):
File "app.py", line 42, in process_order
price = item["price"] * quantity
KeyError: 'price'
๐ฉ What dict? Which line? Why? How do I fix it?
After traceback-ai
Traceback (most recent call last):
File "app.py", line 42, in process_order
price = item["price"] * quantity
KeyError: 'price'
โโโ AI Debug Analysis โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ via Anthropic โโโ
โญโ KeyError: 'price' โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ
โ You're trying to access 'price' from a dictionary, but that key โ
โ doesn't exist. This happens when the item dictionary was created โ
โ without a 'price' field, or the key was stored under a different name โ
โ (e.g. 'unit_price' or 'cost'). โ
โ โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Likely Causes
โข The item came from an API that returns 'unit_price' instead of 'price'
โข Items without prices are not being filtered before processing
โข A recent schema change renamed the field
Suggested Fix
Use .get() with a default, or validate the schema before processing
โญโ Fix Code โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ
โ # Safe access with default โ
โ price = item.get("price", 0.0) โ
โ โ
โ # Or validate and handle missing prices โ
โ if "price" not in item: โ
โ raise ValueError(f"Item {item.get('id')} has no price") โ
โ price = item["price"] * quantity โ
โ โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Search hint: Python dict KeyError safe access .get() default value
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Installation
pip install traceback-ai
Quickstart
import traceback_ai
traceback_ai.install()
# That's it. Now any unhandled exception gets AI analysis.
data = {}
print(data["missing_key"]) # โ AI explains and suggests a fix
Set your API key via environment variable (pick any one):
export ANTHROPIC_API_KEY="sk-ant-..." # Recommended
export OPENAI_API_KEY="sk-..."
export GROQ_API_KEY="gsk_..." # Free tier available
export CEREBRAS_API_KEY="csk_..." # Free tier available
# No key needed for Ollama (local)
auto mode (the default) detects whichever key is set and uses it automatically.
Features
- Zero boilerplate โ one line to install, works immediately
- 5 providers โ OpenAI, Anthropic, Groq, Cerebras, and Ollama (100% offline, no API key!)
- Rich terminal output โ beautiful panels with syntax-highlighted fix code
- Structured analysis โ explanation, root causes, fix description, and runnable fix code
- Interactive mode โ ask follow-up questions after the initial analysis
- Privacy-first โ
show_localsis off by default; secrets auto-redacted when enabled - Jupyter support โ works in notebooks out of the box
- CLI tool โ analyze saved tracebacks from log files
- Type-safe โ fully typed, mypy strict compatible
Usage
Global exception hook (most common)
import traceback_ai
traceback_ai.install()
Analyze caught exceptions
import sys, traceback_ai
try:
do_something_risky()
except Exception:
traceback_ai.analyze(*sys.exc_info())
Configuration
traceback_ai.install(
provider="anthropic", # 'openai' | 'anthropic' | 'groq' | 'cerebras' | 'ollama' | 'auto'
model="claude-sonnet-4-6", # Override the default model
show_locals=True, # Include local variable values (off by default)
interactive=True, # Ask follow-up questions
context_lines=20, # Source code lines around the error
redact_secrets=True, # Hide API keys / passwords (default: True)
)
Or via environment variables:
TBAI_PROVIDER=anthropic
TBAI_MODEL=claude-haiku-4-5-20251001
TBAI_SHOW_LOCALS=true
TBAI_INTERACTIVE=true
TBAI_CONTEXT_LINES=20
Ollama (100% offline, no API key)
# Install Ollama: https://ollama.ai
ollama pull llama3.2
traceback_ai.install(provider="ollama", model="llama3.2")
Groq or Cerebras (very fast, free tier)
traceback_ai.install(provider="groq") # uses GROQ_API_KEY
traceback_ai.install(provider="cerebras") # uses CEREBRAS_API_KEY
Get free API keys: Groq ยท Cerebras
Interactive mode
โโโ AI Debug Analysis โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ via OpenAI โโโ
...analysis...
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Ask a follow-up question (or press Enter to quit):
> Why does dict.get() return None instead of raising KeyError?
CLI
# Analyze a saved traceback from a log file
tbai analyze error.log
tbai analyze crash.txt --provider anthropic --interactive
# Run a Python script with AI debugging
tbai run my_script.py
tbai run my_script.py --show-locals
# Check your configuration
tbai config
# List available Ollama models
tbai ollama-models
Providers
| Provider | API Key | Default Model | Speed | Cost |
|---|---|---|---|---|
anthropic |
ANTHROPIC_API_KEY |
claude-haiku-4-5 | Fast | Low |
openai |
OPENAI_API_KEY |
gpt-4o-mini | Fast | Low |
groq |
GROQ_API_KEY |
llama-3.1-8b-instant | Very fast | Low (free tier) |
cerebras |
CEREBRAS_API_KEY |
llama3.1-8b | Very fast | Low (free tier) |
ollama |
None | llama3.2 | Medium | Free |
auto |
Detects available | โ | โ | โ |
auto (default) picks the first provider with a key set: Anthropic โ OpenAI โ Groq โ Cerebras โ Ollama.
Programmatic API
from traceback_ai.context import build_context
from traceback_ai.analyzer import run_analysis
import sys
try:
risky()
except Exception:
ctx = build_context(*sys.exc_info())
result = run_analysis(ctx)
# Access structured data:
print(result.explanation) # str
print(result.causes) # List[str]
print(result.fix) # str
print(result.fix_code) # Optional[str]
print(result.docs_hint) # Optional[str]
Jupyter Notebooks
import traceback_ai
traceback_ai.install()
# Works automatically โ IPython's exception handler is patched
Privacy
show_locals is False by default โ local variable values are never sent to the LLM unless you explicitly opt in. This prevents accidental exposure of secrets, PII, or sensitive business data in your tracebacks.
When you do enable show_locals=True, redact_secrets=True (also on by default) automatically redacts values that look like secrets โ API keys, tokens, passwords, AWS credentials, JWTs, PEM keys, and secrets nested inside dicts โ before anything is sent.
You can always review exactly what would be sent before enabling:
from traceback_ai.context import build_context
import sys
try:
my_bad_code()
except Exception:
ctx = build_context(*sys.exc_info(), show_locals=True, redact_secrets=True)
print(ctx.to_prompt_text(show_locals=True)) # Inspect before sending
Contributing
Contributions are welcome! See CONTRIBUTING.md.
git clone https://github.com/hugo-onnx/traceback-ai
cd traceback-ai
pip install -e ".[dev]"
pytest
License
MIT โ see LICENSE.
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 traceback_ai-0.1.0.tar.gz.
File metadata
- Download URL: traceback_ai-0.1.0.tar.gz
- Upload date:
- Size: 30.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1537a19b21d04bf84ec8b8f330b595b034fea18857bf7663b3fcbffc2401f92
|
|
| MD5 |
947e0cc2cd50c8e06ce9b09d36c6b7c1
|
|
| BLAKE2b-256 |
60424c0f301f59d33113340d053fb5c77d673fbc1a882e9836d3db49d56a7e0c
|
File details
Details for the file traceback_ai-0.1.0-py3-none-any.whl.
File metadata
- Download URL: traceback_ai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 29.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eef4ca6c18447791d7d711976162e55a7107c82c14ccedc51a2e5cc75ef1b1bb
|
|
| MD5 |
9592e31143af2c63e8997903d6ca3bd6
|
|
| BLAKE2b-256 |
bdab67d1f54c33646ac4c75e9db431f5f1a44a2966132eef73d7c3b9316bf88c
|