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:

Copied
pip install sentry-sdk

Add MistralIntegration() to your integrations list:

Copied
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.

Copied
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.complete and Chat.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_ai key of the data_collection option, which lets you control inputs and outputs separately. When you pass a data_collection dictionary, both are collected by default:

    Copied
    sentry_sdk.init(
        # ...
        data_collection={
            # Record LLM inputs, but not outputs
            "gen_ai": {"inputs": True, "outputs": False},
        },
    )
    

    Alternatively, set send_default_pii=True in the sentry_sdk.init() call. send_default_pii will be removed in version 3.0 of the SDK.

  • Mistral AI (mistralai): 2.0.5+
Was this helpful?
Help improve this content
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").