Sentry Python SDK

2.56.0 · active · verified Wed Mar 25

Official Sentry error monitoring and performance Python SDK. Current version: 2.56.0 (Mar 2026). v2.0 (Jan 2024) removed the Hub class entirely — all Hub-based patterns are broken. Scope API replaced Hub. Integrations (Django, FastAPI, Celery, etc.) must be explicitly imported and passed. traces_sample_rate must be set to enable performance monitoring — omitting it means zero traces sent. enable_tracing deprecated in favor of traces_sample_rate.

Warnings

Install

Imports

Quickstart

Sentry Python SDK — error capture, context, and performance tracing.

# pip install sentry-sdk
import sentry_sdk

sentry_sdk.init(
    dsn='https://KEY@oNNN.ingest.sentry.io/PROJECT_ID',
    traces_sample_rate=1.0,
    environment='production',
    release='1.0.0',
    before_send=lambda event, hint: event  # return None to drop event
)

# Automatic exception capture — unhandled exceptions sent automatically

# Manual exception capture
try:
    result = 1 / 0
except Exception as e:
    sentry_sdk.capture_exception(e)

# Add user context
sentry_sdk.set_user({'id': '42', 'email': 'user@example.com'})

# Add tags
sentry_sdk.set_tag('payment_provider', 'razorpay')

# Add extra data
sentry_sdk.set_extra('order_id', 'ord_123')

# Breadcrumbs
sentry_sdk.add_breadcrumb(
    category='auth',
    message='User logged in',
    level='info'
)

# Performance tracing
with sentry_sdk.start_transaction(op='task', name='process_order'):
    with sentry_sdk.start_span(op='db', description='fetch order'):
        pass  # your DB call here

view raw JSON →