Skip to main content

LLM-friendly Python debugger with structured, readable output

Project description

Blackbox

LLM-friendly Python debugger with structured, readable output and comprehensive instrumentation.

When Python code crashes, blackbox provides clean, well-organized output that's easy for LLMs (and humans) to understand and act on. It captures not just the crash, but the full context: I/O operations, resource usage, memory allocations, and more.

Installation

pip install jaymd96-blackbox

For full instrumentation features, install optional dependencies:

pip install jaymd96-blackbox[full]
# Or individually:
pip install psutil wrapt  # for resource monitoring and HTTP tracking

Usage

# Run with crash handling and full instrumentation (default)
python -m blackbox script.py

# Run with execution tracing
python -m blackbox --trace script.py

# Focus on specific package (hide stdlib/frameworks)
python -m blackbox --focus mypackage script.py

# Set timeout for hang detection
python -m blackbox --timeout 30 script.py

# Disable instrumentation (just crash handling)
python -m blackbox --no-instrumentation script.py

What You Get

On Crash - Full Context

══════════════════════════════════════════════════════════════════════
CRASH: KeyError
══════════════════════════════════════════════════════════════════════

Message: 'missing_key'
Location: mypackage/api.py:67 in process_data()

──────────────────────────────────────────────────────────────────────
CALL CHAIN
──────────────────────────────────────────────────────────────────────

    <module> → main → process_data

    [django: 12 frames hidden]
    [stdlib: 8 frames hidden]

──────────────────────────────────────────────────────────────────────
FRAME 1: mypackage/api.py:67  ← CRASH HERE
──────────────────────────────────────────────────────────────────────

Function: process_data(data)

   65│
   66│     # Access a key that doesn't exist
>> 67│     result = data['missing_key']
   68│     return result

Arguments:
    data         = {'status': 'ok', 'items': [1, 2, 3]}

Locals:
    large_list   = list([10000 items])

──────────────────────────────────────────────────────────────────────
I/O TRAIL (recent activity)
──────────────────────────────────────────────────────────────────────

  [HTTP]
    • GET https://api.example.com/data → 200 (45.2ms)
    • POST https://api.example.com/submit → 201 (123.5ms)

  [DB]
    • SELECT: SELECT * FROM users WHERE id = ? (2.3ms)
      query_type=SELECT, table=USERS

  [FILE]
    • open(config/settings.yaml, mode=r)
    • open(data/cache.json, mode=w)

  [CONFIG]
    • ENV[API_KEY] = <REDACTED>
    • ENV[DEBUG] → not set

──────────────────────────────────────────────────────────────────────
RESOURCE STATE (at crash)
──────────────────────────────────────────────────────────────────────

  Memory:
    RSS:     62.5 MB
    VMS:     415.2 GB
    Percent: 0.4%

  CPU: 12.3%

  Threads: 4

  Open Files (3):
    • /path/to/data.db
    • /path/to/log.txt
    • /tmp/cache.json

  Network Connections (1):
    • 127.0.0.1:5432 → 10.0.0.1:5432 (ESTABLISHED)

──────────────────────────────────────────────────────────────────────
TOP MEMORY ALLOCATIONS
──────────────────────────────────────────────────────────────────────

    391.6 KB  mypackage/api.py:64  (9872 allocs)
     28.8 KB  lib/python3.13/linecache.py:146  (358 allocs)
     10.0 KB  <frozen runpy>:259  (130 allocs)

──────────────────────────────────────────────────────────────────────
SLOW OPERATIONS
──────────────────────────────────────────────────────────────────────

       123.5ms  POST https://api.example.com/submit [http] ⚠ SLOW

──────────────────────────────────────────────────────────────────────
ANALYSIS
──────────────────────────────────────────────────────────────────────

Problem: Key 'missing_key' not found in 'data'
Available keys: ['status', 'items']

══════════════════════════════════════════════════════════════════════

With Execution Tracing

══════════════════════════════════════════════════════════════════════
EXECUTION TRACE
══════════════════════════════════════════════════════════════════════

