Skip to main content

ghosttrap-sdk

Error reporting for Python apps, built for AI agents. Part of ghosttrap.

Install

pip install ghosttrap-sdk

Use

import ghosttrap
ghosttrap.init("t_your_token_here")

Get your token by running ghosttrap setup from ghosttrap-cli.

Optional kwargs

ghosttrap.init(
    "t_your_token_here",
    server="https://ghosttrap.io",  # override only if self-hosting
    send_user=False,                # see "User context" below
    trap_logs=False,                # see "Bare log errors" below
)

What it hooks into

  • sys.excepthook — unhandled exceptions
  • threading.excepthook — unhandled exceptions in background threads (these never reach sys.excepthook)
  • Python logging — logger.exception() and logger.error(..., exc_info=True)
  • Celery — task failures via celery.signals.task_failure (auto-detected)

Bare log errors (opt-in)

By default, only log records carrying an exception are reported. Pass trap_logs=True to also report logger.error() / logger.critical() calls with no exception — they arrive as type LoggedError / LoggedCritical with the log call site as the frame. Off by default deliberately: in a chatty codebase every error-level log line becomes an event, and error trackers that default this on are famous for the resulting floods. The server's 5-minute dedup window still applies either way.

What it sends

Every report includes the exception type, message, traceback, frames (file/line/function/code), and the server's hostname (socket.gethostname()).

Django

INSTALLED_APPS = [
    ...
    "ghosttrap.django.GhostTrapApp",
]

MIDDLEWARE = [
    "ghosttrap.django.GhostTrapMiddleware",
    ...
]

Browser / JavaScript errors

Errors in your users' browsers never reach the Python hooks. The Django integration ships a small relay so they land in the same stream. Two lines:

# urls.py
path("ghosttrap/", include("ghosttrap.django.urls")),
<!-- base template -->
<script src="{% static 'ghosttrap/ghosttrap.js' %}" defer></script>

The script hooks uncaught errors and unhandled promise rejections, drops known junk (opaque cross-origin "Script error.", browser-extension noise), deduplicates per page, and caps itself at 10 reports per page load. It posts to your own domain — the ghosttrap token never appears in page source — and the relay view forwards through the endpoint you configured with init(). No CORS setup, and ad blockers don't see a third-party request.

Browser events arrive with the JS error type (e.g. TypeError), the page URL in the traceback header, and the parsed JS stack as frames. Minified bundles produce minified frames — there's no source-map support. If you mount the relay somewhere other than /ghosttrap/, point the script at it with data-endpoint="/your/path/js/".

Manually trap an event

For errors you catch and handle but still want logged, or for non-exception conditions worth flagging:

import ghosttrap

try:
    do_thing()
except ValueError as e:
    ghosttrap.trap(e)        # caught exception — sent with type, message, traceback
    fallback()

ghosttrap.trap("payment gateway returned 503")  # synthetic event labelled "TrappedEvent"

trap() accepts an exception instance or a string. Strings get the caller's stack attached and a TrappedEvent type so they're distinct from real exceptions in the CLI. Both go through the same 5-minute dedup window as the auto-hook.

User context

Off by default. Pass send_user=True to init() and the Django middleware will attach the authenticated user's id and username to each report. Has no effect outside Django.

ghosttrap.init("t_your_token_here", send_user=True)

Zero dependencies

Pure Python stdlib. No transitive dependencies in your production image.

Metadata

Release files for ghosttrap-sdk 0.4.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ghosttrap-sdk 0.4.8
File Size Uploaded Provenance Actions
ghosttrap_sdk-0.4.8.tar.gz 10.1 kB Without provenance Details for ghosttrap_sdk-0.4.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ghosttrap-sdk 0.4.8
File Interpreter ABI Platform Actions
ghosttrap_sdk-0.4.8-py3-none-any.whl Python 3 none any Details for ghosttrap_sdk-0.4.8-py3-none-any.whl

Total release size: 20.4 kB

Release files / ghosttrap_sdk-0.4.8.tar.gz

Download URL ghosttrap_sdk-0.4.8.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c65a075be26b47da451e9bd63824dcd3c63eb2a8c52d57aa4449bbf2177b287c
BLAKE2b-256 checksum
How to use checksums
b139e033a2fecb2928d97b7a9fd5a5c7c7919c225f0d0c3e87483d6732bb7aee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / ghosttrap_sdk-0.4.8-py3-none-any.whl

Download URL ghosttrap_sdk-0.4.8-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb33dcb96aa7c6c996079f876290aeea15283c8033760d38bfa9aaff07b83261
BLAKE2b-256 checksum
How to use checksums
db198416920a46971b2ab54f12607752939371ecb26c85699b2465247f01cb4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Security for ghosttrap-sdk 0.4.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page