Mistral for Python
Learn about using Sentry for Mistral.
This integration connects Sentry with the Mistral Python SDK.
Once you've installed this SDK, you can use the Sentry Agents Tracing, a Sentry dashboard that helps you understand what's going on with your AI requests.
Sentry AI Observability will automatically collect information about prompts, tokens, and models. Learn more about the Agents Dashboard.
Install sentry-sdk from PyPI:
pip install sentry-sdk
pip install sentry-sdk
uv add sentry-sdk
Add MistralIntegration() to your integrations list:
import sentry_sdk
from sentry_sdk.integrations.mistral import MistralIntegration
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"gen_ai": {"inputs": True, "outputs": True},
},
integrations=[
MistralIntegration(),
],
)
import sentry_sdk
from sentry_sdk.integrations.mistral import MistralIntegration
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for tracing.
traces_sample_rate=1.0,
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"gen_ai": {"inputs": True, "outputs": True},
},
integrations=[
MistralIntegration(),
],
)
Verify that the integration works by making a chat request to Mistral.
import sentry_sdk
from sentry_sdk.integrations.mistral import MistralIntegration
from mistralai.client import Mistral
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
traces_sample_rate=1.0,
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"gen_ai": {"inputs": True, "outputs": True},
},
integrations=[
MistralIntegration(),
],
)
client = Mistral(api_key="(your Mistral key)")
response = client.chat.complete(
model="mistral-medium-latest",
messages=[
{"role": "user", "content": "What is the best French cheese?"}
],
)
import sentry_sdk
from sentry_sdk.integrations.mistral import MistralIntegration
from mistralai.client import Mistral
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
traces_sample_rate=1.0,
# Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
data_collection={
"gen_ai": {"inputs": True, "outputs": True},
},
integrations=[
MistralIntegration(),
],
)
client = Mistral(api_key="(your Mistral key)")
response = client.chat.complete(
model="mistral-medium-latest",
messages=[
{"role": "user", "content": "What is the best French cheese?"}
],
)
To try the async variant, use client.chat.complete_async(...) with await.
After running this script, the resulting data should show up in the AI Spans tab on the Explore > Traces > Trace page on Sentry.io.
If you manually created an Invoke Agent Span (not done in the example above) the data will also show up in the Agents Dashboard.
It may take a couple of moments for the data to appear in sentry.io.
The Mistral integration will connect Sentry with the supported Mistral methods automatically.
The supported functions are currently
Chat.completeandChat.complete_async(sync and async). Streaming completions (stream=True) are not instrumented.Sentry considers LLM inputs/outputs as PII (Personally identifiable information) and doesn't include PII data by default. To include the data, use the
gen_aikey of thedata_collectionoption, which lets you control inputs and outputs separately. When you pass adata_collectiondictionary, both are collected by default:Copiedsentry_sdk.init( # ... data_collection={ # Record LLM inputs, but not outputs "gen_ai": {"inputs": True, "outputs": False}, }, )sentry_sdk.init( # ... data_collection={ # Record LLM inputs, but not outputs "gen_ai": {"inputs": True, "outputs": False}, }, )Alternatively, set
send_default_pii=Truein thesentry_sdk.init()call.send_default_piiwill be removed in version 3.0 of the SDK.
- Mistral AI (
mistralai): 2.0.5+
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").