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
6 changes: 3 additions & 3 deletions sentry_sdk/integrations/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from sentry_sdk import consts
from sentry_sdk.ai.monitoring import record_token_usage
from sentry_sdk.ai.utils import set_data_normalized
from sentry_sdk.ai.utils import get_start_span_function, set_data_normalized
from sentry_sdk.consts import SPANDATA
from sentry_sdk.tracing_utils import set_span_errored

Expand Down Expand Up @@ -142,7 +142,7 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any":

message = kwargs.get("message")

span = sentry_sdk.start_span(
span = get_start_span_function()(
op=consts.OP.COHERE_CHAT_COMPLETIONS_CREATE,
name="cohere.client.Chat",
origin=CohereIntegration.origin,
Expand Down Expand Up @@ -225,7 +225,7 @@ def new_embed(*args: "Any", **kwargs: "Any") -> "Any":
if integration is None:
return f(*args, **kwargs)

with sentry_sdk.start_span(
with get_start_span_function()(
op=consts.OP.COHERE_EMBEDDINGS_CREATE,
name="Cohere Embedding Creation",
origin=CohereIntegration.origin,
Expand Down
8 changes: 6 additions & 2 deletions sentry_sdk/integrations/huggingface_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import sentry_sdk
from sentry_sdk.ai.monitoring import record_token_usage
from sentry_sdk.ai.utils import _set_span_data_attribute, set_data_normalized
from sentry_sdk.ai.utils import (
_set_span_data_attribute,
get_start_span_function,
set_data_normalized,
)
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.scope import should_send_default_pii
Expand Down Expand Up @@ -97,7 +101,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
},
)
else:
span = sentry_sdk.start_span(
span = get_start_span_function()(
op=op,
name=f"{operation_name} {model}",
origin=HuggingfaceHubIntegration.origin,
Expand Down
5 changes: 3 additions & 2 deletions sentry_sdk/integrations/langgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import sentry_sdk
from sentry_sdk.ai.utils import (
get_start_span_function,

Check warning on line 6 in sentry_sdk/integrations/langgraph.py

View check run for this annotation

@sentry/warden / warden: find-bugs

`get_start_span_function()` calls in openai/cohere pass unsupported `op`/`origin` kwargs in span-streaming mode

When `_experiments.trace_lifecycle == 'stream'` is enabled, `sentry_sdk.ai.utils.get_start_span_function()` returns `sentry_sdk.traces.start_span`, whose signature is `start_span(name, attributes=None, parent_span=..., active=True)` — it accepts neither `op` nor `origin` and has no `**kwargs`. The new/preserved call sites added/touched by this PR in `sentry_sdk/integrations/openai.py` (lines 717, 785, 1181, 1213, 1267, 1325) and `sentry_sdk/integrations/cohere.py` (lines ~145 and ~228) all invoke `get_start_span_function()(op=..., name=..., origin=...)`. Under streaming mode this raises `TypeError: start_span() got an unexpected keyword argument 'op'`, which propagates out of the wrapper and breaks every wrapped OpenAI/Cohere call. The PR's own `huggingface_hub.py` change demonstrates the correct pattern: it explicitly branches on `has_span_streaming_enabled(...)` and passes `op`/`origin` via `attributes={"sentry.op": ..., "sentry.origin": ...}` when calling `sentry_sdk.traces.start_span`. The same guard was not applied to the openai and cohere call sites that this PR touches.
Comment thread
alexander-alderman-webb marked this conversation as resolved.
normalize_message_roles,
set_data_normalized,
truncate_and_annotate_messages,
Expand Down Expand Up @@ -159,7 +160,7 @@
f"invoke_agent {graph_name}".strip() if graph_name else "invoke_agent"
)

with sentry_sdk.start_span(
with get_start_span_function()(
op=OP.GEN_AI_INVOKE_AGENT,
name=span_name,
origin=LanggraphIntegration.origin,
Expand Down Expand Up @@ -219,7 +220,7 @@
f"invoke_agent {graph_name}".strip() if graph_name else "invoke_agent"
)

with sentry_sdk.start_span(
with get_start_span_function()(
op=OP.GEN_AI_INVOKE_AGENT,
name=span_name,
origin=LanggraphIntegration.origin,
Expand Down
13 changes: 7 additions & 6 deletions sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
from sentry_sdk.ai.monitoring import record_token_usage
from sentry_sdk.ai.utils import (
get_start_span_function,
normalize_message_roles,
set_data_normalized,
truncate_and_annotate_embedding_inputs,
Expand Down Expand Up @@ -713,7 +714,7 @@ def _new_sync_chat_completion(f: "Any", *args: "Any", **kwargs: "Any") -> "Any":

model = kwargs.get("model")

span = sentry_sdk.start_span(
span = get_start_span_function()(
op=consts.OP.GEN_AI_CHAT,
name=f"chat {model}",
origin=OpenAIIntegration.origin,
Expand Down Expand Up @@ -781,7 +782,7 @@ async def _new_async_chat_completion(f: "Any", *args: "Any", **kwargs: "Any") ->

model = kwargs.get("model")

span = sentry_sdk.start_span(
span = get_start_span_function()(
op=consts.OP.GEN_AI_CHAT,
name=f"chat {model}",
origin=OpenAIIntegration.origin,
Expand Down Expand Up @@ -1177,7 +1178,7 @@ def _new_sync_embeddings_create(f: "Any", *args: "Any", **kwargs: "Any") -> "Any

model = kwargs.get("model")

with sentry_sdk.start_span(
with get_start_span_function()(
op=consts.OP.GEN_AI_EMBEDDINGS,
name=f"embeddings {model}",
origin=OpenAIIntegration.origin,
Expand Down Expand Up @@ -1209,7 +1210,7 @@ async def _new_async_embeddings_create(

model = kwargs.get("model")

with sentry_sdk.start_span(
with get_start_span_function()(
op=consts.OP.GEN_AI_EMBEDDINGS,
name=f"embeddings {model}",
origin=OpenAIIntegration.origin,
Expand Down Expand Up @@ -1263,7 +1264,7 @@ def _new_sync_responses_create(f: "Any", *args: "Any", **kwargs: "Any") -> "Any"

model = kwargs.get("model")

span = sentry_sdk.start_span(
span = get_start_span_function()(
op=consts.OP.GEN_AI_RESPONSES,
name=f"responses {model}",
origin=OpenAIIntegration.origin,
Expand Down Expand Up @@ -1321,7 +1322,7 @@ async def _new_async_responses_create(f: "Any", *args: "Any", **kwargs: "Any") -

model = kwargs.get("model")

span = sentry_sdk.start_span(
span = get_start_span_function()(
op=consts.OP.GEN_AI_RESPONSES,
name=f"responses {model}",
origin=OpenAIIntegration.origin,
Expand Down
3 changes: 2 additions & 1 deletion tests/integrations/cohere/test_cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ def test_bad_chat(sentry_init, capture_events):
with pytest.raises(httpx.HTTPError):
client.chat(model="some-model", message="hello")

(event,) = events
(event, transaction) = events
assert event["level"] == "error"
assert transaction["contexts"]["trace"]["status"] == "internal_error"


def test_span_status_error(sentry_init, capture_events):
Expand Down
24 changes: 16 additions & 8 deletions tests/integrations/openai/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,7 @@ def test_bad_chat_completion(
)

if stream_gen_ai_spans:
items = capture_items("event")
items = capture_items("event", "transaction")

client = OpenAI(api_key="z")
client.chat.completions._post = mock.Mock(
Expand All @@ -2177,6 +2177,7 @@ def test_bad_chat_completion(
)

(event,) = (item.payload for item in items if item.type == "event")
(transaction,) = (item.payload for item in items if item.type == "transaction")
else:
events = capture_events()

Expand All @@ -2190,9 +2191,10 @@ def test_bad_chat_completion(
messages=[{"role": "system", "content": "hello"}],
)

(event,) = events
(event, transaction) = events

assert event["level"] == "error"
assert transaction["contexts"]["trace"]["status"] == "internal_error"


@pytest.mark.parametrize("stream_gen_ai_spans", [True, False])
Expand Down Expand Up @@ -2266,14 +2268,15 @@ async def test_bad_chat_completion_async(
side_effect=OpenAIError("API rate limit reached")
)
if stream_gen_ai_spans:
items = capture_items("event")
items = capture_items("event", "transaction")

with pytest.raises(OpenAIError):
await client.chat.completions.create(
model="some-model", messages=[{"role": "system", "content": "hello"}]
)

(event,) = (item.payload for item in items if item.type == "event")
(transaction,) = (item.payload for item in items if item.type == "transaction")
else:
events = capture_events()

Expand All @@ -2282,9 +2285,10 @@ async def test_bad_chat_completion_async(
model="some-model", messages=[{"role": "system", "content": "hello"}]
)

(event,) = events
(event, transaction) = events

assert event["level"] == "error"
assert transaction["contexts"]["trace"]["status"] == "internal_error"


@pytest.mark.parametrize("stream_gen_ai_spans", [True, False])
Expand Down Expand Up @@ -2834,21 +2838,23 @@ def test_embeddings_create_raises_error(
)

if stream_gen_ai_spans:
items = capture_items("event")
items = capture_items("event", "transaction")

with pytest.raises(OpenAIError):
client.embeddings.create(input="hello", model="text-embedding-3-large")

(event,) = (item.payload for item in items if item.type == "event")
(transaction,) = (item.payload for item in items if item.type == "transaction")
else:
events = capture_events()

with pytest.raises(OpenAIError):
client.embeddings.create(input="hello", model="text-embedding-3-large")

(event,) = events
(event, transaction) = events

assert event["level"] == "error"
assert transaction["contexts"]["trace"]["status"] == "internal_error"


@pytest.mark.parametrize("stream_gen_ai_spans", [True, False])
Expand Down Expand Up @@ -2879,14 +2885,15 @@ async def test_embeddings_create_raises_error_async(
)

if stream_gen_ai_spans:
items = capture_items("event")
items = capture_items("event", "transaction")

with pytest.raises(OpenAIError):
await client.embeddings.create(
input="hello", model="text-embedding-3-large"
)

(event,) = (item.payload for item in items if item.type == "event")
(transaction,) = (item.payload for item in items if item.type == "transaction")
else:
events = capture_events()

Expand All @@ -2895,9 +2902,10 @@ async def test_embeddings_create_raises_error_async(
input="hello", model="text-embedding-3-large"
)

(event,) = events
(event, transaction) = events

assert event["level"] == "error"
assert transaction["contexts"]["trace"]["status"] == "internal_error"


@pytest.mark.parametrize("stream_gen_ai_spans", [True, False])
Expand Down
Loading