fix(huggingface_hub): instrument OpenAI-compatible chat completions alias - #6983
Conversation
…tions.create() OpenAI-compatible alias Closes getsentry#5848
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 66a5825. Configure here.
|
|
||
| _active_huggingface_task = contextvars.ContextVar( | ||
| "active_huggingface_task", default=False | ||
| ) |
There was a problem hiding this comment.
Unconditional contextvars import breaks 3.6
High Severity
This module now does a top-level import contextvars before the huggingface_hub availability guard. On Python 3.6, where contextvars is not in the stdlib, auto-enabling imports of HuggingfaceHubIntegration raise ImportError, which is not converted to DidNotEnable, so sentry_sdk.init() can crash even when Hugging Face is unused. Elsewhere the SDK uses ContextVar from sentry_sdk.utils for this compatibility path.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit 66a5825. Configure here.


Fixes #5848.
The HuggingFace integration setup only monkeypatched
InferenceClient.text_generationandInferenceClient.chat_completion. HuggingFace's OpenAI-compatibleclient.chat.completions.create()path is implemented on a separate chat/completions helper object, so calls through that alias did not invoke the patchedInferenceClient.chat_completionmethod. As a result, those calls never entered_wrap_huggingface_taskand missedgen_aispans, prompt/response attributes, token usage, and error capture.This adds a guarded patch in
HuggingfaceHubIntegration.setup_oncefor the class method backingInferenceClient.chat.completions.create(), reusing the existing_wrap_huggingface_taskwrapper withOP.GEN_AI_CHATso the alias records the same span data aschat_completion.The patch remains conditional via
hasattr/getattrso older supportedhuggingface_hubversions that do not expose the OpenAI-compatible alias still import and initialize cleanly.Modified files:
sentry_sdk/integrations/huggingface_hub.pytests/integrations/huggingface_hub/test_huggingface_hub.pyruff check sentry_sdk/integrations/huggingface_hub.py tests/integrations/huggingface_hub/test_huggingface_hub.pyreports no new findings on the changed files.Ran
pytest -xlocally with no new failures.