├─→ process(items=[1, 2, 3])
│   ├─→ validate(items=[1, 2, 3])
│   │     returned True
│   ├─→ transform(items=[1, 2, 3])
│   │     returned [2, 4, 6]
│   └─ returned [2, 4, 6]

──────────────────────────────────────────────────────────────────────
SUMMARY
──────────────────────────────────────────────────────────────────────

Duration    : 0.003s
Calls       : 3
Max depth   : 2

══════════════════════════════════════════════════════════════════════

Instrumentation Features

Blackbox captures comprehensive runtime information:

Feature What it captures Requires
I/O Breadcrumbs File opens, socket connections, subprocess calls stdlib
HTTP Tracking requests/httpx/urllib3 calls with timing wrapt
Database Queries SQLAlchemy events, sqlite3 queries stdlib/sqlalchemy
Environment Access os.getenv() calls, with sensitive value masking stdlib
Resource Monitoring Memory, CPU, open files, network connections psutil
Memory Allocations Top allocations by file/line stdlib (tracemalloc)
Timing Metrics Slow operation detection stdlib

Options

Option Description
--trace Enable execution tracing
--focus PACKAGE Only show frames from this package
--collapse PACKAGE Collapse frames from this package
--show-stdlib Show standard library frames
--max-depth N Maximum frames to show
--context N Lines of code context (default: 2)
--timeout SECONDS Interrupt after N seconds
--no-instrumentation Disable all instrumentation
--slow-threshold MS Threshold for slow operations (default: 100ms)
-o FILE Write output to file

Programmatic Usage

from blackbox import install_crash_handler
from blackbox.instruments import install_all_hooks

# Install instrumentation first
install_all_hooks()

# Install as global exception handler
install_crash_handler(focus=['mypackage'])

# Now any uncaught exception will produce rich output

Custom Breadcrumbs

from blackbox.collector import get_collector, BreadcrumbType

collector = get_collector()

# Add custom breadcrumb
collector.add_breadcrumb(
    type=BreadcrumbType.CUSTOM,
    category="business_logic",
    message="Processing order #12345",
    data={"order_id": 12345, "items": 3},
)

Manual Timing

from blackbox.instruments.timing import timed, timed_block

# Decorator
@timed("critical_operation")
def my_function():
    pass

# Context manager
with timed_block("data_processing"):
    process_data()

Why Blackbox?

Standard Python tracebacks are noisy and hard to parse:

  • Mixed with framework internals
  • Missing local variable values
  • No context about what happened before the crash
  • No analysis or hints
  • Difficult for LLMs to process

Blackbox provides:

  • Clean visual hierarchy - Clear sections and separators
  • Noise filtering - Collapse stdlib and frameworks
  • Local variables - See what values caused the crash
  • Code context - Surrounding lines for context
  • I/O trail - What happened leading up to the crash
  • Resource state - Memory, CPU, files, connections at crash time
  • Memory analysis - Where allocations are happening
  • Analysis hints - Automatic problem identification
  • Structured format - Easy for LLMs to parse and reason about

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

jaymd96_blackbox-0.2.0.tar.gz (30.4 kB view details)

Uploaded Source

Built Distribution

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

jaymd96_blackbox-0.2.0-py3-none-any.whl (36.8 kB view details)

Uploaded Python 3

File details

Details for the file jaymd96_blackbox-0.2.0.tar.gz.

File metadata

  • Download URL: jaymd96_blackbox-0.2.0.tar.gz
  • Upload date:
  • Size: 30.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for jaymd96_blackbox-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d97822a29415234b721fe71cf528c83a2ffe411f7763f474bce4628c91e86137
MD5 d92376ba4953438b2811f57167a13f4c
BLAKE2b-256 6c6600d937735fbb77e62b8984a560b9b99a3307b387a107581a4e48f2e4819b

See more details on using hashes here.

File details

Details for the file jaymd96_blackbox-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jaymd96_blackbox-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db5e836e6c3b4acb59be6b5a4b238ec9ac1a57d7c14d3d595ec68ea6f6145238
MD5 045b83ad07db250b77e236ccc57f5401
BLAKE2b-256 9b093d59b12bc8b2c9015bd96f14e04068502076618782ba89ae31fab306b9b4

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