Skip to content

Commit 324c8ba

Browse files
parametrize some langgraph helpers
1 parent 2804959 commit 324c8ba

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

sentry_sdk/integrations/langgraph.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from sentry_sdk.consts import OP, SPANDATA
1212
from sentry_sdk.integrations import DidNotEnable, Integration
1313
from sentry_sdk.scope import should_send_default_pii
14+
from sentry_sdk.traces import StreamedSpan
1415
from sentry_sdk.tracing_utils import (
1516
has_span_streaming_enabled,
1617
should_truncate_gen_ai_input,
@@ -389,14 +390,18 @@ def _set_usage_data(span: "sentry_sdk.tracing.Span", messages: "Any") -> None:
389390
output_tokens += int(token_usage.get("completion_tokens", 0))
390391
total_tokens += int(token_usage.get("total_tokens", 0))
391392

393+
set_on_span = (
394+
span.set_attribute if isinstance(span, StreamedSpan) else span.set_data
395+
)
396+
392397
if input_tokens > 0:
393-
span.set_data(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
398+
set_on_span(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens)
394399

395400
if output_tokens > 0:
396-
span.set_data(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
401+
set_on_span(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)
397402

398403
if total_tokens > 0:
399-
span.set_data(
404+
set_on_span(
400405
SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS,
401406
total_tokens,
402407
)
@@ -436,13 +441,17 @@ def _set_response_attributes(
436441
if not (should_send_default_pii() and integration.include_prompts):
437442
return
438443

444+
set_on_span = (
445+
span.set_attribute if isinstance(span, StreamedSpan) else span.set_data
446+
)
447+
439448
llm_response_text = _extract_llm_response_text(new_messages)
440449
if llm_response_text:
441-
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, llm_response_text)
450+
set_on_span(span, SPANDATA.GEN_AI_RESPONSE_TEXT, llm_response_text)
442451
elif new_messages:
443-
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, new_messages)
452+
set_on_span(span, SPANDATA.GEN_AI_RESPONSE_TEXT, new_messages)
444453
else:
445-
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, result)
454+
set_on_span(span, SPANDATA.GEN_AI_RESPONSE_TEXT, result)
446455

447456
tool_calls = _extract_tool_calls(new_messages)
448457
if tool_calls:

0 commit comments

Comments
 (0)