|
| 1 | +--- |
| 2 | +title: Hugging Face Hub |
| 3 | +description: "Learn about using Sentry for Hugging Face Hub." |
| 4 | +--- |
| 5 | + |
| 6 | +This integration connects Sentry with [Hugging Face Hub](https://github.com/huggingface/huggingface_hub) in Python. |
| 7 | + |
| 8 | +Once you've installed this SDK, you can use Sentry AI Agents Monitoring, a Sentry dashboard that helps you understand what's going on with your AI requests. Sentry AI Monitoring will automatically collect information about prompts, tools, tokens, and models. Learn more about the [AI Agents Dashboard](/product/insights/ai/agents). |
| 9 | + |
| 10 | +## Install |
| 11 | + |
| 12 | +Install `sentry-sdk` from PyPI with the `huggingface_hub` extra: |
| 13 | + |
| 14 | +```bash {tabTitle:pip} |
| 15 | +pip install "sentry-sdk[huggingface_hub]" |
| 16 | +``` |
| 17 | + |
| 18 | +```bash {tabTitle:uv} |
| 19 | +uv add "sentry-sdk[huggingface_hub]" |
| 20 | +``` |
| 21 | + |
| 22 | +## Configure |
| 23 | + |
| 24 | +If you have the `huggingface_hub` package in your dependencies, the Hugging Face Hub integration will be enabled automatically when you initialize the Sentry SDK. |
| 25 | + |
| 26 | +```python |
| 27 | +import sentry_sdk |
| 28 | + |
| 29 | +sentry_sdk.init( |
| 30 | + dsn="___PUBLIC_DSN___", |
| 31 | + environment="local", |
| 32 | + traces_sample_rate=1.0, |
| 33 | + send_default_pii=True, |
| 34 | +) |
| 35 | +``` |
| 36 | + |
| 37 | +## Verify |
| 38 | + |
| 39 | +Verify that the integration works by initializing a transaction and invoking an agent. In these examples, we're providing a function tool to roll a die. |
| 40 | + |
| 41 | +```python |
| 42 | +import random |
| 43 | +import sentry_sdk |
| 44 | +from huggingface_hub import InferenceClient |
| 45 | + |
| 46 | +def roll_die(sides): |
| 47 | + """Roll a die with a given number of sides""" |
| 48 | + return f"Rolled a {random.randint(1, sides)} on a {sides}-sided die." |
| 49 | + |
| 50 | +HF_TOKEN = "..." |
| 51 | +TOOLS = [{ |
| 52 | + "type": "function", |
| 53 | + "function": { |
| 54 | + "name": "roll_die", |
| 55 | + "description": "Roll a die with a given number of sides", |
| 56 | + "parameters": { |
| 57 | + "type": "object", |
| 58 | + "properties": { |
| 59 | + "sides": { |
| 60 | + "type": "int", |
| 61 | + "description": "The number of sides of the die" |
| 62 | + } |
| 63 | + }, |
| 64 | + "required": ["sides"], |
| 65 | + }, |
| 66 | + } |
| 67 | +}] |
| 68 | + |
| 69 | +def main(): |
| 70 | + sentry_sdk.init(...) # same as above |
| 71 | + |
| 72 | + client = InferenceClient(token=HF_TOKEN) |
| 73 | + |
| 74 | + with sentry_sdk.start_transaction(name="testing_sentry"): |
| 75 | + response = client.chat_completion( |
| 76 | + model="Qwen/Qwen2.5-72B-Instruct", |
| 77 | + messages=[{ |
| 78 | + "role": "user", |
| 79 | + "content": "Greet the user and use the die roll tool to roll a 6-sided die." |
| 80 | + }], |
| 81 | + tools=TOOLS, |
| 82 | + tool_choice="auto", |
| 83 | + ) |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +After running this script, the resulting data should show up in the `"AI Spans"` tab on the `"Explore" > "Traces"` page on Sentry.io, and in the [AI Agents Dashboard](/product/insights/ai/agents). |
| 91 | + |
| 92 | +It may take a couple of moments for the data to appear in [sentry.io](https://sentry.io). |
| 93 | + |
| 94 | +## Behavior |
| 95 | + |
| 96 | +- The Hugging Face Hub integration will connect Sentry with all supported Hugging Face Hub methods automatically. |
| 97 | + |
| 98 | +- All exceptions are reported. |
| 99 | + |
| 100 | +- Sentry considers LLM and tokenizer inputs/outputs as PII (Personally identifiable information) 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](#options) below. |
| 101 | + |
| 102 | +## Options |
| 103 | + |
| 104 | +By adding `HuggingfaceHubIntegration` to your `sentry_sdk.init()` call explicitly, you can set options for `HuggingfaceHubIntegration` to change its behavior: |
| 105 | + |
| 106 | +```python |
| 107 | +import sentry_sdk |
| 108 | +from sentry_sdk.integrations.huggingface_hub import HuggingfaceHubIntegration |
| 109 | + |
| 110 | +sentry_sdk.init( |
| 111 | + # ... |
| 112 | + # Add data like inputs and responses; |
| 113 | + # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info |
| 114 | + send_default_pii=True, |
| 115 | + integrations=[ |
| 116 | + HuggingfaceHubIntegration( |
| 117 | + include_prompts=False, # LLM inputs/outputs will be not sent to Sentry, despite send_default_pii=True |
| 118 | + ), |
| 119 | + ], |
| 120 | +) |
| 121 | +``` |
| 122 | + |
| 123 | +You can pass the following keyword arguments to `HuggingfaceHubIntegration()`: |
| 124 | + |
| 125 | +- `include_prompts` |
| 126 | + |
| 127 | + Whether LLM and tokenizer inputs and outputs should be sent to Sentry. Sentry considers this data personal identifiable data (PII) 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`. |
| 128 | + |
| 129 | + The default is `True`. |
| 130 | + |
| 131 | +## Supported Versions |
| 132 | + |
| 133 | +- Python: 3.8+ |
| 134 | +- huggingface_hub: 0.22+ |
0 commit comments