Commit 1faec55
authored
feat(huggingface_hub): Migrate to span first (#6124)
Migrates the huggingface_hub integration to the span-first (streaming
span) architecture.
When `_experiments={"trace_lifecycle": "stream"}` is enabled, the
integration now uses `StreamedSpan` via `sentry_sdk.traces.start_span`
instead of the legacy `Span`-based (transactions) path. The legacy path
remains unchanged for backwards compatibility.
This is the first AI integration to emit `StreamedSpan`s, so the shared
AI monitoring utilities needed to accept both span types.
`record_token_usage` (`sentry_sdk/ai/monitoring.py`) and
`set_data_normalized` (`sentry_sdk/ai/utils.py`) now accept `Union[Span,
StreamedSpan]` and route attribute writes through a new private
`_set_span_data_attribute` helper in `sentry_sdk.ai.utils` — calling
`set_attribute` on `StreamedSpan` and `set_data` on `Span`. Existing AI
integrations (anthropic, openai, cohere, langchain, langgraph, litellm,
google_genai, openai_agents, pydantic_ai) are unaffected at runtime:
they continue to pass `Span` instances, which route to `set_data` via
the isinstance check.
Test coverage mirrors the existing non-streaming suite under streaming
mode (text_generation / chat_completion, sync and streaming, with and
without tools, plus an error-path test) parametrized over
`send_default_pii` and `include_prompts`.
Part of the broader span-first integration migration.
Fixes PY-2332
Fixes #60301 parent 6e4354a commit 1faec55
4 files changed
Lines changed: 505 additions & 25 deletions
File tree
- sentry_sdk
- ai
- integrations
- tests/integrations/huggingface_hub
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
97 | 99 | | |
98 | 100 | | |
99 | 101 | | |
100 | | - | |
| 102 | + | |
101 | 103 | | |
102 | 104 | | |
103 | 105 | | |
| |||
108 | 110 | | |
109 | 111 | | |
110 | 112 | | |
111 | | - | |
| 113 | + | |
112 | 114 | | |
113 | 115 | | |
114 | | - | |
| 116 | + | |
115 | 117 | | |
116 | 118 | | |
117 | | - | |
| 119 | + | |
| 120 | + | |
118 | 121 | | |
119 | 122 | | |
120 | 123 | | |
121 | 124 | | |
122 | 125 | | |
123 | | - | |
| 126 | + | |
| 127 | + | |
124 | 128 | | |
125 | 129 | | |
126 | 130 | | |
127 | 131 | | |
128 | 132 | | |
129 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
130 | 136 | | |
131 | 137 | | |
132 | | - | |
| 138 | + | |
| 139 | + | |
133 | 140 | | |
134 | 141 | | |
135 | 142 | | |
| |||
138 | 145 | | |
139 | 146 | | |
140 | 147 | | |
141 | | - | |
| 148 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
490 | 490 | | |
491 | 491 | | |
492 | 492 | | |
493 | | - | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
494 | 497 | | |
495 | 498 | | |
496 | 499 | | |
497 | | - | |
| 500 | + | |
498 | 501 | | |
499 | | - | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
500 | 512 | | |
501 | 513 | | |
502 | 514 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
19 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
| |||
83 | 87 | | |
84 | 88 | | |
85 | 89 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
91 | 105 | | |
92 | 106 | | |
93 | | - | |
| 107 | + | |
94 | 108 | | |
95 | 109 | | |
96 | | - | |
| 110 | + | |
97 | 111 | | |
98 | 112 | | |
99 | 113 | | |
| |||
116 | 130 | | |
117 | 131 | | |
118 | 132 | | |
119 | | - | |
| 133 | + | |
120 | 134 | | |
121 | 135 | | |
122 | 136 | | |
| |||
177 | 191 | | |
178 | 192 | | |
179 | 193 | | |
180 | | - | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
181 | 197 | | |
182 | 198 | | |
183 | 199 | | |
| |||
328 | 344 | | |
329 | 345 | | |
330 | 346 | | |
331 | | - | |
332 | | - | |
| 347 | + | |
| 348 | + | |
333 | 349 | | |
334 | 350 | | |
335 | 351 | | |
| |||
0 commit comments