Sentry Protobuf Definitions (Generated Python Code)
sentry-protos provides the auto-generated Python code for Sentry's internal Protobuf definitions. It encapsulates the data structures used across various Sentry services, allowing Python applications to interact with Sentry's protobuf-based APIs. The library is actively maintained with frequent minor releases to incorporate new or updated protobuf schemas.
Common errors
-
ModuleNotFoundError: No module named 'sentry_protos.some_service.v1.some_service_pb2'
cause The `sentry-protos` package is either not installed, or the specific protobuf definition you are trying to import does not exist in your installed version (e.g., it's a new service/version or has been renamed).fixEnsure `sentry-protos` is installed (`pip install sentry-protos`). Verify the exact module path by checking the `sentry_protos` directory in your `site-packages` or by looking at the Sentry GitHub repository. If it's a new service, upgrade `sentry-protos` to the latest version. -
TypeError: Parameter to MergeFrom() must be instance of same class: expected sentry_protos.snuba.v1.snuba_pb2.QueryRequest got sentry_protos.snuba.v1.snuba_pb2.QueryRequest
cause This cryptic error usually indicates a protobuf version mismatch between different parts of your application or dependencies, or an incompatibility when trying to merge/assign messages that, despite having the same class name, were generated from slightly different `.proto` definitions or by different `protobuf` runtime versions.fixEnsure all parts of your application are using the same installed version of `sentry-protos` and `protobuf`. Try updating both `pip install --upgrade sentry-protos protobuf` and rebuilding any environments to ensure consistent versions. Avoid mixing message objects created from different `sentry-protos` installations or different `protobuf` runtime versions.
Warnings
- breaking As `sentry-protos` is a pre-1.0 library (version 0.x.y), minor version bumps (e.g., 0.8.x to 0.9.x) can introduce breaking changes to message definitions or removal of fields/services. Always review the changelog when upgrading.
- gotcha This package contains only the generated Python message definitions (`_pb2.py` files). It does not include gRPC service stubs or client/server code (`_pb2_grpc.py` files). If you need to implement gRPC services or clients based on these protos, you will need `grpcio` and `grpcio-tools` and will have to generate those stubs yourself.
- gotcha Due to Sentry's continuous development, upstream `.proto` definitions are frequently updated. This means `sentry-protos` receives regular updates to reflect new fields, messages, or services. Older versions of `sentry-protos` may lack definitions for newer features or services, leading to `AttributeError` or unexpected behavior.
Install
-
pip install sentry-protos
Imports
- snuba_pb2
import snuba_pb2
from sentry_protos.snuba.v1 import snuba_pb2
- MyMessage
from sentry_protos.some_service.vX import some_service_pb2
Quickstart
from sentry_protos.snuba.v1 import snuba_pb2
# Create a QueryRequest message
query_request = snuba_pb2.QueryRequest(
dataset='events',
query="MATCH (event) SELECT event.id"
)
print(f"Successfully created Snuba QueryRequest:")
print(f" Dataset: {query_request.dataset}")
print(f" Query: {query_request.query}")
# You can also set fields dynamically
another_query = snuba_pb2.QueryRequest()
another_query.dataset = 'transactions'
another_query.query = 'MATCH (transaction) SELECT transaction.duration'
print(f"\nAnother QueryRequest: {another_query.query}")