|
11 | 11 | from sentry_sdk.consts import OP, SPANDATA |
12 | 12 | from sentry_sdk.integrations import DidNotEnable, Integration |
13 | 13 | from sentry_sdk.scope import should_send_default_pii |
| 14 | +from sentry_sdk.traces import StreamedSpan |
14 | 15 | from sentry_sdk.tracing_utils import ( |
15 | 16 | has_span_streaming_enabled, |
16 | 17 | should_truncate_gen_ai_input, |
@@ -389,14 +390,18 @@ def _set_usage_data(span: "sentry_sdk.tracing.Span", messages: "Any") -> None: |
389 | 390 | output_tokens += int(token_usage.get("completion_tokens", 0)) |
390 | 391 | total_tokens += int(token_usage.get("total_tokens", 0)) |
391 | 392 |
|
| 393 | + set_on_span = ( |
| 394 | + span.set_attribute if isinstance(span, StreamedSpan) else span.set_data |
| 395 | + ) |
| 396 | + |
392 | 397 | 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) |
394 | 399 |
|
395 | 400 | 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) |
397 | 402 |
|
398 | 403 | if total_tokens > 0: |
399 | | - span.set_data( |
| 404 | + set_on_span( |
400 | 405 | SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, |
401 | 406 | total_tokens, |
402 | 407 | ) |
@@ -436,13 +441,17 @@ def _set_response_attributes( |
436 | 441 | if not (should_send_default_pii() and integration.include_prompts): |
437 | 442 | return |
438 | 443 |
|
| 444 | + set_on_span = ( |
| 445 | + span.set_attribute if isinstance(span, StreamedSpan) else span.set_data |
| 446 | + ) |
| 447 | + |
439 | 448 | llm_response_text = _extract_llm_response_text(new_messages) |
440 | 449 | 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) |
442 | 451 | 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) |
444 | 453 | else: |
445 | | - set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, result) |
| 454 | + set_on_span(span, SPANDATA.GEN_AI_RESPONSE_TEXT, result) |
446 | 455 |
|
447 | 456 | tool_calls = _extract_tool_calls(new_messages) |
448 | 457 | if tool_calls: |
|
0 commit comments