# Huggingface Hub | Sentry for Python

##### Product compatibility

This integration is designed for legacy [LLM Monitoring](https://docs.sentry.io/product/insights/ai/llm-monitoring.md) and is not currently compatible with [AI Agents Insights](https://docs.sentry.io/product/insights/agents.md).

We are working on this, and it will soon be compatible with [AI Agents Insights](https://docs.sentry.io/product/insights/agents.md).

This integration connects Sentry with the [Huggingface Hub Python SDK](https://github.com/huggingface/huggingface_hub.md) and has been confirmed to work with Huggingface Hub version 0.21.4.

Once you've installed this SDK, you can use Sentry LLM Monitoring, a Sentry dashboard that helps you understand what's going on with your AI pipelines.

Sentry LLM Monitoring will automatically collect information about prompts, tokens, and models from providers like OpenAI. Learn more about it [here](https://docs.sentry.io/product/insights/llm-monitoring.md).

## [Install](https://docs.sentry.io/.md#install)

Install `sentry-sdk` from PyPI with the `huggingface_hub` extra:

```bash
pip install "sentry-sdk[huggingface_hub]"
```

## [Configure](https://docs.sentry.io/.md#configure)

If you have the `huggingface_hub` package in your dependencies, the Huggingface Hub integration will be enabled automatically when you initialize the Sentry SDK.

Error Monitoring\[ ]Tracing\[ ]Profiling\[ ]Logs (Beta)

```python
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    #  performance
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    #  performance
    #  profiling
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be automatically collected while
    # there is an active span.
    profile_lifecycle="trace",
    #  profiling
    #  logs

    # Enable logs to be sent to Sentry
    _experiments={
        "enable_logs": True,
    },
    #  logs
)
```

## [Verify](https://docs.sentry.io/.md#verify)

Verify that the integration works by creating an AI pipeline. The resulting data should show up in your LLM monitoring dashboard.

```python
import sentry_sdk
from sentry_sdk.ai.monitoring import ai_track
from huggingface_hub import InferenceClient

sentry_sdk.init(...)  # same as above

client = InferenceClient(token="(your Huggingface Hub API token)", model="HuggingFaceH4/zephyr-7b-beta")

@ai_track("My AI pipeline")
def my_pipeline():
    with sentry_sdk.start_transaction(op="ai-inference", name="The result of the AI inference"):
        print(client.text_generation(prompt="say hello", details=True))
```

After running this script, a pipeline will be created in the [LLM Monitoring](https://docs.sentry.io/product/insights/llm-monitoring.md) section of the Sentry dashboard. The pipeline will have an associated Huggingface Hub span for the `text_generation` operation.

It may take a couple of moments for the data to appear in [sentry.io](https://sentry.io/.md).

## [Behavior](https://docs.sentry.io/.md#behavior)

* The Huggingface Hub integration will connect Sentry with all supported Huggingface Hub methods automatically.

* All exceptions in supported SDK methods are reported to Sentry automatically.

* Currently, the only supported module is `InferenceClient.text_generation`.

* Sentry considers LLM and tokenizer inputs/outputs as PII and doesn't include PII data by default. If you want to include the data, set `send_default_pii=True` in the `sentry_sdk.init()` call. To explicitly exclude prompts and outputs despite `send_default_pii=True`, configure the integration with `include_prompts=False` as shown in the [Options section](https://docs.sentry.io/.md#options) below.

## [Options](https://docs.sentry.io/.md#options)

After adding `HuggingfaceHubIntegration` to your `sentry_sdk.init()` call explicitly, you'll be able to set options to change its behavior:

```python
import sentry_sdk
from sentry_sdk.integrations.huggingface_hub import HuggingfaceHubIntegration

sentry_sdk.init(
    # ...
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    integrations=[
        HuggingfaceHubIntegration(
            include_prompts=False, # LLM/tokenizer inputs/outputs will be not sent to Sentry, despite send_default_pii=True
        ),
    ],
)
```

## [Supported Versions](https://docs.sentry.io/.md#supported-versions)

* huggingface\_hub: 0.21.4+
* Python: 3.9+
