Sentry Python SDK
raw JSON → 2.56.0 verified Tue May 12 auth: no python install: verified quickstart: stale
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.
pip install sentry-sdk Common errors
error AttributeError: module 'sentry_sdk' has no attribute 'Hub' ↓
cause The `sentry_sdk.Hub` class was entirely removed in `sentry-sdk` version 2.0 (released January 2024), replaced by direct SDK functions and the `Scope` API.
fix
Remove all references to
sentry_sdk.Hub. For managing scope, use sentry_sdk.set_context(), sentry_sdk.set_user(), sentry_sdk.add_breadcrumb(), or sentry_sdk.push_scope(). error NameError: name 'DjangoIntegration' is not defined ↓
cause Specific framework integrations (e.g., `DjangoIntegration`, `FastApiIntegration`) must be explicitly imported from their respective `sentry_sdk.integrations.<framework>` modules before being passed to `sentry_sdk.init()`.
fix
Add the necessary import statement, for example:
from sentry_sdk.integrations.django import DjangoIntegration. error TypeError: init() got an unexpected keyword argument 'enable_tracing' ↓
cause The `enable_tracing` parameter was deprecated in `sentry-sdk` v1.x and completely removed in v2.0; performance monitoring is now controlled solely by `traces_sample_rate`.
fix
Replace
enable_tracing=True with traces_sample_rate=1.0 (or your desired sampling rate between 0.0 and 1.0) in your sentry_sdk.init() call. error [sentry] DSN not set, not sending events. ↓
cause The DSN (Data Source Name) is a mandatory configuration for `sentry-sdk` to know where to send events, and it has not been provided.
fix
Provide your Sentry DSN either directly in
sentry_sdk.init(dsn="YOUR_DSN_HERE") or by setting the SENTRY_DSN environment variable. error TypeError: exception should be an exception instance, got str ↓
cause The `sentry_sdk.capture_exception()` function expects an actual exception object as its argument, not a string or other non-exception type.
fix
Pass an actual exception instance (e.g., a caught exception object or
sys.exc_info()[1]) to capture_exception(). For capturing arbitrary string messages, use sentry_sdk.capture_message() instead. Warnings
breaking Hub class removed in v2.0.0 (Jan 2024). All Hub-based patterns (Hub.current, configure_scope, push_scope via Hub) raise AttributeError. Massive volume of pre-2024 tutorials use Hub. ↓
fix Replace Hub.current.configure_scope() with sentry_sdk.new_scope(). Replace Hub.current.capture_exception() with sentry_sdk.capture_exception().
breaking enable_tracing parameter deprecated. Still works but logs DeprecationWarning. Will be removed in next major version. ↓
fix Replace enable_tracing=True with traces_sample_rate=1.0
gotcha Omitting traces_sample_rate means NO performance data (transactions/spans) is sent to Sentry — only errors. Default is 0.0. Many devs wonder why traces are missing. ↓
fix Add traces_sample_rate=0.1 (10%) for production or traces_sample_rate=1.0 for development.
gotcha before_send returning None silently drops the event. Sentry logs 'before send dropped event' at INFO level — easy to miss if not watching logs. ↓
fix before_send must return the event dict to send it. Return None only when intentionally filtering.
gotcha set_measurement() deprecated in favor of set_data() on spans. Will be removed in next major version. ↓
fix span.set_data('key', value) instead of span.set_measurement('key', value)
gotcha sentry_sdk.init() must be called before any framework initialization (before app = FastAPI(), before Django setup). Calling it after means some auto-instrumentation is missed. ↓
fix Call sentry_sdk.init() at the very top of your entry point, before importing framework app objects.
gotcha In async apps (FastAPI, async Django), sentry_sdk.init() should be called inside an async context or at module level before the event loop starts to ensure async instrumentation works correctly. ↓
fix Initialize at module level before app creation, not inside async startup handlers.
breaking sentry_sdk.init() fails if the DSN provided is malformed or contains placeholders (e.g., 'PROJECT_ID', 'PUBLIC_KEY', 'HOST') instead of actual values, leading to a sentry_sdk.utils.BadDsn error. ↓
fix Ensure the DSN string passed to `sentry_sdk.init()` is a complete and valid Sentry DSN, replacing any placeholders with their actual values obtained from your Sentry project settings.
breaking sentry_sdk.init() failed because the DSN contains placeholder values (e.g., 'PROJECT_ID', 'KEY', 'oNNN') instead of actual Sentry DSN components. ↓
fix Replace all DSN placeholders (PROJECT_ID, KEY, oNNN) with the actual values obtained from your Sentry project settings. Ensure the DSN is a valid URL.
Install
pip install 'sentry-sdk[fastapi]' pip install 'sentry-sdk[django]' Install compatibility verified last tested: 2026-05-12
python os / libc variant status wheel install import disk
3.10 alpine (musl) django - - 0.32s 71.3M
3.10 alpine (musl) fastapi - - 0.40s 36.9M
3.10 alpine (musl) sentry-sdk - - 0.32s 22.8M
3.10 slim (glibc) django - - 0.23s 72M
3.10 slim (glibc) fastapi - - 0.28s 37M
3.10 slim (glibc) sentry-sdk - - 0.23s 23M
3.11 alpine (musl) django - - 0.47s 76.6M
3.11 alpine (musl) fastapi - - 0.61s 40.9M
3.11 alpine (musl) sentry-sdk - - 0.48s 25.6M
3.11 slim (glibc) django - - 0.39s 77M
3.11 slim (glibc) fastapi - - 0.48s 41M
3.11 slim (glibc) sentry-sdk - - 0.40s 26M
3.12 alpine (musl) django - - 0.63s 67.9M
3.12 alpine (musl) fastapi - - 0.70s 32.0M
3.12 alpine (musl) sentry-sdk - - 0.61s 17.2M
3.12 slim (glibc) django - - 0.58s 68M
3.12 slim (glibc) fastapi - - 0.67s 32M
3.12 slim (glibc) sentry-sdk - - 0.59s 18M
3.13 alpine (musl) django - - 0.63s 67.6M
3.13 alpine (musl) fastapi - - 0.73s 31.7M
3.13 alpine (musl) sentry-sdk - - 0.62s 16.8M
3.13 slim (glibc) django - - 0.57s 68M
3.13 slim (glibc) fastapi - - 0.65s 31M
3.13 slim (glibc) sentry-sdk - - 0.58s 17M
3.9 alpine (musl) django - - 0.29s 69.3M
3.9 alpine (musl) fastapi - - 0.38s 36.1M
3.9 alpine (musl) sentry-sdk - - 0.28s 22.3M
3.9 slim (glibc) django - - 0.25s 70M
3.9 slim (glibc) fastapi - - 0.34s 36M
3.9 slim (glibc) sentry-sdk - - 0.26s 23M
Imports
- sentry_sdk.init wrong
# v1 Hub pattern — Hub removed in v2 from sentry_sdk import Hub with Hub.current.configure_scope() as scope: scope.set_tag('key', 'val') # AttributeError: Hub removed # enable_tracing deprecated sentry_sdk.init(dsn='...', enable_tracing=True) # Missing traces_sample_rate — no performance data sent sentry_sdk.init(dsn='...') # errors only, no tracescorrectimport sentry_sdk sentry_sdk.init( dsn='https://KEY@oNNN.ingest.sentry.io/PROJECT_ID', traces_sample_rate=1.0, # 0.0-1.0 — required for performance environment='production', release='myapp@1.0.0', send_default_pii=False, # do not send PII by default ) # Capture exceptions try: 1 / 0 except ZeroDivisionError: sentry_sdk.capture_exception() # Capture message sentry_sdk.capture_message('Something happened', level='warning') # Add context with sentry_sdk.new_scope() as scope: scope.set_tag('component', 'checkout') scope.set_user({'id': '123', 'email': 'user@example.com'}) sentry_sdk.capture_exception() - Framework integrations wrong
# Wrong: integrations auto-detected in v1 but must be explicit in some cases # Wrong: passing string names instead of instances sentry_sdk.init( dsn='...', integrations=['fastapi', 'celery'] # TypeError )correctimport sentry_sdk from sentry_sdk.integrations.fastapi import FastApiIntegration from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration from sentry_sdk.integrations.celery import CeleryIntegration sentry_sdk.init( dsn='https://KEY@oNNN.ingest.sentry.io/PROJECT_ID', traces_sample_rate=0.1, # 10% of transactions integrations=[ FastApiIntegration(), SqlalchemyIntegration(), CeleryIntegration(), ] )
Quickstart stale last tested: 2026-04-23
# 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