Skip to content

fix(huggingface_hub): instrument OpenAI-compatible chat completions alias - #6983

Open
saisharan0103 wants to merge 1 commit into
getsentry:masterfrom
saisharan0103:mergepath/issue-5848-huggingface-hub-instrument-chat-completi
Open

fix(huggingface_hub): instrument OpenAI-compatible chat completions alias#6983
saisharan0103 wants to merge 1 commit into
getsentry:masterfrom
saisharan0103:mergepath/issue-5848-huggingface-hub-instrument-chat-completi

Conversation

@saisharan0103

Copy link
Copy Markdown

Fixes #5848.

The HuggingFace integration setup only monkeypatched InferenceClient.text_generation and InferenceClient.chat_completion. HuggingFace's OpenAI-compatible client.chat.completions.create() path is implemented on a separate chat/completions helper object, so calls through that alias did not invoke the patched InferenceClient.chat_completion method. As a result, those calls never entered _wrap_huggingface_task and missed gen_ai spans, prompt/response attributes, token usage, and error capture.

This adds a guarded patch in HuggingfaceHubIntegration.setup_once for the class method backing InferenceClient.chat.completions.create(), reusing the existing _wrap_huggingface_task wrapper with OP.GEN_AI_CHAT so the alias records the same span data as chat_completion.

The patch remains conditional via hasattr/getattr so older supported huggingface_hub versions that do not expose the OpenAI-compatible alias still import and initialize cleanly.

Modified files:

  • sentry_sdk/integrations/huggingface_hub.py
  • tests/integrations/huggingface_hub/test_huggingface_hub.py

ruff check sentry_sdk/integrations/huggingface_hub.py tests/integrations/huggingface_hub/test_huggingface_hub.py reports no new findings on the changed files.
Ran pytest -x locally with no new failures.

@saisharan0103
saisharan0103 requested a review from a team as a code owner August 1, 2026 15:53

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 66a5825. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

huggingface_hub: Instrument chat.completions.create() OpenAI-compatible alias

1 participant