Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ typing = [
"pydantic>=2.13.4",
"pydantic-ai-slim>=2.23.0",
"langchain-core>=1.5.3",
"huggingface-hub>=1.26.1",
]
test = [
"dataclasses ; python_full_version < '3.7'",
Expand Down
14 changes: 9 additions & 5 deletions sentry_sdk/integrations/huggingface_hub.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
import sys
from functools import wraps
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

import sentry_sdk
from sentry_sdk.ai.monitoring import record_token_usage
Expand All @@ -24,6 +24,10 @@
if TYPE_CHECKING:
from typing import Any, Callable, Iterable, Union

from huggingface_hub import (
ChatCompletionStreamOutput,
)

from sentry_sdk.tracing import Span

try:
Expand All @@ -44,13 +48,13 @@ def __init__(
@staticmethod
def setup_once() -> None:
# Other tasks that can be called: https://huggingface.co/docs/huggingface_hub/guides/inference#supported-providers-and-tasks
huggingface_hub.inference._client.InferenceClient.text_generation = (
huggingface_hub.inference._client.InferenceClient.text_generation = ( # type: ignore[method-assign]
_wrap_huggingface_task(
huggingface_hub.inference._client.InferenceClient.text_generation,
OP.GEN_AI_TEXT_COMPLETION,
)
)
huggingface_hub.inference._client.InferenceClient.chat_completion = (
huggingface_hub.inference._client.InferenceClient.chat_completion = ( # type: ignore[method-assign]
_wrap_huggingface_task(
huggingface_hub.inference._client.InferenceClient.chat_completion,
OP.GEN_AI_CHAT,
Expand Down Expand Up @@ -302,15 +306,15 @@ def new_details_iterator() -> "Iterable[Any]":

else:
# chat-completion stream output
def new_iterator() -> "Iterable[str]":
def new_iterator() -> "Iterable[ChatCompletionStreamOutput]":
finish_reason = None
response_model = None
response_text_buffer: "list[str]" = []
tool_calls = None
usage = None

with capture_internal_exceptions():
for chunk in res:
for chunk in cast("Iterable[ChatCompletionStreamOutput]", res):
if hasattr(chunk, "model") and chunk.model is not None:
response_model = chunk.model

Expand Down
66 changes: 66 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading