Skip to main content

Reveal hidden state in Python exceptions with automatic variable tracing

Project description

Tracelight

Reveal hidden state in Python exceptions with automatic variable tracing

Tracelight exposes the hidden state of your application by automatically logging all local variables in each frame of an exception's traceback. This gives you instant insight into what went wrong without having to add print statements or run in debug mode.

Installation

pip install tracelight

Quick Start

import logging
from tracelight import log_exception_state

# Configure your logger however you like
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("exception_logger")

def some_function(x):
    a = x + 1
    b = a * 2
    return b / 0  # Deliberate error

try:
    some_function(5)
except Exception as e:
    # Log all local variables from each frame
    log_exception_state(e, logger)
    # Re-raise if needed
    raise

Key Features

Function Decorator

Automate error handling with the @traced decorator:

from tracelight import traced

@traced()
def risky_function(x, y):
    complicated_result = process(x)
    return complicated_result[y]  # Might raise KeyError

# All exceptions automatically logged with full variable context!
risky_function("input", "missing_key")

Agent Tool Decorator

Specifically designed for agent systems and MCP servers:

from tracelight.agent_utils import traced_tool

@traced_tool()
def weather_tool(location="New York"):
    # Complex implementation with HTTP calls etc.
    return get_weather_data(location) 

# If anything fails, returns a structured error dict:
# {"status": "error", "error_type": "...", ...}

Context Manager

Easily wrap specific blocks of code:

from tracelight import TracedError

# Automatically logs and preserves original exception
with TracedError(logger=my_logger):
    risky_operation()

Use Cases

Tracelight is particularly useful for:

1. Data‐Pipeline Breakdowns

Context: Multi-stage ETL jobs where a mysterious KeyError pops up.

With Tracelight: Your logs show the full contents of the record and every local variable—no need to sprinkle print calls or guess which field is missing.

2. Async Callback Chaos

Context: In an asyncio-driven system, tasks fire off callbacks and one raises an exception deep inside a helper function.

With Tracelight: You get the full context of all local variables in that callback frame—instantly pinpointing the cause.

3. Agent-Based Workflows

Context: Your LLM‐driven agent orchestrates several tools; one tool call fails with a parsing error.

With Tracelight: You immediately see all variables in context, including the raw responses and state data—so you can adjust your tool chain.

Integration with Agent Systems

Tracelight is designed to work seamlessly with agent-based systems and Model Context Protocol (MCP) servers. It can be integrated as a drop-in error handler to provide rich debugging information when tool calls or agent workflows fail unexpectedly.

from tracelight import log_exception_state
from tracelight.agent_utils import traced_tool

# Method 1: Wrap entire tool with decorator
@traced_tool()
def agent_tool(params):
    # Complex implementation...
    return process_complex_workflow(params)

# Method 2: Manual integration
def another_tool(params):
    try:
        # Complex tool implementation
        result = process_complex_workflow(params)
        return {"status": "success", "data": result}
    except Exception as e:
        # Log all variables in each frame of the exception
        log_exception_state(e, logger)
        # Return a graceful error response with helpful context
        return {
            "status": "error",
            "error": str(e),
            "error_type": type(e).__name__
        }

FastMCP Integration Example

Tracelight integrates seamlessly with FastMCP servers for rich error handling in MCP tools:

from mcp.server.fastmcp import FastMCP, Context
from pydantic import BaseModel, Field
from tracelight.agent_utils import traced_tool
import logging

# Setup logging
logger = logging.getLogger("mcp-server")

# Create FastMCP server
mcp = FastMCP("TracelightExample")

# Define request model
class WeatherRequest(BaseModel):
    city: str = Field(..., description="City to get weather for")
    country_code: str = Field(None, description="Optional 2-letter country code")

# Define response models
class WeatherResponse(BaseModel):
    temperature: float
    condition: str
    humidity: int

class ErrorResponse(BaseModel):
    status: str = "error"
    error_type: str
    error: str
    context: dict = None

# Create traced tool with automatic error handling
@mcp.tool()
@traced_tool(logger=logger)
async def get_weather(request: WeatherRequest, ctx: Context):
    """Get current weather with automatic error tracing."""
    # This will automatically log all variables if an exception occurs
    # and return a properly formatted error response
    
    if request.city.lower() == "atlantis":
        raise ValueError("City not found: Atlantis is a fictional city")  
        
    # Simulate API call
    weather_data = await fetch_weather_api(request.city)  # Would raise on failure
    
    # The decorator automatically handles any exceptions and returns
    # a structured error response that works with FastMCP
    return WeatherResponse(
        temperature=23.5,
        condition="sunny",
        humidity=65
    )

When the tool fails, Tracelight logs the complete variable state and returns a structured error response that works perfectly with FastMCP's tool response format, making it easy for agents to handle errors gracefully.

Advanced Usage

from tracelight import log_exception_state
from tracelight.agent_utils import format_for_agent

# Exclude sensitive variables
log_exception_state(e, logger, exclude_vars=["password", "api_key"])

# Custom formatting for agent consumption
log_exception_state(e, logger, format_var=format_for_agent)

# Customize log level and variable length
log_exception_state(e, logger, 
                   level=logging.ERROR,  # Log level
                   max_var_length=2000)  # Allow longer variable values

License

MIT

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

tracelight-0.1.2.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

tracelight-0.1.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file tracelight-0.1.2.tar.gz.

File metadata

  • Download URL: tracelight-0.1.2.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tracelight-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d5a6a9092fbdcbe3d6424c670ec75753127545a3debff30f2f7a99c1c3902985
MD5 ddb33d1011865f0b70f91f3694c84eb9
BLAKE2b-256 faa35a51419e581e1d482ef639845909b739398271d66699e7ba029d159a5616

See more details on using hashes here.

Provenance

The following attestation bundles were made for tracelight-0.1.2.tar.gz:

Publisher: python-publish.yml on newsbubbles/tracelight

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tracelight-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tracelight-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tracelight-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d37f6b8de0e3c5d9e0db8902111e69546f14861bbc425950e0fb0ed7e1ea57cf
MD5 5a974e9285b9d17f0372dcec9cd21618
BLAKE2b-256 a6f911834d112dcf976d1071e030927a5de7156ce96bc2169b53c5b415fb8228

See more details on using hashes here.

Provenance

The following attestation bundles were made for tracelight-0.1.2-py3-none-any.whl:

Publisher: python-publish.yml on newsbubbles/tracelight

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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