TypeSafe AI for Python

Learn about using Sentry for TypeSafe AI.

This integration connects Sentry with the TypeSafe 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.

Install sentry-sdk from PyPI:

Copied
pip install sentry-sdk

Add TypeSafeIntegration() to your integrations list:

Copied
import sentry_sdk
from sentry_sdk.integrations.typesafe import TypeSafeIntegration

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=[
        TypeSafeIntegration(),
    ],
)

Verify that the integration works by making a request to TypeSafe AI.

Copied
import sentry_sdk
from sentry_sdk.integrations.typesafe import TypeSafeIntegration

from typesafe_sdk import Choice, TypeSafeClient

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=[
        TypeSafeIntegration(),
    ],
)

client = TypeSafeClient(api_key="(your TypeSafe key)")

response = client.system_one(
    state={"document": "I was charged twice. Please fix this ASAP."},
    questions={
        "category": Choice(
            instructions="What is this ticket about?",
            criteria={"billing": None, "technical": None, "other": None},
        ),
    },
)

To try the async variant, use AsyncTypeSafeClient 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.

It may take a couple of moments for the data to appear in sentry.io.

  • TypeSafe AI (typesafe-sdk): 0.7.0+
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").