From cd5af76aa8bc0019032c1e062adbca1c9db11aae Mon Sep 17 00:00:00 2001 From: srothh Date: Wed, 25 Nov 2026 16:14:12 +0100 Subject: [PATCH 1/3] ref(attributes): Enforce sentry conventions in AI integrations Enforce sentry-conventions attribute names in AI integrations (anthropic, cohere, google_genai, huggingface_hub, litellm, mistral, openai, typesafe). #8323 PY-3090 --- sentry_sdk/integrations/anthropic.py | 63 ++++---- sentry_sdk/integrations/cohere.py | 27 ++-- .../integrations/google_genai/__init__.py | 68 +++++---- .../integrations/google_genai/streaming.py | 18 ++- sentry_sdk/integrations/google_genai/utils.py | 63 ++++---- sentry_sdk/integrations/huggingface_hub.py | 59 ++++---- sentry_sdk/integrations/litellm.py | 40 +++-- sentry_sdk/integrations/mistral.py | 88 ++++++----- sentry_sdk/integrations/openai.py | 142 ++++++++++-------- sentry_sdk/integrations/typesafe.py | 48 +++--- 10 files changed, 341 insertions(+), 275 deletions(-) diff --git a/sentry_sdk/integrations/anthropic.py b/sentry_sdk/integrations/anthropic.py index c09c0fdb9e..b1a0e38908 100644 --- a/sentry_sdk/integrations/anthropic.py +++ b/sentry_sdk/integrations/anthropic.py @@ -4,6 +4,8 @@ from functools import wraps from typing import TYPE_CHECKING, cast +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.ai.utils import ( GEN_AI_ALLOWED_MESSAGE_ROLES, @@ -531,19 +533,19 @@ def _set_common_input_data( """ Set input data for the span based on the provided keyword arguments for the anthropic message creation. """ - span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, "anthropic") - span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat") + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME, "anthropic") + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME, "chat") if max_tokens is not None and _is_given(max_tokens): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS, max_tokens) if model is not None and _is_given(model): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) if temperature is not None and _is_given(temperature): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE, temperature) if top_k is not None and _is_given(top_k): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_K, top_k) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_K, top_k) if top_p is not None and _is_given(top_p): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P, top_p) if sentry_sdk.get_client().options["data_collection"]["gen_ai"]["inputs"]: if tools is not None and _is_given(tools) and len(tools) > 0: # type: ignore @@ -556,7 +558,7 @@ def _set_common_input_data( if isinstance(system, str) or isinstance(system, Iterable): span.set_attribute( - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, json.dumps(_transform_system_instructions(system)), ) @@ -621,7 +623,9 @@ def _set_create_input_data( """ Set input data for the span based on the provided keyword arguments for the anthropic message creation. """ - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_STREAMING, kwargs.get("stream", False)) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING, kwargs.get("stream", False) + ) _set_common_input_data( span=span, @@ -713,11 +717,13 @@ def _set_output_data( """ Set output data for the span based on the AI response.""" if model is not None: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, model) if response_id is not None: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_ID, response_id) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID, response_id) if finish_reason is not None: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, [finish_reason]) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS, [finish_reason] + ) if sentry_sdk.get_client().options["data_collection"]["gen_ai"]["outputs"]: parts: "list[Union[TextPart, ReasoningPart]]" = [] @@ -742,27 +748,28 @@ def _set_output_data( if len(parts) > 0: set_data_normalized( span, - SPANDATA.GEN_AI_OUTPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES, [{"role": "assistant", "parts": parts}], unpack=False, ) if input_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) if cache_read_input_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, cache_read_input_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, + cache_read_input_tokens, ) if cache_write_input_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, cache_write_input_tokens, ) if output_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) if output_tokens_reasoning is not None: span.set_attribute( @@ -771,7 +778,7 @@ def _set_output_data( if input_tokens is not None and output_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, input_tokens + output_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, input_tokens + output_tokens ) @@ -799,8 +806,8 @@ def _sentry_patched_create_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any span = sentry_sdk.start_span( name=f"chat {model}".strip(), attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": AnthropicIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: AnthropicIntegration.origin, }, ) @@ -900,8 +907,8 @@ async def _sentry_patched_create_async( span = sentry_sdk.start_span( name=f"chat {model}".strip(), attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": AnthropicIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: AnthropicIntegration.origin, }, ) @@ -1155,9 +1162,9 @@ def _sentry_patched_enter(self: "MessageStreamManager") -> "MessageStream": if patched_self._model is None else f"chat {patched_self._model}".strip(), attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": AnthropicIntegration.origin, - SPANDATA.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: AnthropicIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, }, ) @@ -1259,9 +1266,9 @@ async def _sentry_patched_aenter( if patched_self._model is None else f"chat {patched_self._model}".strip(), attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": AnthropicIntegration.origin, - SPANDATA.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: AnthropicIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, }, ) diff --git a/sentry_sdk/integrations/cohere.py b/sentry_sdk/integrations/cohere.py index 981da7b475..e08b57976b 100644 --- a/sentry_sdk/integrations/cohere.py +++ b/sentry_sdk/integrations/cohere.py @@ -2,6 +2,8 @@ from functools import wraps from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + from sentry_sdk import consts from sentry_sdk.ai.utils import set_data_normalized from sentry_sdk.consts import SPANDATA @@ -120,13 +122,13 @@ def collect_chat_response_fields( if hasattr(res.meta, "billed_units"): if res.meta.billed_units.input_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, res.meta.billed_units.input_tokens, ) if res.meta.billed_units.output_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, res.meta.billed_units.output_tokens, ) @@ -135,19 +137,20 @@ def collect_chat_response_fields( and res.meta.billed_units.output_tokens is not None ): span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, res.meta.billed_units.input_tokens + res.meta.billed_units.output_tokens, ) elif hasattr(res.meta, "tokens"): if res.meta.tokens.input_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, res.meta.tokens.input_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, + res.meta.tokens.input_tokens, ) if res.meta.tokens.output_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, res.meta.tokens.output_tokens, ) @@ -156,7 +159,7 @@ def collect_chat_response_fields( and res.meta.tokens.output_tokens is not None ): span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, res.meta.tokens.input_tokens + res.meta.tokens.output_tokens, ) @@ -179,8 +182,8 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any": span = sentry_sdk.start_span( name="cohere.client.Chat", attributes={ - "sentry.op": consts.OP.GEN_AI_CHAT, - "sentry.origin": CohereIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: CohereIntegration.origin, }, ) @@ -273,8 +276,8 @@ def new_embed(*args: "Any", **kwargs: "Any") -> "Any": with sentry_sdk.start_span( name="Cohere Embedding Creation", attributes={ - "sentry.op": consts.OP.GEN_AI_EMBEDDINGS, - "sentry.origin": CohereIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_EMBEDDINGS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: CohereIntegration.origin, }, ) as span: if ( @@ -310,12 +313,12 @@ def new_embed(*args: "Any", **kwargs: "Any") -> "Any": ): if res.meta.billed_units.input_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, res.meta.billed_units.input_tokens, ) span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, res.meta.billed_units.input_tokens, ) return res diff --git a/sentry_sdk/integrations/google_genai/__init__.py b/sentry_sdk/integrations/google_genai/__init__.py index f7a67d6625..5aacf4acc9 100644 --- a/sentry_sdk/integrations/google_genai/__init__.py +++ b/sentry_sdk/integrations/google_genai/__init__.py @@ -7,8 +7,10 @@ List, ) +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.traces import SpanStatus from sentry_sdk.utils import parse_version @@ -80,12 +82,12 @@ def new_generate_content_stream( chat_span = sentry_sdk.start_span( name=f"chat {model_name}", attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": ORIGIN, - SPANDATA.GEN_AI_OPERATION_NAME: "chat", - SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, - SPANDATA.GEN_AI_REQUEST_MODEL: model_name, - SPANDATA.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN, + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, }, ) @@ -144,12 +146,12 @@ async def new_async_generate_content_stream( chat_span = sentry_sdk.start_span( name=f"chat {model_name}", attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": ORIGIN, - SPANDATA.GEN_AI_OPERATION_NAME: "chat", - SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, - SPANDATA.GEN_AI_REQUEST_MODEL: model_name, - SPANDATA.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN, + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, }, ) @@ -204,11 +206,11 @@ def new_generate_content(self: "Any", *args: "Any", **kwargs: "Any") -> "Any": with sentry_sdk.start_span( name=f"chat {model_name}", attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": ORIGIN, - SPANDATA.GEN_AI_OPERATION_NAME: "chat", - SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, - SPANDATA.GEN_AI_REQUEST_MODEL: model_name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN, + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name, }, ) as chat_span: set_span_data_for_request( @@ -247,11 +249,11 @@ async def new_async_generate_content( with sentry_sdk.start_span( name=f"chat {model_name}", attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": ORIGIN, - SPANDATA.GEN_AI_OPERATION_NAME: "chat", - SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, - SPANDATA.GEN_AI_REQUEST_MODEL: model_name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN, + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name, }, ) as chat_span: set_span_data_for_request( @@ -284,11 +286,11 @@ def new_embed_content(self: "Any", *args: "Any", **kwargs: "Any") -> "Any": with sentry_sdk.start_span( name=f"embeddings {model_name}", attributes={ - "sentry.op": OP.GEN_AI_EMBEDDINGS, - "sentry.origin": ORIGIN, - SPANDATA.GEN_AI_OPERATION_NAME: "embeddings", - SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, - SPANDATA.GEN_AI_REQUEST_MODEL: model_name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_EMBEDDINGS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN, + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "embeddings", + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name, }, ) as span: set_span_data_for_embed_request(span, integration, contents, kwargs) @@ -322,11 +324,11 @@ async def new_async_embed_content( with sentry_sdk.start_span( name=f"embeddings {model_name}", attributes={ - "sentry.op": OP.GEN_AI_EMBEDDINGS, - "sentry.origin": ORIGIN, - SPANDATA.GEN_AI_OPERATION_NAME: "embeddings", - SPANDATA.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, - SPANDATA.GEN_AI_REQUEST_MODEL: model_name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_EMBEDDINGS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN, + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "embeddings", + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: GEN_AI_SYSTEM, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: model_name, }, ) as span: set_span_data_for_embed_request(span, integration, contents, kwargs) diff --git a/sentry_sdk/integrations/google_genai/streaming.py b/sentry_sdk/integrations/google_genai/streaming.py index 55bc70ac84..e0b5e42bbc 100644 --- a/sentry_sdk/integrations/google_genai/streaming.py +++ b/sentry_sdk/integrations/google_genai/streaming.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING, Any, List, Optional, TypedDict +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.ai.utils import set_data_normalized from sentry_sdk.consts import SPANDATA @@ -106,48 +108,48 @@ def set_span_data_for_streaming_response( if accumulated_response.get("finish_reasons"): set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS, accumulated_response["finish_reasons"], ) response_id = accumulated_response.get("id") if response_id is not None: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_ID, response_id) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID, response_id) response_model = accumulated_response.get("model") if response_model is not None: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, response_model) if accumulated_response["usage_metadata"] is None: return if accumulated_response["usage_metadata"]["input_tokens"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, accumulated_response["usage_metadata"]["input_tokens"], ) if accumulated_response["usage_metadata"]["input_tokens_cached"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, accumulated_response["usage_metadata"]["input_tokens_cached"], ) if accumulated_response["usage_metadata"]["output_tokens"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, accumulated_response["usage_metadata"]["output_tokens"], ) if accumulated_response["usage_metadata"]["output_tokens_reasoning"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, accumulated_response["usage_metadata"]["output_tokens_reasoning"], ) if accumulated_response["usage_metadata"]["total_tokens"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, accumulated_response["usage_metadata"]["total_tokens"], ) diff --git a/sentry_sdk/integrations/google_genai/utils.py b/sentry_sdk/integrations/google_genai/utils.py index c9cb1e3f51..1fdd747667 100644 --- a/sentry_sdk/integrations/google_genai/utils.py +++ b/sentry_sdk/integrations/google_genai/utils.py @@ -17,6 +17,7 @@ ) from google.genai.types import Content, GenerateContentConfig, Part, PartDict +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk._types import BLOB_DATA_SUBSTITUTE @@ -681,13 +682,13 @@ def _create_tool_span(tool_name: str, tool_doc: "Optional[str]") -> "Span": span = sentry_sdk.start_span( name=f"execute_tool {tool_name}", attributes={ - "sentry.op": OP.GEN_AI_EXECUTE_TOOL, - "sentry.origin": ORIGIN, - SPANDATA.GEN_AI_TOOL_NAME: tool_name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_EXECUTE_TOOL, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ORIGIN, + ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME: tool_name, }, ) if tool_doc: - span.set_attribute(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool_doc) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_TOOL_DESCRIPTION, tool_doc) return span @@ -709,7 +710,8 @@ async def async_wrapped(*args: "Any", **kwargs: "Any") -> "Any": tool_input = _capture_tool_input(args, kwargs, tool) with capture_internal_exceptions(): span.set_attribute( - SPANDATA.GEN_AI_TOOL_CALL_ARGUMENTS, safe_serialize(tool_input) + ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_ARGUMENTS, + safe_serialize(tool_input), ) try: @@ -718,7 +720,8 @@ async def async_wrapped(*args: "Any", **kwargs: "Any") -> "Any": # Capture tool output with capture_internal_exceptions(): span.set_attribute( - SPANDATA.GEN_AI_TOOL_CALL_RESULT, safe_serialize(result) + ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT, + safe_serialize(result), ) return result @@ -736,7 +739,8 @@ def sync_wrapped(*args: "Any", **kwargs: "Any") -> "Any": tool_input = _capture_tool_input(args, kwargs, tool) with capture_internal_exceptions(): span.set_attribute( - SPANDATA.GEN_AI_TOOL_CALL_ARGUMENTS, safe_serialize(tool_input) + ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_ARGUMENTS, + safe_serialize(tool_input), ) try: @@ -745,7 +749,8 @@ def sync_wrapped(*args: "Any", **kwargs: "Any") -> "Any": # Capture tool output with capture_internal_exceptions(): span.set_attribute( - SPANDATA.GEN_AI_TOOL_CALL_RESULT, safe_serialize(result) + ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT, + safe_serialize(result), ) return result @@ -902,11 +907,11 @@ def set_span_data_for_request( ) -> None: """Set span data for the request.""" client = sentry_sdk.get_client() - span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, GEN_AI_SYSTEM) - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME, GEN_AI_SYSTEM) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) if kwargs.get("stream", False): - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_STREAMING, True) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING, True) config: "Optional[GenerateContentConfig]" = kwargs.get("config") @@ -940,7 +945,7 @@ def set_span_data_for_request( if system_instructions is not None: span.set_attribute( - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, json.dumps(_transform_system_instructions(system_instructions)), ) @@ -959,13 +964,13 @@ def set_span_data_for_request( # Extract parameters directly from config (not nested under generation_config) for param, span_key in [ - ("temperature", SPANDATA.GEN_AI_REQUEST_TEMPERATURE), - ("top_p", SPANDATA.GEN_AI_REQUEST_TOP_P), - ("top_k", SPANDATA.GEN_AI_REQUEST_TOP_K), - ("max_output_tokens", SPANDATA.GEN_AI_REQUEST_MAX_TOKENS), - ("presence_penalty", SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY), - ("frequency_penalty", SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY), - ("seed", SPANDATA.GEN_AI_REQUEST_SEED), + ("temperature", ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE), + ("top_p", ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P), + ("top_k", ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_K), + ("max_output_tokens", ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS), + ("presence_penalty", ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY), + ("frequency_penalty", ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY), + ("seed", ATTRIBUTE_NAMES.GEN_AI_REQUEST_SEED), ]: if hasattr(config, param): value = getattr(config, param) @@ -986,44 +991,44 @@ def set_span_data_for_response( finish_reasons = extract_finish_reasons(response) if finish_reasons: set_data_normalized( - span, SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons ) response_id = getattr(response, "response_id", None) if response_id is not None: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_ID, response_id) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID, response_id) model_version = getattr(response, "model_version", None) if model_version is not None: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, model_version) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, model_version) usage_data = extract_usage_data(response) if usage_data["input_tokens"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, usage_data["input_tokens"] + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, usage_data["input_tokens"] ) if usage_data["input_tokens_cached"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, usage_data["input_tokens_cached"], ) if usage_data["output_tokens"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, usage_data["output_tokens"] + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, usage_data["output_tokens"] ) if usage_data["output_tokens_reasoning"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, usage_data["output_tokens_reasoning"], ) if usage_data["total_tokens"]: span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, usage_data["total_tokens"] + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, usage_data["total_tokens"] ) tool_calls = extract_tool_calls(response) @@ -1102,7 +1107,7 @@ def set_span_data_for_embed_request( if input_texts: set_data_normalized( span, - SPANDATA.GEN_AI_EMBEDDINGS_INPUT, + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT, input_texts, unpack=False, ) @@ -1130,4 +1135,4 @@ def set_span_data_for_embed_response( # Set token count if we found any if total_tokens > 0: - span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, total_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, total_tokens) diff --git a/sentry_sdk/integrations/huggingface_hub.py b/sentry_sdk/integrations/huggingface_hub.py index 50d39cd38c..bd9742b10b 100644 --- a/sentry_sdk/integrations/huggingface_hub.py +++ b/sentry_sdk/integrations/huggingface_hub.py @@ -3,6 +3,8 @@ from functools import wraps from typing import TYPE_CHECKING, cast +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.ai.utils import set_data_normalized from sentry_sdk.consts import OP, SPANDATA @@ -89,24 +91,24 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": span = sentry_sdk.start_span( name=f"{operation_name} {model}", attributes={ - "sentry.op": op, - "sentry.origin": HuggingfaceHubIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: op, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: HuggingfaceHubIntegration.origin, }, ) - span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, operation_name) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME, operation_name) if model: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) attribute_mapping = { - "frequency_penalty": SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, - "max_tokens": SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, - "presence_penalty": SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, - "temperature": SPANDATA.GEN_AI_REQUEST_TEMPERATURE, - "top_p": SPANDATA.GEN_AI_REQUEST_TOP_P, - "top_k": SPANDATA.GEN_AI_REQUEST_TOP_K, - "stream": SPANDATA.GEN_AI_RESPONSE_STREAMING, + "frequency_penalty": ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY, + "max_tokens": ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS, + "presence_penalty": ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY, + "temperature": ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE, + "top_p": ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P, + "top_k": ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_K, + "stream": ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING, } if client.options["data_collection"]["gen_ai"]["inputs"]: @@ -180,12 +182,14 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": response_text_buffer.append(choice.message.content) if response_model is not None: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, response_model + ) if finish_reason is not None: set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS, finish_reason, ) @@ -211,17 +215,18 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": if usage is not None: if usage is not None and usage.prompt_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, usage.prompt_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, usage.prompt_tokens ) if usage is not None and usage.completion_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, usage.completion_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, + usage.completion_tokens, ) if usage is not None and usage.total_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, usage.total_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, usage.total_tokens ) elif ( @@ -230,12 +235,12 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": and usage.completion_tokens is not None ): span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, usage.prompt_tokens + usage.completion_tokens, ) elif tokens_used > 0: span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, tokens_used, ) @@ -285,7 +290,7 @@ def new_details_iterator() -> "Iterable[Any]": if finish_reason is not None: set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS, finish_reason, ) @@ -301,7 +306,7 @@ def new_details_iterator() -> "Iterable[Any]": if tokens_used > 0: span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, tokens_used + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, tokens_used ) span.__exit__(None, None, None) @@ -367,13 +372,13 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]": with capture_internal_exceptions(): if response_model is not None: span.set_attribute( - SPANDATA.GEN_AI_RESPONSE_MODEL, response_model + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, response_model ) if finish_reason is not None: set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS, finish_reason, ) @@ -407,25 +412,27 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]": if usage.prompt_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, usage.prompt_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, + usage.prompt_tokens, ) if usage.completion_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, usage.completion_tokens, ) if usage.total_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, usage.total_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, + usage.total_tokens, ) elif ( usage.prompt_tokens is not None and usage.completion_tokens is not None ): span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, usage.prompt_tokens + usage.completion_tokens, ) diff --git a/sentry_sdk/integrations/litellm.py b/sentry_sdk/integrations/litellm.py index 6d366054f4..d2d13bf9c3 100644 --- a/sentry_sdk/integrations/litellm.py +++ b/sentry_sdk/integrations/litellm.py @@ -1,6 +1,8 @@ import copy from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk import consts from sentry_sdk.ai.utils import ( @@ -95,20 +97,20 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None: span = sentry_sdk.start_span( name=f"{operation} {model}", attributes={ - "sentry.op": ( + ATTRIBUTE_NAMES.SENTRY_OP: ( consts.OP.GEN_AI_CHAT if operation == "chat" else consts.OP.GEN_AI_EMBEDDINGS ), - "sentry.origin": LiteLLMIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: LiteLLMIntegration.origin, }, ) _store_span(kwargs, span) # Set basic data - set_data_normalized(span, SPANDATA.GEN_AI_PROVIDER_NAME, provider) - set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, operation) + set_data_normalized(span, ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME, provider) + set_data_normalized(span, ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME, operation) if client.options["data_collection"]["gen_ai"]["inputs"]: if operation == "embeddings": @@ -124,7 +126,7 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None: if input_list is not None: set_data_normalized( span, - SPANDATA.GEN_AI_EMBEDDINGS_INPUT, + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT, input_list, unpack=False, ) @@ -142,13 +144,13 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None: # Record other parameters params = { - "model": SPANDATA.GEN_AI_REQUEST_MODEL, - "stream": SPANDATA.GEN_AI_RESPONSE_STREAMING, - "max_tokens": SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, - "presence_penalty": SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, - "frequency_penalty": SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, - "temperature": SPANDATA.GEN_AI_REQUEST_TEMPERATURE, - "top_p": SPANDATA.GEN_AI_REQUEST_TOP_P, + "model": ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, + "stream": ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING, + "max_tokens": ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS, + "presence_penalty": ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY, + "frequency_penalty": ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY, + "temperature": ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE, + "top_p": ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P, } for key, attribute in params.items(): value = kwargs.get(key) @@ -181,7 +183,7 @@ def _success_callback( # Record model information if hasattr(completion_response, "model"): set_data_normalized( - span, SPANDATA.GEN_AI_RESPONSE_MODEL, completion_response.model + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, completion_response.model ) if client.options["data_collection"]["gen_ai"]["outputs"]: @@ -215,11 +217,15 @@ def _success_callback( input_tokens = getattr(usage, "prompt_tokens", None) if input_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, input_tokens + ) output_tokens = getattr(usage, "completion_tokens", None) if output_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens + ) total_tokens = getattr(usage, "total_tokens", None) if ( @@ -230,7 +236,9 @@ def _success_callback( total_tokens = input_tokens + output_tokens if total_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens + ) finally: is_streaming = kwargs.get("stream") diff --git a/sentry_sdk/integrations/mistral.py b/sentry_sdk/integrations/mistral.py index 021108784c..1770d76cec 100644 --- a/sentry_sdk/integrations/mistral.py +++ b/sentry_sdk/integrations/mistral.py @@ -3,8 +3,10 @@ from functools import wraps from typing import TYPE_CHECKING, cast +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable, Integration if TYPE_CHECKING: @@ -231,16 +233,16 @@ def wrap_complete(self: "Chat", *args: "Any", **kwargs: "Any") -> "Any": with sentry_sdk.start_span( name=f"chat {model}" if model is not None else "chat", attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": MistralIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "mistral", - SPANDATA.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: MistralIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "mistral", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", }, ) as span: if model is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_STREAMING, False) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING, False) messages = kwargs.get("messages") if ( @@ -252,43 +254,47 @@ def wrap_complete(self: "Chat", *args: "Any", **kwargs: "Any") -> "Any": ] if len(system_instructions) > 0: span.set_attribute( - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, json.dumps(_transform_system_instructions(system_instructions)), ) span.set_attribute( - SPANDATA.GEN_AI_INPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES, json.dumps(_transform_input_messages(messages)), ) max_tokens = kwargs.get("max_tokens") if max_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS, max_tokens + ) frequency_penalty = kwargs.get("frequency_penalty") if frequency_penalty is not None: span.set_attribute( - SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty + ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty ) presence_penalty = kwargs.get("presence_penalty") if presence_penalty is not None: span.set_attribute( - SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty + ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty ) temperature = kwargs.get("temperature") if temperature is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE, temperature + ) top_p = kwargs.get("top_p") if top_p is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P, top_p) reasoning_effort = kwargs.get("reasoning_effort") if reasoning_effort is not None: span.set_attribute( - SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL, reasoning_effort + ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL, reasoning_effort ) response = f(self, *args, **kwargs) @@ -298,18 +304,20 @@ def wrap_complete(self: "Chat", *args: "Any", **kwargs: "Any") -> "Any": if response.usage.prompt_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, response.usage.prompt_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, + response.usage.prompt_tokens, ) if response.usage.completion_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, response.usage.completion_tokens, ) if response.usage.total_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, response.usage.total_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, + response.usage.total_tokens, ) if not client.options["data_collection"]["gen_ai"]["outputs"]: @@ -327,7 +335,7 @@ def wrap_complete(self: "Chat", *args: "Any", **kwargs: "Any") -> "Any": output_messages.append(transformed_message) span.set_attribute( - SPANDATA.GEN_AI_OUTPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES, json.dumps(output_messages), ) @@ -349,16 +357,16 @@ async def wrap_complete_async(self: "Chat", *args: "Any", **kwargs: "Any") -> "A with sentry_sdk.start_span( name=f"chat {model}" if model is not None else "chat", attributes={ - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": MistralIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "mistral", - SPANDATA.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: MistralIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "mistral", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", }, ) as span: if model is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_STREAMING, False) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING, False) messages: "Optional[Union[Iterable[ChatCompletionRequestMessage], Iterable[ChatCompletionRequestMessageTypedDict]]]" = kwargs.get( "messages" @@ -372,43 +380,47 @@ async def wrap_complete_async(self: "Chat", *args: "Any", **kwargs: "Any") -> "A ] if len(system_instructions) > 0: span.set_attribute( - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, json.dumps(_transform_system_instructions(system_instructions)), ) span.set_attribute( - SPANDATA.GEN_AI_INPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES, json.dumps(_transform_input_messages(messages)), ) max_tokens = kwargs.get("max_tokens") if max_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS, max_tokens + ) frequency_penalty = kwargs.get("frequency_penalty") if frequency_penalty is not None: span.set_attribute( - SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty + ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty ) presence_penalty = kwargs.get("presence_penalty") if presence_penalty is not None: span.set_attribute( - SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty + ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty ) temperature = kwargs.get("temperature") if temperature is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE, temperature + ) top_p = kwargs.get("top_p") if top_p is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P, top_p) reasoning_effort = kwargs.get("reasoning_effort") if reasoning_effort is not None: span.set_attribute( - SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL, reasoning_effort + ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL, reasoning_effort ) response = await f(self, *args, **kwargs) @@ -418,18 +430,20 @@ async def wrap_complete_async(self: "Chat", *args: "Any", **kwargs: "Any") -> "A if response.usage.prompt_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, response.usage.prompt_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, + response.usage.prompt_tokens, ) if response.usage.completion_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, response.usage.completion_tokens, ) if response.usage.total_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, response.usage.total_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, + response.usage.total_tokens, ) if not client.options["data_collection"]["gen_ai"]["outputs"]: @@ -447,7 +461,7 @@ async def wrap_complete_async(self: "Chat", *args: "Any", **kwargs: "Any") -> "A output_messages.append(transformed_message) span.set_attribute( - SPANDATA.GEN_AI_OUTPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES, json.dumps(output_messages), ) diff --git a/sentry_sdk/integrations/openai.py b/sentry_sdk/integrations/openai.py index e2fde6af33..3ab7e23657 100644 --- a/sentry_sdk/integrations/openai.py +++ b/sentry_sdk/integrations/openai.py @@ -5,6 +5,8 @@ from functools import wraps from typing import TYPE_CHECKING, cast +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk import consts from sentry_sdk.ai._openai_completions_api import ( @@ -233,22 +235,22 @@ def _calculate_completions_token_usage( # Do not set token data if it is 0 input_tokens = input_tokens or None if input_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) input_tokens_cached = input_tokens_cached or None if input_tokens_cached is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, input_tokens_cached + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, input_tokens_cached ) output_tokens = output_tokens or None if output_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) output_tokens_reasoning = output_tokens_reasoning or None if output_tokens_reasoning is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, output_tokens_reasoning, ) @@ -257,7 +259,7 @@ def _calculate_completions_token_usage( total_tokens = input_tokens + output_tokens if total_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens) def _calculate_responses_token_usage( @@ -324,22 +326,22 @@ def _calculate_responses_token_usage( # Do not set token data if it is 0 input_tokens = input_tokens or None if input_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) input_tokens_cached = input_tokens_cached or None if input_tokens_cached is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, input_tokens_cached + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, input_tokens_cached ) output_tokens = output_tokens or None if output_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) output_tokens_reasoning = output_tokens_reasoning or None if output_tokens_reasoning is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, + ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS, output_tokens_reasoning, ) @@ -348,7 +350,7 @@ def _calculate_responses_token_usage( total_tokens = input_tokens + output_tokens if total_tokens is not None: - span.set_attribute(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens) def _set_responses_api_input_data( @@ -356,23 +358,23 @@ def _set_responses_api_input_data( kwargs: "dict[str, Any]", integration: "OpenAIIntegration", ) -> None: - set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "chat") + set_data_normalized(span, ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME, "chat") model = kwargs.get("model") if model is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) max_tokens = kwargs.get("max_output_tokens") if max_tokens is not None and _is_given(max_tokens): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS, max_tokens) temperature = kwargs.get("temperature") if temperature is not None and _is_given(temperature): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE, temperature) top_p = kwargs.get("top_p") if top_p is not None and _is_given(top_p): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P, top_p) conversation = kwargs.get("conversation") if conversation is not None and _is_given(conversation): @@ -382,12 +384,12 @@ def _set_responses_api_input_data( elif isinstance(conversation, dict): conversation_id = conversation.get("id") if conversation_id is not None: - span.set_attribute(SPANDATA.GEN_AI_CONVERSATION_ID, conversation_id) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID, conversation_id) reasoning = kwargs.get("reasoning") if isinstance(reasoning, dict) and "effort" in reasoning: span.set_attribute( - SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL, reasoning["effort"], ) @@ -397,7 +399,7 @@ def _set_responses_api_input_data( tools = kwargs.get("tools") if tools is not None and _is_given(tools): span.set_attribute( - SPANDATA.GEN_AI_TOOL_DEFINITIONS, + ATTRIBUTE_NAMES.GEN_AI_TOOL_DEFINITIONS, json.dumps(_transform_tool_definitions_responses(tools)), ) @@ -411,7 +413,7 @@ def _set_responses_api_input_data( if messages is None: if has_explicit_instructions: span.set_attribute( - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, json.dumps( [ { @@ -438,7 +440,7 @@ def _set_responses_api_input_data( ) if len(instructions_text_parts) > 0: span.set_attribute( - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, json.dumps(instructions_text_parts), ) @@ -472,35 +474,41 @@ def _set_completions_api_input_data( kwargs: "dict[str, Any]", integration: "OpenAIIntegration", ) -> None: - set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "chat") + set_data_normalized(span, ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME, "chat") model = kwargs.get("model") if model is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) max_tokens = kwargs.get("max_tokens") if max_tokens is not None and _is_given(max_tokens): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS, max_tokens) presence_penalty = kwargs.get("presence_penalty") if presence_penalty is not None and _is_given(presence_penalty): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty + ) frequency_penalty = kwargs.get("frequency_penalty") if frequency_penalty is not None and _is_given(frequency_penalty): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty + ) temperature = kwargs.get("temperature") if temperature is not None and _is_given(temperature): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE, temperature) top_p = kwargs.get("top_p") if top_p is not None and _is_given(top_p): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P, top_p) reasoning_level = kwargs.get("reasoning_effort") if reasoning_level is not None and _is_given(reasoning_level): - span.set_attribute(SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL, reasoning_level) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL, reasoning_level + ) if not sentry_sdk.get_client().options["data_collection"]["gen_ai"]["inputs"]: return @@ -508,7 +516,7 @@ def _set_completions_api_input_data( tools = kwargs.get("tools") if tools is not None and _is_given(tools): span.set_attribute( - SPANDATA.GEN_AI_TOOL_DEFINITIONS, + ATTRIBUTE_NAMES.GEN_AI_TOOL_DEFINITIONS, json.dumps(_transform_tool_definitions_completions(tools)), ) @@ -539,7 +547,7 @@ def _set_completions_api_input_data( system_instructions = _get_system_instructions_completions(messages) if len(system_instructions) > 0: span.set_attribute( - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, json.dumps(_transform_system_instructions_completions(system_instructions)), ) @@ -564,11 +572,11 @@ def _set_embeddings_input_data( integration: "OpenAIIntegration", ) -> None: - set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "embeddings") + set_data_normalized(span, ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME, "embeddings") model = kwargs.get("model") if model is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) if not sentry_sdk.get_client().options["data_collection"]["gen_ai"]["inputs"]: return @@ -585,7 +593,7 @@ def _set_embeddings_input_data( if normalized_messages is not None: set_data_normalized( span, - SPANDATA.GEN_AI_EMBEDDINGS_INPUT, + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT, normalized_messages, unpack=False, ) @@ -604,7 +612,7 @@ def _set_embeddings_input_data( if normalized_messages is not None: set_data_normalized( span, - SPANDATA.GEN_AI_EMBEDDINGS_INPUT, + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT, normalized_messages, unpack=False, ) @@ -619,7 +627,7 @@ def _set_common_output_data( ) -> None: client = sentry_sdk.get_client() if hasattr(response, "model"): - set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_MODEL, response.model) + set_data_normalized(span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, response.model) # Chat Completions API if hasattr(response, "choices") and response.choices is not None: @@ -733,10 +741,10 @@ def _new_sync_chat_completion( span = sentry_sdk.start_span( name=f"chat {model}", attributes={ - "sentry.op": consts.OP.GEN_AI_CHAT, - "sentry.origin": OpenAIIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", - SPANDATA.GEN_AI_RESPONSE_STREAMING: is_streaming_response, + ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: OpenAIIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: is_streaming_response, }, ) @@ -811,10 +819,10 @@ async def _new_async_chat_completion( span = sentry_sdk.start_span( name=f"chat {model}", attributes={ - "sentry.op": consts.OP.GEN_AI_CHAT, - "sentry.origin": OpenAIIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", - SPANDATA.GEN_AI_RESPONSE_STREAMING: is_streaming_response, + ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: OpenAIIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: is_streaming_response, }, ) @@ -896,7 +904,7 @@ def _wrap_synchronous_completions_chunk_iterator( client = sentry_sdk.get_client() for x in old_iterator: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, x.model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, x.model) with capture_internal_exceptions(): if hasattr(x, "choices") and x.choices is not None: @@ -918,7 +926,7 @@ def _wrap_synchronous_completions_chunk_iterator( with capture_internal_exceptions(): if ttft is not None: set_data_normalized( - span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft ) all_responses = None if len(data_buf) > 0: @@ -959,7 +967,7 @@ async def _wrap_asynchronous_completions_chunk_iterator( client = sentry_sdk.get_client() async for x in old_iterator: - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, x.model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, x.model) with capture_internal_exceptions(): if hasattr(x, "choices") and x.choices is not None: @@ -981,7 +989,7 @@ async def _wrap_asynchronous_completions_chunk_iterator( with capture_internal_exceptions(): if ttft is not None: set_data_normalized( - span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft ) all_responses = None if len(data_buf) > 0: @@ -1031,7 +1039,9 @@ def _wrap_synchronous_responses_event_iterator( data_buf[0].append(x.delta or "") elif isinstance(x, ResponseCompletedEvent): - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, x.response.model) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, x.response.model + ) _calculate_responses_token_usage( input=input, @@ -1047,7 +1057,7 @@ def _wrap_synchronous_responses_event_iterator( with capture_internal_exceptions(): if ttft is not None: set_data_normalized( - span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft ) if len(data_buf) > 0: all_responses = ["".join(chunk) for chunk in data_buf] @@ -1096,7 +1106,9 @@ async def _wrap_asynchronous_responses_event_iterator( data_buf[0].append(x.delta or "") elif isinstance(x, ResponseCompletedEvent): - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, x.response.model) + span.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, x.response.model + ) _calculate_responses_token_usage( input=input, @@ -1112,7 +1124,7 @@ async def _wrap_asynchronous_responses_event_iterator( with capture_internal_exceptions(): if ttft is not None: set_data_normalized( - span, SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft ) if len(data_buf) > 0: all_responses = ["".join(chunk) for chunk in data_buf] @@ -1217,9 +1229,9 @@ def _new_sync_embeddings_create(f: "Any", *args: "Any", **kwargs: "Any") -> "Any with sentry_sdk.start_span( name=f"embeddings {model}", attributes={ - "sentry.op": consts.OP.GEN_AI_EMBEDDINGS, - "sentry.origin": OpenAIIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_EMBEDDINGS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: OpenAIIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", }, ) as span: _set_embeddings_input_data(span, kwargs, integration) @@ -1254,9 +1266,9 @@ async def _new_async_embeddings_create( with sentry_sdk.start_span( name=f"embeddings {model}", attributes={ - "sentry.op": consts.OP.GEN_AI_EMBEDDINGS, - "sentry.origin": OpenAIIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_EMBEDDINGS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: OpenAIIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", }, ) as span: _set_embeddings_input_data(span, kwargs, integration) @@ -1321,10 +1333,10 @@ def _new_sync_responses_create( span = sentry_sdk.start_span( name=f"responses {model}", attributes={ - "sentry.op": consts.OP.GEN_AI_CHAT, - "sentry.origin": OpenAIIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", - SPANDATA.GEN_AI_RESPONSE_STREAMING: is_streaming_response, + ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: OpenAIIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: is_streaming_response, }, ) @@ -1389,10 +1401,10 @@ async def _new_async_responses_create( span = sentry_sdk.start_span( name=f"responses {model}", attributes={ - "sentry.op": consts.OP.GEN_AI_CHAT, - "sentry.origin": OpenAIIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", - SPANDATA.GEN_AI_RESPONSE_STREAMING: is_streaming_response, + ATTRIBUTE_NAMES.SENTRY_OP: consts.OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: OpenAIIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: is_streaming_response, }, ) diff --git a/sentry_sdk/integrations/typesafe.py b/sentry_sdk/integrations/typesafe.py index 205c201d1d..ee8a7fe8fe 100644 --- a/sentry_sdk/integrations/typesafe.py +++ b/sentry_sdk/integrations/typesafe.py @@ -3,8 +3,10 @@ from functools import wraps from typing import TYPE_CHECKING, cast +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable, Integration if TYPE_CHECKING: @@ -241,14 +243,14 @@ def wrap_system_one(self: "TypeSafeClient", *args: "Any", **kwargs: "Any") -> "A with sentry_sdk.start_span( name=f"decide {model}".strip(), attributes={ - "sentry.op": OP.GEN_AI_DECIDE, - "sentry.origin": TypeSafeIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "typesafe", - SPANDATA.GEN_AI_OPERATION_NAME: "decide", + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_DECIDE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: TypeSafeIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "typesafe", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "decide", }, ) as span: if model is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) if client.options["data_collection"]["gen_ai"]["inputs"]: input_message: "InputMessageModel" = { @@ -264,7 +266,7 @@ def wrap_system_one(self: "TypeSafeClient", *args: "Any", **kwargs: "Any") -> "A input_message["questions"] = _transform_questions(questions) span.set_attribute( - SPANDATA.GEN_AI_INPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES, json.dumps([input_message], default=_json_default), ) @@ -273,23 +275,25 @@ def wrap_system_one(self: "TypeSafeClient", *args: "Any", **kwargs: "Any") -> "A if not isinstance(response, SystemOneResponse): return response - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, response.model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, response.model) if response.usage.input_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, response.usage.input_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, + response.usage.input_tokens, ) if response.usage.output_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, response.usage.output_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, + response.usage.output_tokens, ) if not client.options["data_collection"]["gen_ai"]["outputs"]: return response span.set_attribute( - SPANDATA.GEN_AI_OUTPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES, json.dumps( [ { @@ -327,14 +331,14 @@ async def wrap_system_one_async( with sentry_sdk.start_span( name=f"decide {model}".strip(), attributes={ - "sentry.op": OP.GEN_AI_DECIDE, - "sentry.origin": TypeSafeIntegration.origin, - SPANDATA.GEN_AI_PROVIDER_NAME: "typesafe", - SPANDATA.GEN_AI_OPERATION_NAME: "decide", + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_DECIDE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: TypeSafeIntegration.origin, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "typesafe", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "decide", }, ) as span: if model is not None: - span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, model) if client.options["data_collection"]["gen_ai"]["inputs"]: input_message: "InputMessageModel" = { @@ -350,7 +354,7 @@ async def wrap_system_one_async( input_message["questions"] = _transform_questions(questions) span.set_attribute( - SPANDATA.GEN_AI_INPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES, json.dumps([input_message], default=_json_default), ) @@ -359,23 +363,25 @@ async def wrap_system_one_async( if not isinstance(response, SystemOneResponse): return response - span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, response.model) + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, response.model) if response.usage.input_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, response.usage.input_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS, + response.usage.input_tokens, ) if response.usage.output_tokens is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, response.usage.output_tokens + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS, + response.usage.output_tokens, ) if not client.options["data_collection"]["gen_ai"]["outputs"]: return response span.set_attribute( - SPANDATA.GEN_AI_OUTPUT_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES, json.dumps( [ { From 7ea0d67c875bfd38723b68d9b3864e09e23c70bb Mon Sep 17 00:00:00 2001 From: srothh Date: Thu, 26 Nov 2026 11:32:39 +0100 Subject: [PATCH 2/3] test(attributes): Enforce sentry-conventions in AI tests Enforce sentry-conventions attribute names in AI integration tests (anthropic, cohere, google_genai, huggingface_hub, litellm, mistral, openai, typesafe). #8323 PY-3090 Co-Authored-By: Claude Opus 5.5 --- .../integrations/anthropic/test_anthropic.py | 1066 +++++++++-------- tests/integrations/cohere/test_cohere.py | 59 +- .../google_genai/test_google_genai.py | 341 ++++-- .../huggingface_hub/test_huggingface_hub.py | 541 +++++---- tests/integrations/litellm/test_litellm.py | 241 ++-- tests/integrations/mistral/test_mistral.py | 97 +- tests/integrations/openai/test_openai.py | 651 +++++----- tests/integrations/typesafe/test_typesafe.py | 44 +- 8 files changed, 1664 insertions(+), 1376 deletions(-) diff --git a/tests/integrations/anthropic/test_anthropic.py b/tests/integrations/anthropic/test_anthropic.py index 40bcfd55de..63a395276b 100644 --- a/tests/integrations/anthropic/test_anthropic.py +++ b/tests/integrations/anthropic/test_anthropic.py @@ -2,6 +2,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk @@ -114,7 +115,7 @@ async def __call__(self, *args, **kwargs): ] DATA_COLLECTION_EXPECTED_INPUT_DATA = { - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS: [ + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS: [ {"type": "text", "content": "You are a helpful assistant."} ], SPANDATA.GEN_AI_REQUEST_MESSAGES: [{"role": "user", "content": "Hello, Claude"}], @@ -344,11 +345,11 @@ def test_nonstreaming_create_message( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ { @@ -371,19 +372,21 @@ def test_nonstreaming_create_message( if ThinkingBlock is not None else [{"type": "text", "content": "Hi, I'm Claude."}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["end_turn"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "end_turn" + ] def test_nonstreaming_create_message_no_sensitive_data( @@ -431,24 +434,26 @@ def test_nonstreaming_create_message_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["end_turn"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "end_turn" + ] @pytest.mark.asyncio @@ -499,11 +504,11 @@ async def test_nonstreaming_create_message_async( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ { @@ -526,16 +531,16 @@ async def test_nonstreaming_create_message_async( if ThinkingBlock is not None else [{"type": "text", "content": "Hi, I'm Claude."}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -588,21 +593,21 @@ async def test_nonstreaming_create_message_async_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -668,14 +673,16 @@ def test_streaming_create_message( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ { @@ -698,19 +705,21 @@ def test_streaming_create_message( if ThinkingBlock is not None else [{"type": "text", "content": "Hi! I'm Claude!"}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["max_tokens"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "max_tokens" + ] def test_streaming_create_message_no_sensitive_data( @@ -774,27 +783,31 @@ def test_streaming_create_message_no_sensitive_data( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["max_tokens"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "max_tokens" + ] def test_streaming_create_message_close( @@ -847,29 +860,31 @@ def test_streaming_create_message_close( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -926,29 +941,31 @@ def test_streaming_create_message_api_error( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -1016,14 +1033,16 @@ def test_stream_messages( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ { @@ -1046,19 +1065,21 @@ def test_stream_messages( if ThinkingBlock is not None else [{"type": "text", "content": "Hi! I'm Claude!"}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["max_tokens"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "max_tokens" + ] def test_stream_messages_no_sensitive_data( @@ -1122,27 +1143,31 @@ def test_stream_messages_no_sensitive_data( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["max_tokens"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "max_tokens" + ] def test_stream_messages_close( @@ -1199,29 +1224,31 @@ def test_stream_messages_close( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -1278,29 +1305,31 @@ def test_stream_messages_api_error( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -1376,11 +1405,11 @@ async def test_streaming_create_message_async( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ { @@ -1403,19 +1432,21 @@ async def test_streaming_create_message_async( if ThinkingBlock is not None else [{"type": "text", "content": "Hi! I'm Claude!"}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["max_tokens"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "max_tokens" + ] @pytest.mark.asyncio @@ -1487,24 +1518,26 @@ async def test_streaming_create_message_async_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["max_tokens"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "max_tokens" + ] @pytest.mark.asyncio @@ -1558,29 +1591,31 @@ async def test_streaming_create_message_async_close( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -1639,29 +1674,31 @@ async def test_streaming_create_message_async_api_error( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -1737,11 +1774,11 @@ async def test_stream_message_async( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ { @@ -1764,16 +1801,16 @@ async def test_stream_message_async( if ThinkingBlock is not None else [{"type": "text", "content": "Hi! I'm Claude!"}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -1847,21 +1884,21 @@ async def test_stream_message_async_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -1921,29 +1958,31 @@ async def test_stream_messages_async_api_error( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -2009,29 +2048,31 @@ async def test_stream_messages_async_close( sentry_sdk.flush() spans = [item.payload for item in items] span = next( - span for span in spans if span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT ) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True assert ( - span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "msg_01XFDUDYJgAACzvnptvVoYEL" ) @@ -2153,27 +2194,27 @@ def test_streaming_create_message_with_input_json_delta( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ { "role": "assistant", "parts": [{"type": "text", "content": '{"location": "San Francisco, CA"}'}], } ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 366 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 407 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 407 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.skipif( @@ -2293,19 +2334,19 @@ def test_streaming_create_message_with_input_json_delta_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 366 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 407 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 407 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.skipif( @@ -2425,27 +2466,27 @@ def test_stream_messages_with_input_json_delta( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ { "role": "assistant", "parts": [{"type": "text", "content": '{"location": "San Francisco, CA"}'}], } ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 366 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 407 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 407 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.skipif( @@ -2565,19 +2606,19 @@ def test_stream_messages_with_input_json_delta_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 366 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 407 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 407 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.asyncio @@ -2704,27 +2745,27 @@ async def test_streaming_create_message_with_input_json_delta_async( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ { "role": "assistant", "parts": [{"type": "text", "content": '{"location": "San Francisco, CA"}'}], } ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 366 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 407 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 407 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.asyncio @@ -2851,19 +2892,19 @@ async def test_streaming_create_message_with_input_json_delta_async_no_sensitive (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 366 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 407 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 407 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.asyncio @@ -2991,27 +3032,27 @@ async def test_stream_message_with_input_json_delta_async( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]' ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ { "role": "assistant", "parts": [{"type": "text", "content": '{"location": "San Francisco, CA"}'}], } ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 366 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 407 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 407 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.asyncio @@ -3139,19 +3180,19 @@ async def test_stream_message_with_input_json_delta_async_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 366 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 407 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 41 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 407 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True def test_exception_message_create( @@ -3209,8 +3250,8 @@ def test_span_status_error( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] assert spans[0]["status"] == "error" - assert spans[0]["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert spans[0]["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" @pytest.mark.asyncio @@ -3242,8 +3283,8 @@ async def test_span_status_error_async( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] assert spans[0]["status"] == "error" - assert spans[0]["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert spans[0]["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" @pytest.mark.asyncio @@ -3299,9 +3340,9 @@ def test_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[0]["attributes"]["sentry.origin"] == "auto.ai.anthropic" - assert spans[0]["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert spans[0]["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.anthropic" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" @pytest.mark.asyncio @@ -3330,9 +3371,9 @@ async def test_span_origin_async( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[0]["attributes"]["sentry.origin"] == "auto.ai.anthropic" - assert spans[0]["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert spans[0]["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.anthropic" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" @pytest.mark.skipif( @@ -3403,15 +3444,15 @@ def test_set_output_data_with_input_json_delta(sentry_init): content_blocks=[{"text": "".join(json_deltas), "type": "text"}], ) - assert json.loads(span._attributes.get(SPANDATA.GEN_AI_OUTPUT_MESSAGES)) == [ + assert json.loads(span._attributes.get(ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES)) == [ { "role": "assistant", "parts": [{"type": "text", "content": "{'test': 'data','more': 'json'}"}], } ] - assert span._attributes.get(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS) == 10 - assert span._attributes.get(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS) == 20 - assert span._attributes.get(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS) == 30 + assert span._attributes.get(ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS) == 10 + assert span._attributes.get(ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS) == 20 + assert span._attributes.get(ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS) == 30 # Test messages with mixed roles including "ai" that should be mapped to "assistant" @@ -3474,9 +3515,9 @@ def mock_messages_create(*args, **kwargs): span = next(item.payload for item in items) # Verify that the span was created correctly - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] # Parse the stored messages @@ -3532,15 +3573,15 @@ def test_nonstreaming_create_message_with_system_prompt( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] system_instructions = json.loads( - span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] ) assert system_instructions == [ {"type": "text", "content": "You are a helpful assistant."} @@ -3562,15 +3603,17 @@ def test_nonstreaming_create_message_with_system_prompt( if ThinkingBlock is not None else [{"type": "text", "content": "Hi, I'm Claude."}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["end_turn"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "end_turn" + ] def test_nonstreaming_create_message_with_system_prompt_no_sensitive_data( @@ -3620,21 +3663,23 @@ def test_nonstreaming_create_message_with_system_prompt_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["end_turn"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "end_turn" + ] @pytest.mark.asyncio @@ -3685,15 +3730,15 @@ async def test_nonstreaming_create_message_with_system_prompt_async( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] system_instructions = json.loads( - span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] ) assert system_instructions == [ {"type": "text", "content": "You are a helpful assistant."} @@ -3715,15 +3760,17 @@ async def test_nonstreaming_create_message_with_system_prompt_async( if ThinkingBlock is not None else [{"type": "text", "content": "Hi, I'm Claude."}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["end_turn"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "end_turn" + ] @pytest.mark.asyncio @@ -3774,21 +3821,23 @@ async def test_nonstreaming_create_message_with_system_prompt_async_no_sensitive (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["end_turn"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == [ + "end_turn" + ] def test_streaming_create_message_with_system_prompt( @@ -3856,15 +3905,15 @@ def test_streaming_create_message_with_system_prompt( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] system_instructions = json.loads( - span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] ) assert system_instructions == [ {"type": "text", "content": "You are a helpful assistant."} @@ -3886,14 +3935,14 @@ def test_streaming_create_message_with_system_prompt( if ThinkingBlock is not None else [{"type": "text", "content": "Hi! I'm Claude!"}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True def test_streaming_create_message_with_system_prompt_no_sensitive_data( @@ -3961,20 +4010,20 @@ def test_streaming_create_message_with_system_prompt_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True def test_stream_messages_with_system_prompt( @@ -4039,15 +4088,15 @@ def test_stream_messages_with_system_prompt( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] system_instructions = json.loads( - span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] ) assert system_instructions == [ {"type": "text", "content": "You are a helpful assistant."} @@ -4068,14 +4117,14 @@ def test_stream_messages_with_system_prompt( if ThinkingBlock is not None else [{"type": "text", "content": "Hi! I'm Claude!"}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True def test_stream_messages_with_system_prompt_no_sensitive_data( @@ -4140,20 +4189,20 @@ def test_stream_messages_with_system_prompt_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.asyncio @@ -4223,15 +4272,15 @@ async def test_stream_message_with_system_prompt_async( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] system_instructions = json.loads( - span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] ) assert system_instructions == [ {"type": "text", "content": "You are a helpful assistant."} @@ -4253,14 +4302,14 @@ async def test_stream_message_with_system_prompt_async( if ThinkingBlock is not None else [{"type": "text", "content": "Hi! I'm Claude!"}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.asyncio @@ -4330,20 +4379,20 @@ async def test_stream_message_with_system_prompt_async_no_sensitive_data( (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.asyncio @@ -4415,15 +4464,15 @@ async def test_streaming_create_message_with_system_prompt_async( assert len(spans) == 1 (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] system_instructions = json.loads( - span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] ) assert system_instructions == [ {"type": "text", "content": "You are a helpful assistant."} @@ -4446,14 +4495,14 @@ async def test_streaming_create_message_with_system_prompt_async( if ThinkingBlock is not None else [{"type": "text", "content": "Hi! I'm Claude!"}] ) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ {"role": "assistant", "parts": expected_parts} ] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.asyncio @@ -4525,20 +4574,20 @@ async def test_streaming_create_message_with_system_prompt_async_no_sensitive_da assert len(spans) == 1 (span,) = spans - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat model" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True def test_system_prompt_with_complex_structure( @@ -4586,12 +4635,12 @@ def test_system_prompt_with_complex_structure( (span,) = spans - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "anthropic" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS in span["attributes"] system_instructions = json.loads( - span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] ) # System content should be a list of text blocks @@ -5075,11 +5124,16 @@ def test_cache_tokens_nonstreaming( sentry_sdk.flush() (span,) = (item.payload for item in items) # input_tokens normalized: 100 + 80 (cache_read) + 20 (cache_write) = 200 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 200 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 250 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 200 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 250 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] + == 20 + ) @pytest.mark.skipif( @@ -5124,8 +5178,8 @@ def test_reasoning_tokens_nonstreaming( sentry_sdk.flush() (span,) = (item.payload for item in items) # Reasoning tokens are part of output_tokens, so totals are unchanged. - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 150 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 150 assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 @@ -5172,8 +5226,8 @@ async def test_reasoning_tokens_nonstreaming_async( sentry_sdk.flush() (span,) = (item.payload for item in items) # Reasoning tokens are part of output_tokens, so totals are unchanged. - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 150 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 150 assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 @@ -5227,10 +5281,15 @@ def test_input_tokens_include_cache_write_nonstreaming( (span,) = (item.payload for item in items) # input_tokens should be total: 19 (non-cached) + 2846 (cache_write) = 2865 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 2865 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 2879 # 2865 + 14 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 0 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] == 2846 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 2865 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 2879 + ) # 2865 + 14 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 0 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] + == 2846 + ) def test_input_tokens_include_cache_read_nonstreaming( @@ -5283,10 +5342,17 @@ def test_input_tokens_include_cache_read_nonstreaming( (span,) = [item.payload for item in items] # input_tokens should be total: 19 (non-cached) + 2846 (cache_read) = 2865 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 2865 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 2879 # 2865 + 14 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 2846 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] == 0 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 2865 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 2879 + ) # 2865 + 14 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 2846 + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] + == 0 + ) def test_input_tokens_include_cache_read_streaming( @@ -5354,10 +5420,17 @@ def test_input_tokens_include_cache_read_streaming( (span,) = (item.payload for item in items) # input_tokens should be total: 19 + 2846 = test_stream_messages_input_tokens_include_cache_read_streaming - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 2865 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 2879 # 2865 + 14 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 2846 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] == 0 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 2865 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 2879 + ) # 2865 + 14 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 2846 + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] + == 0 + ) def test_stream_messages_input_tokens_include_cache_read_streaming( @@ -5423,10 +5496,17 @@ def test_stream_messages_input_tokens_include_cache_read_streaming( (span,) = (item.payload for item in items) # input_tokens should be total: 19 + 2846 = 2865 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 2865 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 2879 # 2865 + 14 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 2846 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] == 0 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 2865 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 2879 + ) # 2865 + 14 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 2846 + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] + == 0 + ) def test_input_tokens_unchanged_without_caching( @@ -5471,8 +5551,10 @@ def test_input_tokens_unchanged_without_caching( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 32 # 20 + 12 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 32 + ) # 20 + 12 def test_cache_tokens_streaming( @@ -5535,11 +5617,16 @@ def test_cache_tokens_streaming( sentry_sdk.flush() (span,) = (item.payload for item in items) # input_tokens normalized: 100 + 80 (cache_read) + 20 (cache_write) = 200 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 200 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 210 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 200 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 210 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] + == 20 + ) def test_stream_messages_cache_tokens( @@ -5601,11 +5688,16 @@ def test_stream_messages_cache_tokens( sentry_sdk.flush() (span,) = (item.payload for item in items) # input_tokens normalized: 100 + 80 (cache_read) + 20 (cache_write) = 200 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 200 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 210 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 200 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 210 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 80 + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS] + == 20 + ) @pytest.mark.skipif( @@ -5670,8 +5762,8 @@ def test_reasoning_tokens_streaming( sentry_sdk.flush() (span,) = (item.payload for item in items) # Reasoning tokens are part of output_tokens, so totals are unchanged. - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 150 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 150 assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 @@ -5736,6 +5828,6 @@ def test_stream_messages_reasoning_tokens( sentry_sdk.flush() (span,) = (item.payload for item in items) # Reasoning tokens are part of output_tokens, so totals are unchanged. - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 150 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 150 assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 diff --git a/tests/integrations/cohere/test_cohere.py b/tests/integrations/cohere/test_cohere.py index f4ff3dbd5a..a564610dfc 100644 --- a/tests/integrations/cohere/test_cohere.py +++ b/tests/integrations/cohere/test_cohere.py @@ -5,6 +5,7 @@ import pytest from cohere import ChatMessage, Client from httpx import Client as HTTPXClient +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import OP, SPANDATA @@ -55,7 +56,7 @@ def test_nonstreaming_chat( assert len(items) == 1 span = items[0].payload - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" assert ( @@ -68,9 +69,9 @@ def test_nonstreaming_chat( ) assert "the model response" in span["attributes"][SPANDATA.AI_RESPONSES] - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 def test_nonstreaming_chat_no_sensitive_data( @@ -117,15 +118,15 @@ def test_nonstreaming_chat_no_sensitive_data( assert len(items) == 1 span = items[0].payload - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" assert SPANDATA.AI_INPUT_MESSAGES not in span["attributes"] assert SPANDATA.AI_RESPONSES not in span["attributes"] - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 def test_streaming_chat( @@ -187,7 +188,7 @@ def test_streaming_chat( assert len(items) == 1 span = items[0].payload - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" assert ( @@ -200,9 +201,9 @@ def test_streaming_chat( ) assert "the model response" in span["attributes"][SPANDATA.AI_RESPONSES] - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 def test_streaming_chat_no_sensitive_data( @@ -264,15 +265,15 @@ def test_streaming_chat_no_sensitive_data( assert len(items) == 1 span = items[0].payload - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" assert SPANDATA.AI_INPUT_MESSAGES not in span["attributes"] assert SPANDATA.AI_RESPONSES not in span["attributes"] - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 def test_embed( @@ -317,11 +318,11 @@ def test_embed( assert len(items) == 1 span = items[0].payload - assert span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS assert "hello" in span["attributes"][SPANDATA.AI_INPUT_MESSAGES] - assert span["attributes"]["gen_ai.usage.input_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 10 def test_embed_no_sensitive_data( @@ -366,11 +367,11 @@ def test_embed_no_sensitive_data( assert len(items) == 1 span = items[0].payload - assert span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS assert SPANDATA.AI_INPUT_MESSAGES not in span["attributes"] - assert span["attributes"]["gen_ai.usage.input_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 10 def test_bad_chat(sentry_init, capture_items): @@ -449,7 +450,7 @@ def test_span_origin_chat(sentry_init, capture_items): sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"]["sentry.origin"] == "auto.ai.cohere" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.cohere" def test_span_origin_embed(sentry_init, capture_items): @@ -482,7 +483,7 @@ def test_span_origin_embed(sentry_init, capture_items): sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"]["sentry.origin"] == "auto.ai.cohere" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.cohere" # data_collection config, expect_inputs, expect_outputs @@ -569,8 +570,8 @@ def test_nonstreaming_chat_data_collection( attributes = items[0].payload["attributes"] assert attributes[SPANDATA.AI_MODEL_ID] == "some-model" - assert attributes["gen_ai.usage.input_tokens"] == 20 - assert attributes["gen_ai.usage.output_tokens"] == 10 + assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 assert attributes["ai.generation_id"] == "gen-1" if expect_inputs: @@ -657,8 +658,8 @@ def test_streaming_chat_data_collection( attributes = items[0].payload["attributes"] assert attributes[SPANDATA.AI_MODEL_ID] == "some-model" - assert attributes["gen_ai.usage.input_tokens"] == 20 - assert attributes["gen_ai.usage.output_tokens"] == 10 + assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 if expect_inputs: assert '{"role": "user", "content": "hello"}' in str( @@ -718,7 +719,7 @@ def test_embed_data_collection( attributes = items[0].payload["attributes"] assert attributes[SPANDATA.AI_MODEL_ID] == "text-embedding-3-large" - assert attributes["gen_ai.usage.input_tokens"] == 10 + assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 if expect_inputs: assert "hello" in str(attributes[SPANDATA.AI_INPUT_MESSAGES]) diff --git a/tests/integrations/google_genai/test_google_genai.py b/tests/integrations/google_genai/test_google_genai.py index e268be4230..9f5ea38059 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -5,6 +5,7 @@ from google import genai from google.genai import types as genai_types from google.genai.types import Content, Part +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk._types import BLOB_DATA_SUBSTITUTE @@ -155,11 +156,14 @@ def test_nonstreaming_generate_content( chat_span = next(item.payload for item in items if item.type == "span") # Check chat span - assert chat_span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert chat_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert chat_span["name"] == "chat gemini-1.5-flash" - assert chat_span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert chat_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "gcp.gemini" - assert chat_span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "gcp.gemini" + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "gemini-1.5-flash" + ) assert json.loads(chat_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ { @@ -185,12 +189,18 @@ def test_nonstreaming_generate_content( assert response_texts == ["Hello! How can I help you today?"] # Check token usage - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 # Output tokens now include reasoning tokens: candidates_token_count (20) + thoughts_token_count (3) = 23 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 23 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 5 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] == 3 + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 23 + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] + == 5 + ) + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] + == 3 + ) def test_nonstreaming_generate_content_no_sensitive_data( @@ -234,21 +244,30 @@ def test_nonstreaming_generate_content_no_sensitive_data( chat_span = next(item.payload for item in items if item.type == "span") # Check chat span - assert chat_span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert chat_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert chat_span["name"] == "chat gemini-1.5-flash" - assert chat_span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert chat_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "gcp.gemini" - assert chat_span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "gcp.gemini" + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "gemini-1.5-flash" + ) assert SPANDATA.GEN_AI_RESPONSE_TEXT not in chat_span["attributes"] # Check token usage - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 # Output tokens now include reasoning tokens: candidates_token_count (20) + thoughts_token_count (3) = 23 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 23 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 5 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] == 3 + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 23 + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] + == 5 + ) + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] + == 3 + ) @pytest.mark.parametrize("generate_content_config", (False, True)) @@ -322,11 +341,13 @@ def test_generate_content_with_system_instruction( invoke_span = next(item.payload for item in items) if expected_texts is None: - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in invoke_span["attributes"] + assert ( + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in invoke_span["attributes"] + ) return system_instructions = json.loads( - invoke_span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS] + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] ) assert system_instructions == [ @@ -461,11 +482,11 @@ def get_weather(location: str) -> str: sentry_sdk.flush() tool_span = next(item.payload for item in items) - assert tool_span["attributes"]["sentry.op"] == OP.GEN_AI_EXECUTE_TOOL + assert tool_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EXECUTE_TOOL assert tool_span["name"] == "execute_tool get_weather" - assert tool_span["attributes"][SPANDATA.GEN_AI_TOOL_NAME] == "get_weather" + assert tool_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "get_weather" assert ( - tool_span["attributes"][SPANDATA.GEN_AI_TOOL_DESCRIPTION] + tool_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_DESCRIPTION] == "Get the weather for a location" ) @@ -624,7 +645,7 @@ def test_streaming_generate_content( ] # Check that streaming flag is set on both spans - assert chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True # Verify accumulated response text (all chunks combined) expected_full_text = "Hello! How can I help you today?" @@ -637,16 +658,28 @@ def test_streaming_generate_content( # Verify finish reasons (only the final chunk has a finish reason) # When there's a single finish reason, it's stored as a plain string (not JSON) - assert SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS in chat_span["attributes"] - assert chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == "STOP" - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 25 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 5 - assert chat_span["attributes"][SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] == 3 + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS in chat_span["attributes"] + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] + == "STOP" + ) + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 25 + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] + == 5 + ) + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] + == 3 + ) # Verify model name - assert chat_span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" + assert ( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "gemini-1.5-flash" + ) def test_span_origin( @@ -673,7 +706,9 @@ def test_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] for span in spans: - assert span["attributes"]["sentry.origin"] == "auto.ai.google_genai" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.google_genai" + ) def test_response_without_usage_metadata( @@ -715,9 +750,9 @@ def test_response_without_usage_metadata( chat_span = next(item.payload for item in items) # Usage data should not be present - assert SPANDATA.GEN_AI_USAGE_INPUT_TOKENS not in chat_span["attributes"] - assert SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS not in chat_span["attributes"] - assert SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS not in chat_span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS not in chat_span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS not in chat_span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS not in chat_span["attributes"] def test_multiple_candidates( @@ -790,7 +825,7 @@ def test_multiple_candidates( # Finish reasons are serialized as JSON finish_reasons = json.loads( - chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] ) assert finish_reasons == ["STOP", "MAX_TOKENS"] @@ -829,13 +864,19 @@ def test_all_configuration_parameters( invoke_span = next(item.payload for item in items) # Check all parameters are recorded - assert invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.8 - assert invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.95 - assert invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_K] == 40 - assert invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 2048 - assert invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_SEED] == 12345 + assert invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.8 + assert invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.95 + assert invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_K] == 40 + assert invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 2048 + assert ( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] + == 0.1 + ) + assert ( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] + == 0.2 + ) + assert invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_SEED] == 12345 def test_empty_response( @@ -910,9 +951,9 @@ def test_response_with_different_id_fields( sentry_sdk.flush() chat_span = next(item.payload for item in items) - assert chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_ID] == "resp-456" + assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_ID] == "resp-456" assert ( - chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "gemini-1.5-flash-001" ) @@ -1131,15 +1172,22 @@ def test_embed_content( (embed_span,) = spans # Check embeddings span - assert embed_span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS + assert embed_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS assert embed_span["name"] == "embeddings text-embedding-004" - assert embed_span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" - assert embed_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "gcp.gemini" assert ( - embed_span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-004" + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" + ) + assert ( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "gcp.gemini" + ) + assert ( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "text-embedding-004" ) - input_texts = json.loads(embed_span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT]) + input_texts = json.loads( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT] + ) assert input_texts == [ "What is your name?", "What is your favorite color?", @@ -1147,8 +1195,8 @@ def test_embed_content( # Check usage data (sum of token counts from statistics: 10 + 15 = 25) # Note: Only available in newer versions with ContentEmbeddingStatistics - if SPANDATA.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: - assert embed_span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 25 + if ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: + assert embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 25 def test_embed_content_no_sensitive_data( @@ -1191,20 +1239,25 @@ def test_embed_content_no_sensitive_data( (embed_span,) = spans # Check embeddings span - assert embed_span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS + assert embed_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS assert embed_span["name"] == "embeddings text-embedding-004" - assert embed_span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" - assert embed_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "gcp.gemini" assert ( - embed_span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-004" + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" + ) + assert ( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "gcp.gemini" + ) + assert ( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "text-embedding-004" ) - assert SPANDATA.GEN_AI_EMBEDDINGS_INPUT not in embed_span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT not in embed_span["attributes"] # Check usage data (sum of token counts from statistics: 10 + 15 = 25) # Note: Only available in newer versions with ContentEmbeddingStatistics - if SPANDATA.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: - assert embed_span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 25 + if ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: + assert embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 25 def test_embed_content_string_input( @@ -1255,13 +1308,15 @@ def test_embed_content_string_input( (embed_span,) = spans # Check that single string is handled correctly - input_texts = json.loads(embed_span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT]) + input_texts = json.loads( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT] + ) assert input_texts == ["Single text input"] # Should use token_count from statistics (5), not billable_character_count (10) # Note: Only available in newer versions with ContentEmbeddingStatistics - if SPANDATA.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: - assert embed_span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 5 + if ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: + assert embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 5 def test_embed_content_error_handling( @@ -1335,7 +1390,7 @@ def test_embed_content_without_statistics( # No usage tokens since there are no statistics in older versions # This is expected and the integration should handle it gracefully - assert SPANDATA.GEN_AI_USAGE_INPUT_TOKENS not in embed_span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS not in embed_span["attributes"] def test_embed_content_span_origin( @@ -1362,7 +1417,9 @@ def test_embed_content_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] for span in spans: - assert span["attributes"]["sentry.origin"] == "auto.ai.google_genai" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.google_genai" + ) @pytest.mark.asyncio @@ -1407,15 +1464,22 @@ async def test_async_embed_content( (embed_span,) = spans # Check embeddings span - assert embed_span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS + assert embed_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS assert embed_span["name"] == "embeddings text-embedding-004" - assert embed_span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" - assert embed_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "gcp.gemini" assert ( - embed_span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-004" + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" + ) + assert ( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "gcp.gemini" + ) + assert ( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "text-embedding-004" ) - input_texts = json.loads(embed_span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT]) + input_texts = json.loads( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT] + ) assert input_texts == [ "What is your name?", "What is your favorite color?", @@ -1423,8 +1487,8 @@ async def test_async_embed_content( # Check usage data (sum of token counts from statistics: 10 + 15 = 25) # Note: Only available in newer versions with ContentEmbeddingStatistics - if SPANDATA.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: - assert embed_span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 25 + if ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: + assert embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 25 @pytest.mark.asyncio @@ -1469,20 +1533,25 @@ async def test_async_embed_content_no_sensitive_data( (embed_span,) = spans # Check embeddings span - assert embed_span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS + assert embed_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS assert embed_span["name"] == "embeddings text-embedding-004" - assert embed_span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" - assert embed_span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "gcp.gemini" assert ( - embed_span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-004" + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" + ) + assert ( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "gcp.gemini" + ) + assert ( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "text-embedding-004" ) - assert SPANDATA.GEN_AI_EMBEDDINGS_INPUT not in embed_span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT not in embed_span["attributes"] # Check usage data (sum of token counts from statistics: 10 + 15 = 25) # Note: Only available in newer versions with ContentEmbeddingStatistics - if SPANDATA.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: - assert embed_span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 25 + if ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: + assert embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 25 @pytest.mark.asyncio @@ -1536,12 +1605,14 @@ async def test_async_embed_content_string_input( (embed_span,) = spans # Check that single string is handled correctly - input_texts = json.loads(embed_span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT]) + input_texts = json.loads( + embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT] + ) assert input_texts == ["Single text input"] # Should use token_count from statistics (5), not billable_character_count (10) # Note: Only available in newer versions with ContentEmbeddingStatistics - if SPANDATA.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: - assert embed_span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 5 + if ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS in embed_span["attributes"]: + assert embed_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 5 @pytest.mark.asyncio @@ -1619,7 +1690,7 @@ async def test_async_embed_content_without_statistics( # No usage tokens since there are no statistics in older versions # This is expected and the integration should handle it gracefully - assert SPANDATA.GEN_AI_USAGE_INPUT_TOKENS not in embed_span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS not in embed_span["attributes"] @pytest.mark.asyncio @@ -1650,7 +1721,9 @@ async def test_async_embed_content_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] for span in spans: - assert span["attributes"]["sentry.origin"] == "auto.ai.google_genai" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.google_genai" + ) # Integration tests for generate_content with different input message formats @@ -2533,14 +2606,14 @@ def __init__(self): DATA_COLLECTION_CHAT_EXPECTED_VALUES = { SPANDATA.GEN_AI_REQUEST_MESSAGES: [{"role": "user", "content": "Tell me a joke"}], - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS: [ + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS: [ {"type": "text", "content": "You are a helpful assistant."} ], SPANDATA.GEN_AI_RESPONSE_TEXT: ["Hello! How can I help you today?"], } DATA_COLLECTION_EMBED_EXPECTED_VALUES = { - SPANDATA.GEN_AI_EMBEDDINGS_INPUT: [ + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT: [ "What is your name?", "What is your favorite color?", ], @@ -2554,7 +2627,7 @@ def __init__(self): {"gen_ai": {"inputs": True, "outputs": False}}, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], [ SPANDATA.GEN_AI_RESPONSE_TEXT, @@ -2568,7 +2641,7 @@ def __init__(self): ], [ SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -2609,7 +2682,11 @@ def test_generate_content_data_collection( sentry_sdk.flush() spans = [item.payload for item in captured if item.type == "span"] - (span,) = [s for s in spans if s["attributes"].get("sentry.op") == OP.GEN_AI_CHAT] + (span,) = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + ] span_data = span["attributes"] for key in expected_present: @@ -2619,14 +2696,14 @@ def test_generate_content_data_collection( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - assert span_data[SPANDATA.GEN_AI_PROVIDER_NAME] == "gcp.gemini" - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" - assert span_data[SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span_data[SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span_data[SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span_data[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 23 - assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 - assert span_data[SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == "STOP" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "gcp.gemini" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 23 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == "STOP" @pytest.mark.parametrize( @@ -2714,7 +2791,11 @@ def test_generate_content_data_collection_tools( sentry_sdk.flush() spans = [item.payload for item in captured if item.type == "span"] - (span,) = [s for s in spans if s["attributes"].get("sentry.op") == OP.GEN_AI_CHAT] + (span,) = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + ] span_data = span["attributes"] for key in expected_present: @@ -2740,7 +2821,7 @@ def test_generate_content_data_collection_tools( {"gen_ai": {"inputs": True, "outputs": False}}, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], [ SPANDATA.GEN_AI_RESPONSE_TEXT, @@ -2754,7 +2835,7 @@ def test_generate_content_data_collection_tools( ], [ SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -2830,7 +2911,11 @@ def test_streaming_generate_content_data_collection( sentry_sdk.flush() spans = [item.payload for item in captured if item.type == "span"] - (span,) = [s for s in spans if s["attributes"].get("sentry.op") == OP.GEN_AI_CHAT] + (span,) = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + ] span_data = span["attributes"] for key in expected_present: @@ -2840,11 +2925,11 @@ def test_streaming_generate_content_data_collection( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - assert span_data[SPANDATA.GEN_AI_RESPONSE_STREAMING] is True - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" - assert span_data[SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == "STOP" - assert span_data[SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 25 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == "STOP" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 25 @pytest.mark.parametrize( @@ -2938,7 +3023,11 @@ def test_streaming_generate_content_data_collection_tools( sentry_sdk.flush() spans = [item.payload for item in captured if item.type == "span"] - (span,) = [s for s in spans if s["attributes"].get("sentry.op") == OP.GEN_AI_CHAT] + (span,) = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + ] span_data = span["attributes"] for key in expected_present: @@ -2962,7 +3051,7 @@ def test_streaming_generate_content_data_collection_tools( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_EMBEDDINGS_INPUT, + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT, ], [], id="gen-ai-inputs-enabled-outputs-disabled", @@ -2971,7 +3060,7 @@ def test_streaming_generate_content_data_collection_tools( {"gen_ai": {"inputs": False, "outputs": True}}, [], [ - SPANDATA.GEN_AI_EMBEDDINGS_INPUT, + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -3008,7 +3097,9 @@ def test_embed_content_data_collection( sentry_sdk.flush() spans = [item.payload for item in captured if item.type == "span"] (span,) = [ - s for s in spans if s["attributes"].get("sentry.op") == OP.GEN_AI_EMBEDDINGS + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_EMBEDDINGS ] span_data = span["attributes"] @@ -3019,8 +3110,8 @@ def test_embed_content_data_collection( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-004" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "text-embedding-004" @pytest.mark.asyncio @@ -3031,7 +3122,7 @@ def test_embed_content_data_collection( {"gen_ai": {"inputs": True, "outputs": False}}, [ SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], [ SPANDATA.GEN_AI_RESPONSE_TEXT, @@ -3045,7 +3136,7 @@ def test_embed_content_data_collection( ], [ SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -3084,7 +3175,11 @@ async def test_async_generate_content_data_collection( sentry_sdk.flush() spans = [item.payload for item in captured if item.type == "span"] - (span,) = [s for s in spans if s["attributes"].get("sentry.op") == OP.GEN_AI_CHAT] + (span,) = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + ] span_data = span["attributes"] for key in expected_present: @@ -3094,8 +3189,8 @@ async def test_async_generate_content_data_collection( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" - assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "gemini-1.5-flash" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio @@ -3105,7 +3200,7 @@ async def test_async_generate_content_data_collection( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_EMBEDDINGS_INPUT, + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT, ], [], id="gen-ai-inputs-enabled-outputs-disabled", @@ -3114,7 +3209,7 @@ async def test_async_generate_content_data_collection( {"gen_ai": {"inputs": False, "outputs": True}}, [], [ - SPANDATA.GEN_AI_EMBEDDINGS_INPUT, + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -3151,7 +3246,9 @@ async def test_async_embed_content_data_collection( sentry_sdk.flush() spans = [item.payload for item in captured if item.type == "span"] (span,) = [ - s for s in spans if s["attributes"].get("sentry.op") == OP.GEN_AI_EMBEDDINGS + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_EMBEDDINGS ] span_data = span["attributes"] @@ -3162,5 +3259,5 @@ async def test_async_embed_content_data_collection( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-004" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "text-embedding-004" diff --git a/tests/integrations/huggingface_hub/test_huggingface_hub.py b/tests/integrations/huggingface_hub/test_huggingface_hub.py index eea83f3474..1fa2a3dd15 100644 --- a/tests/integrations/huggingface_hub/test_huggingface_hub.py +++ b/tests/integrations/huggingface_hub/test_huggingface_hub.py @@ -3,6 +3,7 @@ import pytest from huggingface_hub import InferenceClient +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import SPANDATA @@ -438,30 +439,32 @@ def test_text_generation( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.text_completion" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.text_completion" assert span["name"] == "text_completion test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "text_completion", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "length", - "gen_ai.response.streaming": False, - "gen_ai.usage.total_tokens": 10, - "sentry.op": "gen_ai.text_completion", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "text_completion", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "length", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: False, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 10, + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.text_completion", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } expected_data["gen_ai.request.messages"] = "Hello" @@ -470,7 +473,7 @@ def test_text_generation( assert span["attributes"] == ApproxDict(expected_data) # text generation does not set the response model - assert "gen_ai.response.model" not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL not in span["attributes"] @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @@ -503,30 +506,32 @@ def test_text_generation_no_sensitive_data( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.text_completion" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.text_completion" assert span["name"] == "text_completion test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "text_completion", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "length", - "gen_ai.response.streaming": False, - "gen_ai.usage.total_tokens": 10, - "sentry.op": "gen_ai.text_completion", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "text_completion", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "length", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: False, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 10, + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.text_completion", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } assert "gen_ai.request.messages" not in expected_data @@ -535,7 +540,7 @@ def test_text_generation_no_sensitive_data( assert span["attributes"] == ApproxDict(expected_data) # text generation does not set the response model - assert "gen_ai.response.model" not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL not in span["attributes"] @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @@ -570,31 +575,33 @@ def test_text_generation_streaming( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.text_completion" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.text_completion" assert span["name"] == "text_completion test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "text_completion", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "length", - "gen_ai.response.streaming": True, - "gen_ai.usage.total_tokens": 10, - "sentry.environment": "production", - "sentry.op": "gen_ai.text_completion", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "text_completion", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "length", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 10, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.text_completion", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } expected_data["gen_ai.request.messages"] = "Hello" @@ -603,7 +610,7 @@ def test_text_generation_streaming( assert span["attributes"] == ApproxDict(expected_data) # text generation does not set the response model - assert "gen_ai.response.model" not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL not in span["attributes"] @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @@ -637,31 +644,33 @@ def test_text_generation_streaming_no_sensitive_data( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.text_completion" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.text_completion" assert span["name"] == "text_completion test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "text_completion", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "length", - "gen_ai.response.streaming": True, - "gen_ai.usage.total_tokens": 10, - "sentry.environment": "production", - "sentry.op": "gen_ai.text_completion", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "text_completion", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "length", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 10, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.text_completion", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } assert "gen_ai.request.messages" not in expected_data @@ -670,7 +679,7 @@ def test_text_generation_streaming_no_sensitive_data( assert span["attributes"] == ApproxDict(expected_data) # text generation does not set the response model - assert "gen_ai.response.model" not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL not in span["attributes"] @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @@ -709,34 +718,36 @@ def test_chat_completion( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["name"] == "chat test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "stop", - "gen_ai.response.model": "test-model-123", - "gen_ai.response.streaming": False, - "gen_ai.usage.input_tokens": 10, - "gen_ai.usage.output_tokens": 8, - "gen_ai.usage.total_tokens": 18, - "sentry.environment": "production", - "sentry.op": "gen_ai.chat", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "stop", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: False, + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS: 10, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 8, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 18, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.chat", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } expected_data["gen_ai.request.messages"] = safe_serialize( @@ -788,34 +799,36 @@ def test_chat_completion_no_sensitive_data( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["name"] == "chat test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "stop", - "gen_ai.response.model": "test-model-123", - "gen_ai.response.streaming": False, - "gen_ai.usage.input_tokens": 10, - "gen_ai.usage.output_tokens": 8, - "gen_ai.usage.total_tokens": 18, - "sentry.environment": "production", - "sentry.op": "gen_ai.chat", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "stop", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: False, + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS: 10, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 8, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 18, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.chat", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } assert "gen_ai.request.messages" not in expected_data @@ -862,37 +875,39 @@ def test_chat_completion_streaming( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["name"] == "chat test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "stop", - "gen_ai.response.model": "test-model-123", - "gen_ai.response.streaming": True, - "sentry.environment": "production", - "sentry.op": "gen_ai.chat", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "stop", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.chat", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } # usage is not available in older versions of the library if HF_VERSION and HF_VERSION >= (0, 26, 0): - expected_data["gen_ai.usage.input_tokens"] = 183 - expected_data["gen_ai.usage.output_tokens"] = 14 - expected_data["gen_ai.usage.total_tokens"] = 197 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] = 183 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] = 14 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] = 197 expected_data["gen_ai.request.messages"] = safe_serialize( [ @@ -945,37 +960,39 @@ def test_chat_completion_streaming_no_sensitive_data( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["name"] == "chat test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "stop", - "gen_ai.response.model": "test-model-123", - "gen_ai.response.streaming": True, - "sentry.environment": "production", - "sentry.op": "gen_ai.chat", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "stop", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.chat", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } # usage is not available in older versions of the library if HF_VERSION and HF_VERSION >= (0, 26, 0): - expected_data["gen_ai.usage.input_tokens"] = 183 - expected_data["gen_ai.usage.output_tokens"] = 14 - expected_data["gen_ai.usage.total_tokens"] = 197 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] = 183 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] = 14 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] = 197 assert "gen_ai.request.messages" not in expected_data assert "gen_ai.response.text" not in expected_data @@ -1013,30 +1030,32 @@ def test_chat_completion_api_error( spans = [item.payload for item in items if item.type == "span"] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["name"] == "chat test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) assert span["status"] == "error" assert error["contexts"]["trace"]["trace_id"] == span["trace_id"] expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.model": "test-model", - "sentry.environment": "production", - "sentry.op": "gen_ai.chat", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.chat", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } assert span["attributes"] == ApproxDict(expected_data) @@ -1067,15 +1086,15 @@ def test_span_status_error( spans = [item.payload for item in items if item.type == "span"] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None assert span["status"] == "error" @@ -1127,34 +1146,36 @@ def test_chat_completion_with_tools( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["name"] == "chat test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "chat", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", "gen_ai.request.available_tools": '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "tool_calls", - "gen_ai.response.model": "test-model-123", - "gen_ai.usage.input_tokens": 10, - "gen_ai.usage.output_tokens": 8, - "gen_ai.usage.total_tokens": 18, - "sentry.environment": "production", - "sentry.op": "gen_ai.chat", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "tool_calls", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS: 10, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 8, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 18, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.chat", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } expected_data["gen_ai.request.messages"] = ( @@ -1212,33 +1233,35 @@ def test_chat_completion_with_tools_no_sensitive_data( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["name"] == "chat test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "tool_calls", - "gen_ai.response.model": "test-model-123", - "gen_ai.usage.input_tokens": 10, - "gen_ai.usage.output_tokens": 8, - "gen_ai.usage.total_tokens": 18, - "sentry.environment": "production", - "sentry.op": "gen_ai.chat", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "tool_calls", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS: 10, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 8, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 18, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.chat", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } assert "gen_ai.request.messages" not in expected_data @@ -1298,38 +1321,40 @@ def test_chat_completion_streaming_with_tools( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["name"] == "chat test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "chat", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", "gen_ai.request.available_tools": '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "tool_calls", - "gen_ai.response.model": "test-model-123", - "gen_ai.response.streaming": True, - "sentry.environment": "production", - "sentry.op": "gen_ai.chat", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "tool_calls", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.chat", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } if HF_VERSION and HF_VERSION >= (0, 26, 0): - expected_data["gen_ai.usage.input_tokens"] = 183 - expected_data["gen_ai.usage.output_tokens"] = 14 - expected_data["gen_ai.usage.total_tokens"] = 197 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] = 183 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] = 14 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] = 197 expected_data["gen_ai.request.messages"] = ( '[{"role": "user", "content": "What is the weather in Paris?"}]' @@ -1390,37 +1415,39 @@ def test_chat_completion_streaming_with_tools_no_sensitive_data( spans = [item.payload for item in items] span = None for sp in spans: - if "sentry.op" in sp["attributes"] and sp["attributes"]["sentry.op"].startswith( - "gen_ai" - ): + if ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"] and sp["attributes"][ + ATTRIBUTE_NAMES.SENTRY_OP + ].startswith("gen_ai"): assert span is None, "there is exactly one gen_ai span" span = sp - elif "sentry.op" in sp["attributes"]: + elif ATTRIBUTE_NAMES.SENTRY_OP in sp["attributes"]: # there should be no other spans, just the gen_ai span # and optionally some http.client spans from talking to the hf api - assert sp["attributes"]["sentry.op"] == "http.client" + assert sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span is not None - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["name"] == "chat test-model" - assert span["attributes"]["sentry.origin"] == "auto.ai.huggingface_hub" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.huggingface_hub" + ) expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.model": "test-model", - "gen_ai.response.finish_reasons": "tool_calls", - "gen_ai.response.model": "test-model-123", - "gen_ai.response.streaming": True, - "sentry.environment": "production", - "sentry.op": "gen_ai.chat", - "sentry.origin": "auto.ai.huggingface_hub", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "tool_calls", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.chat", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } if HF_VERSION and HF_VERSION >= (0, 26, 0): - expected_data["gen_ai.usage.input_tokens"] = 183 - expected_data["gen_ai.usage.output_tokens"] = 14 - expected_data["gen_ai.usage.total_tokens"] = 197 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] = 183 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] = 14 + expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] = 197 assert "gen_ai.request.messages" not in expected_data assert "gen_ai.response.text" not in expected_data @@ -1497,7 +1524,11 @@ def test_text_generation_data_collection( client.text_generation("Hello", stream=False, details=True) sentry_sdk.flush() spans = [item.payload for item in captured] - (span,) = [sp for sp in spans if sp["attributes"]["sentry.op"].startswith("gen_ai")] + (span,) = [ + sp + for sp in spans + if sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP].startswith("gen_ai") + ] span_data = span["attributes"] expected_values = { @@ -1512,10 +1543,10 @@ def test_text_generation_data_collection( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "text_completion" - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "test-model" - assert span_data[SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == "length" - assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 10 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "text_completion" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "test-model" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == "length" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 10 @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @@ -1570,7 +1601,11 @@ def test_text_generation_streaming_data_collection( pass sentry_sdk.flush() spans = [item.payload for item in captured] - (span,) = [sp for sp in spans if sp["attributes"]["sentry.op"].startswith("gen_ai")] + (span,) = [ + sp + for sp in spans + if sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP].startswith("gen_ai") + ] span_data = span["attributes"] expected_values = { @@ -1585,11 +1620,11 @@ def test_text_generation_streaming_data_collection( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "text_completion" - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "test-model" - assert span_data[SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == "length" - assert span_data[SPANDATA.GEN_AI_RESPONSE_STREAMING] is True - assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 10 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "text_completion" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "test-model" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == "length" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 10 @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @@ -1649,7 +1684,11 @@ def test_chat_completion_data_collection_tools( ) sentry_sdk.flush() spans = [item.payload for item in captured] - (span,) = [sp for sp in spans if sp["attributes"]["sentry.op"].startswith("gen_ai")] + (span,) = [ + sp + for sp in spans + if sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP].startswith("gen_ai") + ] span_data = span["attributes"] expected_values = { @@ -1668,13 +1707,13 @@ def test_chat_completion_data_collection_tools( # This response carries only tool calls, so there is never any response text assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span_data - assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "test-model" - assert span_data[SPANDATA.GEN_AI_RESPONSE_MODEL] == "test-model-123" - assert span_data[SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == "tool_calls" - assert span_data[SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span_data[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 8 - assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 18 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "test-model" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "test-model-123" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == "tool_calls" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 8 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 18 @pytest.mark.httpx_mock(assert_all_requests_were_expected=False) @@ -1738,7 +1777,11 @@ def test_chat_completion_streaming_data_collection_tools( pass sentry_sdk.flush() spans = [item.payload for item in captured] - (span,) = [sp for sp in spans if sp["attributes"]["sentry.op"].startswith("gen_ai")] + (span,) = [ + sp + for sp in spans + if sp["attributes"][ATTRIBUTE_NAMES.SENTRY_OP].startswith("gen_ai") + ] span_data = span["attributes"] expected_values = { @@ -1755,13 +1798,13 @@ def test_chat_completion_streaming_data_collection_tools( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "test-model" - assert span_data[SPANDATA.GEN_AI_RESPONSE_MODEL] == "test-model-123" - assert span_data[SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == "tool_calls" - assert span_data[SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span_data[ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "test-model" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "test-model-123" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS] == "tool_calls" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True if HF_VERSION and HF_VERSION >= (0, 26, 0): - assert span_data[SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 183 - assert span_data[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 14 - assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 197 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 183 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 14 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 197 diff --git a/tests/integrations/litellm/test_litellm.py b/tests/integrations/litellm/test_litellm.py index 6fcc79a04c..a9b1329ed1 100644 --- a/tests/integrations/litellm/test_litellm.py +++ b/tests/integrations/litellm/test_litellm.py @@ -6,6 +6,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk @@ -195,18 +196,18 @@ def test_nonstreaming_chat_completion( chat_spans = list( x for x in spans - if x["attributes"].get("sentry.op") == OP.GEN_AI_CHAT - and x["attributes"].get("sentry.origin") == "auto.ai.litellm" + if x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + and x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ { @@ -217,9 +218,9 @@ def test_nonstreaming_chat_completion( ] assert SPANDATA.GEN_AI_RESPONSE_TEXT in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 def test_nonstreaming_chat_completion_no_sensitive_data( @@ -283,25 +284,25 @@ def test_nonstreaming_chat_completion_no_sensitive_data( chat_spans = list( x for x in spans - if x["attributes"].get("sentry.op") == OP.GEN_AI_CHAT - and x["attributes"].get("sentry.origin") == "auto.ai.litellm" + if x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + and x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio(loop_scope="session") @@ -366,18 +367,18 @@ async def test_async_nonstreaming_chat_completion( chat_spans = list( x for x in spans - if x["attributes"].get("sentry.op") == OP.GEN_AI_CHAT - and x["attributes"].get("sentry.origin") == "auto.ai.litellm" + if x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + and x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ { @@ -388,9 +389,9 @@ async def test_async_nonstreaming_chat_completion( ] assert SPANDATA.GEN_AI_RESPONSE_TEXT in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio(loop_scope="session") @@ -455,25 +456,25 @@ async def test_async_nonstreaming_chat_completion_no_sensitive_data( chat_spans = list( x for x in spans - if x["attributes"].get("sentry.op") == OP.GEN_AI_CHAT - and x["attributes"].get("sentry.origin") == "auto.ai.litellm" + if x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + and x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 def test_streaming_chat_completion( @@ -534,14 +535,14 @@ def test_streaming_chat_completion( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True @pytest.mark.asyncio(loop_scope="session") @@ -606,14 +607,14 @@ async def test_async_streaming_chat_completion( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True def test_embeddings_create( @@ -670,23 +671,23 @@ def test_embeddings_create( spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(spans) == 1 span = spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS assert span["name"] == "embeddings text-embedding-ada-002" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 5 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 5 assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "text-embedding-ada-002" ) # Check that embeddings input is captured (it's JSON serialized) - embeddings_input = span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT] + embeddings_input = span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT] assert json.loads(embeddings_input) == ["Hello, world!"] @@ -746,22 +747,22 @@ async def test_async_embeddings_create( spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(spans) == 1 span = spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS assert span["name"] == "embeddings text-embedding-ada-002" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 5 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 5 assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "text-embedding-ada-002" ) # Check that embeddings input is captured (it's JSON serialized) - embeddings_input = span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT] + embeddings_input = span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT] assert json.loads(embeddings_input) == ["Hello, world!"] @@ -816,16 +817,16 @@ def test_embeddings_create_with_list_input( spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(spans) == 1 span = spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" # Check that list of embeddings input is captured (it's JSON serialized) - embeddings_input = span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT] + embeddings_input = span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT] assert json.loads(embeddings_input) == [ "First text", @@ -885,16 +886,16 @@ async def test_async_embeddings_create_with_list_input( spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(spans) == 1 span = spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "embeddings" # Check that list of embeddings input is captured (it's JSON serialized) - embeddings_input = span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT] + embeddings_input = span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT] assert json.loads(embeddings_input) == [ "First text", @@ -951,14 +952,14 @@ def test_embeddings_no_sensitive_data( spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(spans) == 1 span = spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - assert SPANDATA.GEN_AI_EMBEDDINGS_INPUT not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + assert ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT not in span["attributes"] @pytest.mark.asyncio(loop_scope="session") @@ -1011,14 +1012,14 @@ async def test_async_embeddings_no_sensitive_data( spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(spans) == 1 span = spans[0] - assert span["attributes"]["sentry.op"] == OP.GEN_AI_EMBEDDINGS - assert SPANDATA.GEN_AI_EMBEDDINGS_INPUT not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + assert ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT not in span["attributes"] def test_exception_handling( @@ -1140,7 +1141,7 @@ def test_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[1]["attributes"]["sentry.origin"] == "auto.ai.litellm" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" def test_multiple_providers( @@ -1241,7 +1242,7 @@ def test_multiple_providers( spans = [item.payload for item in items] for span in spans: # The provider should be detected by litellm.get_llm_provider - assert SPANDATA.GEN_AI_PROVIDER_NAME in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME in span["attributes"] @pytest.mark.asyncio(loop_scope="session") @@ -1345,7 +1346,7 @@ async def test_async_multiple_providers( spans = [item.payload for item in items] for span in spans: # The provider should be detected by litellm.get_llm_provider - assert SPANDATA.GEN_AI_PROVIDER_NAME in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME in span["attributes"] def test_additional_parameters( @@ -1405,17 +1406,17 @@ def test_additional_parameters( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.5 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.5 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.5 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.5 @pytest.mark.asyncio(loop_scope="session") @@ -1476,17 +1477,17 @@ async def test_async_additional_parameters( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.5 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.5 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.5 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.5 def test_no_integration( @@ -1540,8 +1541,8 @@ def test_no_integration( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 0 @@ -1598,8 +1599,8 @@ async def test_async_no_integration( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 0 @@ -1645,7 +1646,7 @@ def test_response_without_usage( (span,) = (item.payload for item in items) # Span should still be created even without usage info - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT assert span["name"] == "chat gpt-3.5-turbo" @@ -1737,8 +1738,8 @@ def test_binary_content_encoding_image_url( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] @@ -1833,8 +1834,8 @@ async def test_async_binary_content_encoding_image_url( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] @@ -1929,8 +1930,8 @@ def test_binary_content_encoding_mixed_content( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] @@ -2014,8 +2015,8 @@ async def test_async_binary_content_encoding_mixed_content( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] @@ -2095,8 +2096,8 @@ def test_binary_content_encoding_uri_type( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] @@ -2185,8 +2186,8 @@ async def test_async_binary_content_encoding_uri_type( chat_spans = list( x for x in spans - if x["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - and x["attributes"]["sentry.origin"] == "auto.ai.litellm" + if x["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + and x["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.litellm" ) assert len(chat_spans) == 1 span = chat_spans[0] @@ -2383,8 +2384,8 @@ def test_chat_completion_data_collection( (span,) = [ x for x in spans - if x["attributes"].get("sentry.op") == OP.GEN_AI_CHAT - and x["attributes"].get("sentry.origin") == "auto.ai.litellm" + if x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT + and x["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) == "auto.ai.litellm" ] span_data = span["attributes"] @@ -2402,7 +2403,7 @@ def test_chat_completion_data_collection( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "chat" - assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" - assert span_data[SPANDATA.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" - assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span_data[ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "gpt-3.5-turbo" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "gpt-3.5-turbo" + assert span_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 diff --git a/tests/integrations/mistral/test_mistral.py b/tests/integrations/mistral/test_mistral.py index 8a35a57979..293feabee6 100644 --- a/tests/integrations/mistral/test_mistral.py +++ b/tests/integrations/mistral/test_mistral.py @@ -13,9 +13,10 @@ UsageInfo, UserMessage, ) +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.mistral import MistralIntegration @@ -95,26 +96,31 @@ def test_nonstreaming_chat( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert span["name"] == "chat mistral-medium-latest" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "mistral" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "mistral" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "mistral-medium-latest" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "mistral-medium-latest" + ) + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL] == "high" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 1024 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL] == "high" - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio @@ -163,26 +169,31 @@ async def test_nonstreaming_chat_async( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert span["name"] == "chat mistral-medium-latest" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "mistral" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "mistral" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "mistral-medium-latest" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "mistral-medium-latest" + ) + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL] == "high" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 1024 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL] == "high" - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.parametrize( @@ -419,16 +430,18 @@ def test_input_attributes_nonstreaming_chat( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert span["name"] == "chat mistral-medium-latest" assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS]) == expected_system_instructions ) assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES]) == expected_input_messages ) @@ -668,16 +681,18 @@ async def test_input_attributes_nonstreaming_chat_async( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert span["name"] == "chat mistral-medium-latest" assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS]) == expected_system_instructions ) assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES]) == expected_input_messages ) @@ -721,11 +736,13 @@ def test_output_attributes_nonstreaming_chat( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert span["name"] == "chat mistral-medium-latest" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ { "role": "assistant", "parts": [ @@ -782,11 +799,13 @@ async def test_output_attributes_nonstreaming_chat_async( sentry_sdk.flush() spans = [item.payload for item in items] (span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ) assert span["name"] == "chat mistral-medium-latest" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ { "role": "assistant", "parts": [ diff --git a/tests/integrations/openai/test_openai.py b/tests/integrations/openai/test_openai.py index 763b927b98..395570cfa7 100644 --- a/tests/integrations/openai/test_openai.py +++ b/tests/integrations/openai/test_openai.py @@ -1,6 +1,7 @@ import json import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.utils import package_version @@ -252,7 +253,7 @@ def test_chat_completion_tool_definitions( sentry_sdk.flush() span = next(item.payload for item in items) - assert json.loads(span["attributes"][SPANDATA.GEN_AI_TOOL_DEFINITIONS]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_DEFINITIONS]) == [ { "type": "function", "name": "name", @@ -328,24 +329,24 @@ def test_nonstreaming_chat_completion_no_sensitive_data( assert response == "the model response" sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False - - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "some-model" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False + + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.parametrize( @@ -478,19 +479,19 @@ def test_nonstreaming_chat_completion( assert response == "the model response" sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "some-model" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS]) == expected_system_instructions ) @@ -501,9 +502,9 @@ def test_nonstreaming_chat_completion( ) assert "the model response" in span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio @@ -557,24 +558,24 @@ async def test_nonstreaming_chat_completion_async_no_sensitive_data( assert response == "the model response" sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False - - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "some-model" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False + + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio @@ -705,19 +706,19 @@ async def test_nonstreaming_chat_completion_async( assert response == "the model response" sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is False - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "some-model" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS]) == expected_system_instructions ) @@ -728,9 +729,9 @@ async def test_nonstreaming_chat_completion_async( ) assert "the model response" in span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 def tiktoken_encoding_if_installed(): @@ -839,29 +840,29 @@ def test_streaming_chat_completion_no_sensitive_data( assert response_string == "hello world" sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "some-model" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "model-id" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "model-id" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] try: import tiktoken # type: ignore # noqa: F401 - assert span["attributes"]["gen_ai.usage.output_tokens"] == 2 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 7 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 9 except ImportError: pass # if tiktoken is not installed, we can't guarantee token usage will be calculated properly @@ -946,10 +947,10 @@ def test_streaming_chat_completion_with_usage_in_stream( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.skipif( @@ -1013,10 +1014,10 @@ def test_streaming_chat_completion_empty_content_preserves_token_usage( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert "gen_ai.usage.output_tokens" not in span["attributes"] - assert span["attributes"]["gen_ai.usage.total_tokens"] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 @pytest.mark.skipif( @@ -1084,10 +1085,10 @@ async def test_streaming_chat_completion_empty_content_preserves_token_usage_asy sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert "gen_ai.usage.output_tokens" not in span["attributes"] - assert span["attributes"]["gen_ai.usage.total_tokens"] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 @pytest.mark.skipif( @@ -1174,10 +1175,10 @@ async def test_streaming_chat_completion_async_with_usage_in_stream( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 # noinspection PyTypeChecker @@ -1361,23 +1362,23 @@ def test_streaming_chat_completion( assert response_string == "hello world" sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "some-model" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS]) == expected_system_instructions ) - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "model-id" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "model-id" assert ( "Message demonstrating the absence of truncation." @@ -1390,11 +1391,15 @@ def test_streaming_chat_completion( import tiktoken # type: ignore # noqa: F401 assert ( - span["attributes"]["gen_ai.usage.output_tokens"] == expected_output_tokens + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] + == expected_output_tokens ) - assert span["attributes"]["gen_ai.usage.input_tokens"] == expected_input_tokens assert ( - span["attributes"]["gen_ai.usage.total_tokens"] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] + == expected_input_tokens + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == expected_output_tokens + expected_input_tokens ) @@ -1504,29 +1509,29 @@ async def test_streaming_chat_completion_async_no_sensitive_data( assert response_string == "hello world" sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "some-model" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "model-id" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "model-id" - assert SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] try: import tiktoken # type: ignore # noqa: F401 - assert span["attributes"]["gen_ai.usage.output_tokens"] == 2 - assert span["attributes"]["gen_ai.usage.input_tokens"] == 7 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 9 except ImportError: pass # if tiktoken is not installed, we can't guarantee token usage will be calculated properly @@ -1720,21 +1725,21 @@ async def test_streaming_chat_completion_async( assert response_string == "hello world" sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING] is True - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "some-model" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY] == 0.1 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY] == 0.2 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "model-id" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "model-id" assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS]) == expected_system_instructions ) @@ -1749,11 +1754,15 @@ async def test_streaming_chat_completion_async( import tiktoken # type: ignore # noqa: F401 assert ( - span["attributes"]["gen_ai.usage.output_tokens"] == expected_output_tokens + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] + == expected_output_tokens ) - assert span["attributes"]["gen_ai.usage.input_tokens"] == expected_input_tokens assert ( - span["attributes"]["gen_ai.usage.total_tokens"] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] + == expected_input_tokens + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == expected_output_tokens + expected_input_tokens ) @@ -1884,14 +1893,17 @@ def test_embeddings_create_no_sensitive_data( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.embeddings" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-3-large" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.embeddings" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "text-embedding-3-large" + ) - assert SPANDATA.GEN_AI_EMBEDDINGS_INPUT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT not in span["attributes"] - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.parametrize( @@ -2004,17 +2016,20 @@ def test_embeddings_create( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.embeddings" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-3-large" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.embeddings" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "text-embedding-3-large" + ) assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT]) == expected_embeddings_input ) - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio @@ -2057,14 +2072,17 @@ async def test_embeddings_create_async_no_sensitive_data( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.embeddings" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-3-large" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.embeddings" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "text-embedding-3-large" + ) - assert SPANDATA.GEN_AI_EMBEDDINGS_INPUT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT not in span["attributes"] - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio @@ -2178,17 +2196,20 @@ async def test_embeddings_create_async( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.embeddings" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-3-large" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.embeddings" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] + == "text-embedding-3-large" + ) assert ( - json.loads(span["attributes"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT]) + json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT]) == expected_embeddings_input ) - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 def test_embeddings_create_raises_error( @@ -2278,7 +2299,7 @@ def test_span_origin_nonstreaming_chat( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] - assert spans[0]["attributes"]["sentry.origin"] == "auto.ai.openai" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.openai" @pytest.mark.asyncio @@ -2315,7 +2336,7 @@ async def test_span_origin_nonstreaming_chat_async( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] - assert spans[0]["attributes"]["sentry.origin"] == "auto.ai.openai" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.openai" def test_span_origin_streaming_chat( @@ -2376,7 +2397,7 @@ def test_span_origin_streaming_chat( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] - assert spans[0]["attributes"]["sentry.origin"] == "auto.ai.openai" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.openai" @pytest.mark.asyncio @@ -2446,7 +2467,7 @@ async def test_span_origin_streaming_chat_async( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] - assert spans[0]["attributes"]["sentry.origin"] == "auto.ai.openai" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.openai" def test_span_origin_embeddings( @@ -2478,7 +2499,7 @@ def test_span_origin_embeddings( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] - assert spans[0]["attributes"]["sentry.origin"] == "auto.ai.openai" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.openai" @pytest.mark.asyncio @@ -2511,7 +2532,7 @@ async def test_span_origin_embeddings_async( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] - assert spans[0]["attributes"]["sentry.origin"] == "auto.ai.openai" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.openai" @pytest.mark.skipif( @@ -2565,11 +2586,11 @@ def test_completions_token_usage_with_detailed_fields( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 5 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] == 8 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS] == 5 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS] == 8 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 def test_completions_token_usage_manual_input_counting( @@ -2623,10 +2644,10 @@ def test_completions_token_usage_manual_input_counting( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 10 if tiktoken_encoding_if_installed(): - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 3 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 3 @pytest.mark.skipif( @@ -2683,10 +2704,10 @@ def test_completions_token_usage_manual_output_counting_streaming( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 if tiktoken_encoding_if_installed(): - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 def test_completions_token_usage_manual_output_counting_choices( @@ -2752,10 +2773,10 @@ def test_completions_token_usage_manual_output_counting_choices( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 if tiktoken_encoding_if_installed(): - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 @pytest.mark.skipif(SKIP_RESPONSES_TESTS, reason="Responses API not available") @@ -2807,10 +2828,10 @@ def test_responses_token_usage_manual_output_counting_response_output( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 20 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 20 if tiktoken_encoding_if_installed(): - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 @pytest.mark.skipif(SKIP_RESPONSES_TESTS, reason="Responses API not available") @@ -2849,28 +2870,28 @@ def test_ai_client_span_responses_api_no_sensitive_data( assert len(spans) == 1 expected_attributes = { - "gen_ai.operation.name": "chat", - "gen_ai.request.max_tokens": 100, - "gen_ai.request.temperature": 0.7, - "gen_ai.request.top_p": 0.9, - "gen_ai.request.reasoning.level": "high", - "gen_ai.request.model": "gpt-4o", - "gen_ai.response.model": "response-model-id", - "gen_ai.response.streaming": False, - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", - "gen_ai.usage.input_tokens": 20, - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: 5, - "gen_ai.usage.output_tokens": 10, - SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, - "gen_ai.usage.total_tokens": 30, - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": "auto.ai.openai", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS: 100, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE: 0.7, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P: 0.9, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL: "high", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "gpt-4o", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "response-model-id", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: False, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS: 20, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: 5, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 10, + ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 30, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.openai", } for attr, value in expected_attributes.items(): assert spans[0]["attributes"][attr] == value - assert "gen_ai.system_instructions" not in spans[0]["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in spans[0]["attributes"] assert "gen_ai.request.messages" not in spans[0]["attributes"] assert "gen_ai.response.text" not in spans[0]["attributes"] @@ -2921,7 +2942,9 @@ def test_ai_client_span_responses_tool_definitions( sentry_sdk.flush() spans = [item.payload for item in items] - assert json.loads(spans[0]["attributes"][SPANDATA.GEN_AI_TOOL_DEFINITIONS]) == [ + assert json.loads( + spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_DEFINITIONS] + ) == [ { "type": "function", "name": "name", @@ -3131,28 +3154,28 @@ def test_ai_client_span_responses_api( assert len(spans) == 1 expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.max_tokens": 100, - "gen_ai.request.temperature": 0.7, - "gen_ai.request.top_p": 0.9, - "gen_ai.request.reasoning.level": "high", - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", - "gen_ai.response.model": "response-model-id", - "gen_ai.response.streaming": False, - "gen_ai.usage.input_tokens": 20, - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: 5, - "gen_ai.usage.output_tokens": 10, - SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, - "gen_ai.usage.total_tokens": 30, + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS: 100, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE: 0.7, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P: 0.9, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL: "high", + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "response-model-id", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: False, + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS: 20, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: 5, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 10, + ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 30, "gen_ai.request.messages": safe_serialize(expected_request_messages), - "gen_ai.request.model": "gpt-4o", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "gpt-4o", "gen_ai.response.text": "the model response", - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": "auto.ai.openai", + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.openai", } if expected_system_instructions is not None: - expected_data["gen_ai.system_instructions"] = safe_serialize( + expected_data[ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] = safe_serialize( expected_system_instructions ) @@ -3196,9 +3219,9 @@ def test_responses_api_conversation_id( (span,) = (item.payload for item in items) if expected_id is None: - assert "gen_ai.conversation.id" not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in span["attributes"] else: - assert span["attributes"]["gen_ai.conversation.id"] == expected_id + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID] == expected_id @pytest.mark.parametrize( @@ -3237,10 +3260,10 @@ def test_responses_api_reasoning_level( span = next(item.payload for item in items if item.type == "span") if expected_level is None: - assert SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL not in span["attributes"] else: assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL] == expected_level ) @@ -3278,7 +3301,7 @@ def test_error_in_responses_api( # make sure the span where the error occurred is captured sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] - assert spans[0]["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT (error_event,) = (item.payload for item in items if item.type == "event") @@ -3473,28 +3496,28 @@ async def test_ai_client_span_responses_async_api( assert len(spans) == 1 expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.max_tokens": 100, - "gen_ai.request.temperature": 0.7, - "gen_ai.request.top_p": 0.9, - "gen_ai.request.reasoning.level": "high", + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS: 100, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE: 0.7, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P: 0.9, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL: "high", "gen_ai.request.messages": safe_serialize(expected_request_messages), - "gen_ai.request.model": "gpt-4o", - "gen_ai.response.model": "response-model-id", - "gen_ai.response.streaming": False, - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", - "gen_ai.usage.input_tokens": 20, - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: 5, - "gen_ai.usage.output_tokens": 10, - SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, - "gen_ai.usage.total_tokens": 30, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "gpt-4o", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "response-model-id", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: False, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS: 20, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: 5, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 10, + ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 30, "gen_ai.response.text": "the model response", - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": "auto.ai.openai", + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.openai", } if expected_system_instructions is not None: - expected_data["gen_ai.system_instructions"] = safe_serialize( + expected_data[ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] = safe_serialize( expected_system_instructions ) @@ -3697,36 +3720,38 @@ async def test_ai_client_span_streaming_responses_async_api( sentry_sdk.flush() spans = [item.payload for item in items] spans = [ - span for span in spans if span["attributes"].get("sentry.op") == OP.GEN_AI_CHAT + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_CHAT ] assert len(spans) == 1 expected_data = { - "gen_ai.operation.name": "chat", - "gen_ai.request.max_tokens": 100, + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS: 100, "gen_ai.request.messages": safe_serialize(expected_request_messages), - "gen_ai.request.temperature": 0.7, - "gen_ai.request.top_p": 0.9, - "gen_ai.request.reasoning.level": "high", - "gen_ai.response.model": "response-model-id", - "gen_ai.response.streaming": True, - SPANDATA.GEN_AI_PROVIDER_NAME: "openai", - SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK: mock.ANY, - "gen_ai.usage.input_tokens": 20, - SPANDATA.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: 5, - "gen_ai.usage.output_tokens": 10, - SPANDATA.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, - "gen_ai.usage.total_tokens": 30, - "gen_ai.request.model": "gpt-4o", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE: 0.7, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P: 0.9, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL: "high", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "response-model-id", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: True, + ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME: "openai", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK: mock.ANY, + ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS: 20, + ATTRIBUTE_NAMES.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: 5, + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 10, + ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, + ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 30, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "gpt-4o", "gen_ai.response.text": "hello world", - "sentry.environment": "production", - "sentry.op": OP.GEN_AI_CHAT, - "sentry.origin": "auto.ai.openai", + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.openai", } if expected_system_instructions is not None: - expected_data["gen_ai.system_instructions"] = safe_serialize( + expected_data[ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS] = safe_serialize( expected_system_instructions ) @@ -3768,7 +3793,7 @@ async def test_error_in_responses_async_api( # make sure the span where the error occurred is captured sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] - assert spans[0]["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT (error_event,) = (item.payload for item in items if item.type == "event") @@ -3903,21 +3928,23 @@ def test_streaming_responses_api( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL] == "high" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL] == "high" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "response-model-id" + ) assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '["hello"]' assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "hello world" - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.skipif(SKIP_RESPONSES_TESTS, reason="Responses API not available") @@ -3971,21 +3998,23 @@ def test_streaming_responses_api_no_sensitive_data( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL] == "high" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL] == "high" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "response-model-id" + ) assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio @@ -4039,21 +4068,23 @@ async def test_streaming_responses_api_async( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL] == "high" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL] == "high" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "response-model-id" + ) assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '["hello"]' assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "hello world" - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 @pytest.mark.asyncio @@ -4107,21 +4138,23 @@ async def test_streaming_responses_api_async_no_sensitive_data( sentry_sdk.flush() (span,) = (item.payload for item in items) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "openai" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9 - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL] == "high" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS] == 100 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE] == 0.7 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL] == "high" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "response-model-id" + ) assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] - assert span["attributes"]["gen_ai.usage.input_tokens"] == 20 - assert span["attributes"]["gen_ai.usage.output_tokens"] == 10 - assert span["attributes"]["gen_ai.usage.total_tokens"] == 30 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 30 # Feature added in https://github.com/openai/openai-python/pull/1952 @@ -4177,10 +4210,10 @@ def test_chat_completion_reasoning_level( span = next(item.payload for item in items if item.type == "span") if expected_level is None: - assert SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL not in span["attributes"] else: assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_REASONING_LEVEL] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL] == expected_level ) @@ -4244,7 +4277,7 @@ def test_openai_message_role_mapping( # Verify that the span was created correctly sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) @@ -4321,11 +4354,11 @@ def test_streaming_chat_completion_ttft( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" # Verify TTFT is captured - assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] - ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] + ttft = span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] assert isinstance(ttft, float) assert ttft > 0 @@ -4403,11 +4436,11 @@ async def test_streaming_chat_completion_ttft_async( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == "gen_ai.chat" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" # Verify TTFT is captured - assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] - ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] + ttft = span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] assert isinstance(ttft, float) assert ttft > 0 @@ -4452,11 +4485,11 @@ def test_streaming_responses_api_ttft( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT # Verify TTFT is captured - assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] - ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] + ttft = span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] assert isinstance(ttft, float) assert ttft > 0 @@ -4503,11 +4536,11 @@ async def test_streaming_responses_api_ttft_async( sentry_sdk.flush() span = next(item.payload for item in items) - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT # Verify TTFT is captured - assert SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] - ttft = span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK in span["attributes"] + ttft = span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] assert isinstance(ttft, float) assert ttft > 0 diff --git a/tests/integrations/typesafe/test_typesafe.py b/tests/integrations/typesafe/test_typesafe.py index a2eec14bdd..6f0fc162e6 100644 --- a/tests/integrations/typesafe/test_typesafe.py +++ b/tests/integrations/typesafe/test_typesafe.py @@ -20,8 +20,10 @@ except ImportError: Image = None # type: ignore[assignment, misc] +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.typesafe import TypeSafeIntegration @@ -110,20 +112,20 @@ def test_system_one(sentry_init, capture_items, typesafe_response): (span,) = ( span for span in spans - if span["attributes"].get("sentry.op") == OP.GEN_AI_DECIDE + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_DECIDE ) assert span["name"] == "decide jev-latest" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "typesafe" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "decide" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "jev-latest" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "typesafe" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "decide" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "jev-latest" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "jev-latest" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "jev-latest" - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 12 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 - assert json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES]) == [ { "type": "evaluation", "state": { @@ -163,7 +165,7 @@ def test_system_one(sentry_init, capture_items, typesafe_response): } ] - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ { "type": "evaluation", "answers": { @@ -263,20 +265,20 @@ async def test_system_one_async(sentry_init, capture_items, typesafe_response): (span,) = ( span for span in spans - if span["attributes"].get("sentry.op") == OP.GEN_AI_DECIDE + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.GEN_AI_DECIDE ) assert span["name"] == "decide jev-latest" - assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "typesafe" - assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "decide" - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL] == "jev-latest" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "typesafe" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "decide" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "jev-latest" - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "jev-latest" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "jev-latest" - assert span["attributes"][SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 12 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 12 + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 3 - assert json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES]) == [ { "type": "evaluation", "state": { @@ -316,7 +318,7 @@ async def test_system_one_async(sentry_init, capture_items, typesafe_response): } ] - assert json.loads(span["attributes"][SPANDATA.GEN_AI_OUTPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ { "type": "evaluation", "answers": { @@ -401,7 +403,7 @@ def test_system_one_with_inline_image( spans = [item.payload for item in items] (span,) = spans - assert json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES]) == [ { "type": "evaluation", "state": [ @@ -474,7 +476,7 @@ async def test_system_one_async_with_inline_image( spans = [item.payload for item in items] (span,) = spans - assert json.loads(span["attributes"][SPANDATA.GEN_AI_INPUT_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_INPUT_MESSAGES]) == [ { "type": "evaluation", "state": [ From 3500c9e069652abc4ad72b7ed517242d4ac3606f Mon Sep 17 00:00:00 2001 From: srothh Date: Mon, 30 Nov 2026 11:12:48 +0100 Subject: [PATCH 3/3] ref(attributes): Use sentry-conventions for deprecated AI attributes Reference deprecated attributes through ATTRIBUTE_NAMES instead of SPANDATA in AI integrations and their tests. The sent keys stay the same. cohere now takes its response attribute keys from its mappings instead of building them from the field names. #8323 PY-3090 Co-Authored-By: Claude Opus 5.5 --- sentry_sdk/integrations/anthropic.py | 11 +- sentry_sdk/integrations/cohere.py | 61 +++---- .../integrations/google_genai/streaming.py | 5 +- sentry_sdk/integrations/google_genai/utils.py | 10 +- sentry_sdk/integrations/huggingface_hub.py | 16 +- sentry_sdk/integrations/litellm.py | 5 +- sentry_sdk/integrations/openai.py | 35 ++-- .../integrations/anthropic/test_anthropic.py | 156 +++++++++++------- tests/integrations/cohere/test_cohere.py | 78 ++++----- .../google_genai/test_google_genai.py | 140 ++++++++++------ .../huggingface_hub/test_huggingface_hub.py | 137 +++++++-------- tests/integrations/litellm/test_litellm.py | 60 ++++--- tests/integrations/openai/test_openai.py | 90 +++++----- 13 files changed, 459 insertions(+), 345 deletions(-) diff --git a/sentry_sdk/integrations/anthropic.py b/sentry_sdk/integrations/anthropic.py index b1a0e38908..07264f20f8 100644 --- a/sentry_sdk/integrations/anthropic.py +++ b/sentry_sdk/integrations/anthropic.py @@ -13,7 +13,7 @@ set_data_normalized, transform_anthropic_content_part, ) -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.traces import Span from sentry_sdk.utils import ( @@ -550,7 +550,7 @@ def _set_common_input_data( if sentry_sdk.get_client().options["data_collection"]["gen_ai"]["inputs"]: if tools is not None and _is_given(tools) and len(tools) > 0: # type: ignore span.set_attribute( - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools) + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS, safe_serialize(tools) ) if messages is None or len(messages) == 0: # type: ignore @@ -609,7 +609,7 @@ def _set_common_input_data( set_data_normalized( span, - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, role_normalized_messages, unpack=False, ) @@ -740,7 +740,7 @@ def _set_output_data( if len(tool_calls) > 0: set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, tool_calls, unpack=False, ) @@ -773,7 +773,8 @@ def _set_output_data( if output_tokens_reasoning is not None: span.set_attribute( - SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING, output_tokens_reasoning + ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING, + output_tokens_reasoning, ) if input_tokens is not None and output_tokens is not None: diff --git a/sentry_sdk/integrations/cohere.py b/sentry_sdk/integrations/cohere.py index e08b57976b..306177ecf6 100644 --- a/sentry_sdk/integrations/cohere.py +++ b/sentry_sdk/integrations/cohere.py @@ -6,7 +6,6 @@ from sentry_sdk import consts from sentry_sdk.ai.utils import set_data_normalized -from sentry_sdk.consts import SPANDATA from sentry_sdk.traces import Span if TYPE_CHECKING: @@ -46,32 +45,32 @@ COLLECTED_CHAT_PARAMS = { - "model": SPANDATA.AI_MODEL_ID, - "k": SPANDATA.AI_TOP_K, - "p": SPANDATA.AI_TOP_P, - "seed": SPANDATA.AI_SEED, - "frequency_penalty": SPANDATA.AI_FREQUENCY_PENALTY, - "presence_penalty": SPANDATA.AI_PRESENCE_PENALTY, - "raw_prompting": SPANDATA.AI_RAW_PROMPTING, + "model": ATTRIBUTE_NAMES._AI_MODEL_ID, + "k": ATTRIBUTE_NAMES.AI_TOP_K, + "p": ATTRIBUTE_NAMES.AI_TOP_P, + "seed": ATTRIBUTE_NAMES.AI_SEED, + "frequency_penalty": ATTRIBUTE_NAMES.AI_FREQUENCY_PENALTY, + "presence_penalty": ATTRIBUTE_NAMES.AI_PRESENCE_PENALTY, + "raw_prompting": ATTRIBUTE_NAMES.AI_RAW_PROMPTING, } COLLECTED_PII_CHAT_PARAMS = { - "tools": SPANDATA.AI_TOOLS, - "preamble": SPANDATA.AI_PREAMBLE, + "tools": ATTRIBUTE_NAMES.AI_TOOLS, + "preamble": ATTRIBUTE_NAMES.AI_PREAMBLE, } COLLECTED_CHAT_RESP_ATTRS = { - "generation_id": SPANDATA.AI_GENERATION_ID, - "is_search_required": SPANDATA.AI_SEARCH_REQUIRED, - "finish_reason": SPANDATA.AI_FINISH_REASON, + "generation_id": ATTRIBUTE_NAMES.AI_GENERATION_ID, + "is_search_required": ATTRIBUTE_NAMES.AI_IS_SEARCH_REQUIRED, + "finish_reason": ATTRIBUTE_NAMES.AI_FINISH_REASON, } COLLECTED_PII_CHAT_RESP_ATTRS = { - "citations": SPANDATA.AI_CITATIONS, - "documents": SPANDATA.AI_DOCUMENTS, - "search_queries": SPANDATA.AI_SEARCH_QUERIES, - "search_results": SPANDATA.AI_SEARCH_RESULTS, - "tool_calls": SPANDATA.AI_TOOL_CALLS, + "citations": ATTRIBUTE_NAMES.AI_CITATIONS, + "documents": ATTRIBUTE_NAMES.AI_DOCUMENTS, + "search_queries": ATTRIBUTE_NAMES.AI_SEARCH_QUERIES, + "search_results": ATTRIBUTE_NAMES.AI_SEARCH_RESULTS, + "tool_calls": ATTRIBUTE_NAMES.AI_TOOL_CALLS, } @@ -107,16 +106,16 @@ def collect_chat_response_fields( if hasattr(res, "text"): set_data_normalized( span, - SPANDATA.AI_RESPONSES, + ATTRIBUTE_NAMES.AI_RESPONSES, [res.text], ) - for pii_attr in COLLECTED_PII_CHAT_RESP_ATTRS: + for pii_attr, key in COLLECTED_PII_CHAT_RESP_ATTRS.items(): if hasattr(res, pii_attr): - set_data_normalized(span, "ai." + pii_attr, getattr(res, pii_attr)) + set_data_normalized(span, key, getattr(res, pii_attr)) - for attr in COLLECTED_CHAT_RESP_ATTRS: + for attr, key in COLLECTED_CHAT_RESP_ATTRS.items(): if hasattr(res, attr): - set_data_normalized(span, "ai." + attr, getattr(res, attr)) + set_data_normalized(span, key, getattr(res, attr)) if hasattr(res, "meta"): if hasattr(res.meta, "billed_units"): @@ -164,7 +163,9 @@ def collect_chat_response_fields( ) if hasattr(res.meta, "warnings"): - set_data_normalized(span, SPANDATA.AI_WARNINGS, res.meta.warnings) + set_data_normalized( + span, ATTRIBUTE_NAMES.AI_WARNINGS, res.meta.warnings + ) @wraps(f) def new_chat(*args: "Any", **kwargs: "Any") -> "Any": @@ -201,7 +202,7 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any": if sentry_sdk.get_client().options["data_collection"]["gen_ai"]["inputs"]: set_data_normalized( span, - SPANDATA.AI_INPUT_MESSAGES, + ATTRIBUTE_NAMES.AI_INPUT_MESSAGES, list( map( lambda x: { @@ -220,7 +221,7 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any": for k, v in COLLECTED_CHAT_PARAMS.items(): if k in kwargs: set_data_normalized(span, v, kwargs[k]) - set_data_normalized(span, SPANDATA.AI_STREAMING, False) + set_data_normalized(span, ATTRIBUTE_NAMES.AI_STREAMING, False) if streaming: old_iterator = res @@ -287,18 +288,20 @@ def new_embed(*args: "Any", **kwargs: "Any") -> "Any": ] ): if isinstance(kwargs["texts"], str): - set_data_normalized(span, SPANDATA.AI_TEXTS, [kwargs["texts"]]) + set_data_normalized( + span, ATTRIBUTE_NAMES.AI_TEXTS, [kwargs["texts"]] + ) elif ( isinstance(kwargs["texts"], list) and len(kwargs["texts"]) > 0 and isinstance(kwargs["texts"][0], str) ): set_data_normalized( - span, SPANDATA.AI_INPUT_MESSAGES, kwargs["texts"] + span, ATTRIBUTE_NAMES.AI_INPUT_MESSAGES, kwargs["texts"] ) if "model" in kwargs: - set_data_normalized(span, SPANDATA.AI_MODEL_ID, kwargs["model"]) + set_data_normalized(span, ATTRIBUTE_NAMES._AI_MODEL_ID, kwargs["model"]) try: res = f(*args, **kwargs) except Exception as e: diff --git a/sentry_sdk/integrations/google_genai/streaming.py b/sentry_sdk/integrations/google_genai/streaming.py index e0b5e42bbc..00912e6b48 100644 --- a/sentry_sdk/integrations/google_genai/streaming.py +++ b/sentry_sdk/integrations/google_genai/streaming.py @@ -4,7 +4,6 @@ import sentry_sdk from sentry_sdk.ai.utils import set_data_normalized -from sentry_sdk.consts import SPANDATA from sentry_sdk.traces import Span from sentry_sdk.utils import ( safe_serialize, @@ -156,13 +155,13 @@ def set_span_data_for_streaming_response( if accumulated_response.get("tool_calls"): if client.options["data_collection"]["gen_ai"]["outputs"]: span.set_attribute( - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, safe_serialize(accumulated_response["tool_calls"]), ) if accumulated_response.get("text"): if client.options["data_collection"]["gen_ai"]["outputs"]: span.set_attribute( - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, safe_serialize([accumulated_response["text"]]), ) diff --git a/sentry_sdk/integrations/google_genai/utils.py b/sentry_sdk/integrations/google_genai/utils.py index 1fdd747667..87bf0f95d2 100644 --- a/sentry_sdk/integrations/google_genai/utils.py +++ b/sentry_sdk/integrations/google_genai/utils.py @@ -27,7 +27,7 @@ set_data_normalized, transform_google_content_part, ) -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.utils import ( capture_internal_exceptions, event_from_exception, @@ -924,7 +924,7 @@ def set_span_data_for_request( if client.options["data_collection"]["gen_ai"]["inputs"]: set_data_normalized( span, - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS, formatted_tools, unpack=False, ) @@ -957,7 +957,7 @@ def set_span_data_for_request( normalized_messages = normalize_message_roles(messages) set_data_normalized( span, - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, normalized_messages, unpack=False, ) @@ -1035,14 +1035,14 @@ def set_span_data_for_response( if tool_calls: if client.options["data_collection"]["gen_ai"]["outputs"]: span.set_attribute( - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, safe_serialize(tool_calls) + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, safe_serialize(tool_calls) ) if client.options["data_collection"]["gen_ai"]["outputs"]: response_texts = _extract_response_text(response) if response_texts: span.set_attribute( - SPANDATA.GEN_AI_RESPONSE_TEXT, safe_serialize(response_texts) + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, safe_serialize(response_texts) ) diff --git a/sentry_sdk/integrations/huggingface_hub.py b/sentry_sdk/integrations/huggingface_hub.py index bd9742b10b..283b51b988 100644 --- a/sentry_sdk/integrations/huggingface_hub.py +++ b/sentry_sdk/integrations/huggingface_hub.py @@ -7,7 +7,7 @@ import sentry_sdk from sentry_sdk.ai.utils import set_data_normalized -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.utils import ( capture_internal_exceptions, @@ -112,9 +112,9 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": } if client.options["data_collection"]["gen_ai"]["inputs"]: - attribute_mapping["tools"] = SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS + attribute_mapping["tools"] = ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS set_data_normalized( - span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompt, unpack=False + span, ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, prompt, unpack=False ) for attribute, span_attribute in attribute_mapping.items(): @@ -197,7 +197,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": if client.options["data_collection"]["gen_ai"]["outputs"]: set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, tool_calls, unpack=False, ) @@ -208,7 +208,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": if client.options["data_collection"]["gen_ai"]["outputs"]: set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, text_response, ) @@ -300,7 +300,7 @@ def new_details_iterator() -> "Iterable[Any]": if text_response: set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, text_response, ) @@ -389,7 +389,7 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]": ): set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, tool_calls, unpack=False, ) @@ -402,7 +402,7 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]": if text_response: set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, text_response, ) diff --git a/sentry_sdk/integrations/litellm.py b/sentry_sdk/integrations/litellm.py index d2d13bf9c3..88f7b0d44f 100644 --- a/sentry_sdk/integrations/litellm.py +++ b/sentry_sdk/integrations/litellm.py @@ -9,7 +9,6 @@ set_data_normalized, transform_openai_content_part, ) -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.utils import ( event_from_exception, @@ -137,7 +136,7 @@ def _input_callback(kwargs: "Dict[str, Any]") -> None: messages = _convert_message_parts(messages) set_data_normalized( span, - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, messages, unpack=False, ) @@ -208,7 +207,7 @@ def _success_callback( if response_messages: set_data_normalized( - span, SPANDATA.GEN_AI_RESPONSE_TEXT, response_messages + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, response_messages ) # Record token usage diff --git a/sentry_sdk/integrations/openai.py b/sentry_sdk/integrations/openai.py index 3ab7e23657..656c97cd62 100644 --- a/sentry_sdk/integrations/openai.py +++ b/sentry_sdk/integrations/openai.py @@ -40,7 +40,6 @@ normalize_message_roles, set_data_normalized, ) -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.traces import Span from sentry_sdk.utils import ( @@ -449,7 +448,7 @@ def _set_responses_api_input_data( normalized_messages = normalize_message_roles([messages]) # type: ignore set_data_normalized( span, - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, normalized_messages, unpack=False, ) @@ -463,7 +462,7 @@ def _set_responses_api_input_data( normalized_messages = normalize_message_roles(non_system_messages) # type: ignore set_data_normalized( span, - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, normalized_messages, unpack=False, ) @@ -531,7 +530,7 @@ def _set_completions_api_input_data( normalized_messages = normalize_message_roles([messages]) # type: ignore set_data_normalized( span, - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, normalized_messages, unpack=False, ) @@ -560,7 +559,7 @@ def _set_completions_api_input_data( normalized_messages = normalize_message_roles(non_system_messages) # type: ignore set_data_normalized( span, - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, normalized_messages, unpack=False, ) @@ -638,7 +637,9 @@ def _set_common_output_data( if choice.message is not None ] if len(response_text) > 0: - set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, response_text) + set_data_normalized( + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, response_text + ) _calculate_completions_token_usage( messages=input, @@ -676,14 +677,16 @@ def _set_common_output_data( if len(output_messages["tool"]) > 0: set_data_normalized( span, - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, output_messages["tool"], unpack=False, ) if len(output_messages["response"]) > 0: set_data_normalized( - span, SPANDATA.GEN_AI_RESPONSE_TEXT, output_messages["response"] + span, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, + output_messages["response"], ) _calculate_responses_token_usage( @@ -932,7 +935,9 @@ def _wrap_synchronous_completions_chunk_iterator( if len(data_buf) > 0: all_responses = ["".join(chunk) for chunk in data_buf] if client.options["data_collection"]["gen_ai"]["outputs"]: - set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, all_responses) + set_data_normalized( + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, all_responses + ) _calculate_completions_token_usage( messages=messages, @@ -995,7 +1000,9 @@ async def _wrap_asynchronous_completions_chunk_iterator( if len(data_buf) > 0: all_responses = ["".join(chunk) for chunk in data_buf] if client.options["data_collection"]["gen_ai"]["outputs"]: - set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, all_responses) + set_data_normalized( + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, all_responses + ) _calculate_completions_token_usage( messages=messages, @@ -1062,7 +1069,9 @@ def _wrap_synchronous_responses_event_iterator( if len(data_buf) > 0: all_responses = ["".join(chunk) for chunk in data_buf] if client.options["data_collection"]["gen_ai"]["outputs"]: - set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, all_responses) + set_data_normalized( + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, all_responses + ) if count_tokens_manually: _calculate_responses_token_usage( @@ -1130,7 +1139,9 @@ async def _wrap_asynchronous_responses_event_iterator( all_responses = ["".join(chunk) for chunk in data_buf] if client.options["data_collection"]["gen_ai"]["outputs"]: - set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, all_responses) + set_data_normalized( + span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, all_responses + ) if count_tokens_manually: _calculate_responses_token_usage( diff --git a/tests/integrations/anthropic/test_anthropic.py b/tests/integrations/anthropic/test_anthropic.py index 63a395276b..7c5ab7e638 100644 --- a/tests/integrations/anthropic/test_anthropic.py +++ b/tests/integrations/anthropic/test_anthropic.py @@ -66,7 +66,7 @@ async def __call__(self, *args, **kwargs): from anthropic.types.content_block import ContentBlock as TextBlock from sentry_sdk.ai.utils import transform_content_part -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.anthropic import ( AnthropicIntegration, _collect_ai_data, @@ -118,7 +118,9 @@ async def __call__(self, *args, **kwargs): ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS: [ {"type": "text", "content": "You are a helpful assistant."} ], - SPANDATA.GEN_AI_REQUEST_MESSAGES: [{"role": "user", "content": "Hello, Claude"}], + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: [ + {"role": "user", "content": "Hello, Claude"} + ], } DATA_COLLECTION_INPUT_DATA_KEYS = list(DATA_COLLECTION_EXPECTED_INPUT_DATA.keys()) @@ -351,7 +353,7 @@ def test_nonstreaming_create_message( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -440,7 +442,7 @@ def test_nonstreaming_create_message_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -510,7 +512,7 @@ async def test_nonstreaming_create_message_async( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -599,7 +601,7 @@ async def test_nonstreaming_create_message_async_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -684,7 +686,7 @@ def test_streaming_create_message( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -794,7 +796,7 @@ def test_streaming_create_message_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -872,7 +874,7 @@ def test_streaming_create_message_close( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -953,7 +955,7 @@ def test_streaming_create_message_api_error( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -1044,7 +1046,7 @@ def test_stream_messages( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -1154,7 +1156,7 @@ def test_stream_messages_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -1236,7 +1238,7 @@ def test_stream_messages_close( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -1317,7 +1319,7 @@ def test_stream_messages_api_error( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -1411,7 +1413,7 @@ async def test_streaming_create_message_async( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -1524,7 +1526,7 @@ async def test_streaming_create_message_async_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -1603,7 +1605,7 @@ async def test_streaming_create_message_async_close( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -1686,7 +1688,7 @@ async def test_streaming_create_message_async_api_error( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -1780,7 +1782,7 @@ async def test_stream_message_async( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -1890,7 +1892,7 @@ async def test_stream_message_async_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -1970,7 +1972,7 @@ async def test_stream_messages_async_api_error( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -2060,7 +2062,7 @@ async def test_stream_messages_async_close( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "Hello, Claude"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -2201,7 +2203,7 @@ def test_streaming_create_message_with_input_json_delta( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -2340,7 +2342,7 @@ def test_streaming_create_message_with_input_json_delta_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 @@ -2473,7 +2475,7 @@ def test_stream_messages_with_input_json_delta( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -2612,7 +2614,7 @@ def test_stream_messages_with_input_json_delta_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 @@ -2752,7 +2754,7 @@ async def test_streaming_create_message_with_input_json_delta_async( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -2898,7 +2900,7 @@ async def test_streaming_create_message_with_input_json_delta_async_no_sensitive assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 @@ -3039,7 +3041,7 @@ async def test_stream_message_with_input_json_delta_async( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ( - span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '[{"role": "user", "content": "What is the weather like in San Francisco?"}]' ) assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES]) == [ @@ -3186,7 +3188,7 @@ async def test_stream_message_with_input_json_delta_async_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 366 @@ -3518,10 +3520,12 @@ def mock_messages_create(*args, **kwargs): assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "anthropic" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in span["attributes"] # Parse the stored messages - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert stored_messages[0]["role"] == expected_role @@ -3587,8 +3591,10 @@ def test_nonstreaming_create_message_with_system_prompt( {"type": "text", "content": "You are a helpful assistant."} ] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in span["attributes"] + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(stored_messages) == 1 assert stored_messages[0]["role"] == "user" assert stored_messages[0]["content"] == "Hello, Claude" @@ -3670,7 +3676,7 @@ def test_nonstreaming_create_message_with_system_prompt_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -3744,8 +3750,10 @@ async def test_nonstreaming_create_message_with_system_prompt_async( {"type": "text", "content": "You are a helpful assistant."} ] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in span["attributes"] + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(stored_messages) == 1 assert stored_messages[0]["role"] == "user" assert stored_messages[0]["content"] == "Hello, Claude" @@ -3828,7 +3836,7 @@ async def test_nonstreaming_create_message_with_system_prompt_async_no_sensitive assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -3919,8 +3927,10 @@ def test_streaming_create_message_with_system_prompt( {"type": "text", "content": "You are a helpful assistant."} ] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in span["attributes"] + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(stored_messages) == 1 assert stored_messages[0]["role"] == "user" assert stored_messages[0]["content"] == "Hello, Claude" @@ -4017,7 +4027,7 @@ def test_streaming_create_message_with_system_prompt_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -4101,8 +4111,10 @@ def test_stream_messages_with_system_prompt( assert system_instructions == [ {"type": "text", "content": "You are a helpful assistant."} ] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in span["attributes"] + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(stored_messages) == 1 assert stored_messages[0]["role"] == "user" assert stored_messages[0]["content"] == "Hello, Claude" @@ -4196,7 +4208,7 @@ def test_stream_messages_with_system_prompt_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -4286,8 +4298,10 @@ async def test_stream_message_with_system_prompt_async( {"type": "text", "content": "You are a helpful assistant."} ] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in span["attributes"] + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(stored_messages) == 1 assert stored_messages[0]["role"] == "user" assert stored_messages[0]["content"] == "Hello, Claude" @@ -4386,7 +4400,7 @@ async def test_stream_message_with_system_prompt_async_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -4478,8 +4492,10 @@ async def test_streaming_create_message_with_system_prompt_async( {"type": "text", "content": "You are a helpful assistant."} ] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in span["attributes"] + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(stored_messages) == 1 assert stored_messages[0]["role"] == "user" @@ -4581,7 +4597,7 @@ async def test_streaming_create_message_with_system_prompt_async_no_sensitive_da assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "model" assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] assert ATTRIBUTE_NAMES.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -4650,8 +4666,10 @@ def test_system_prompt_with_complex_structure( {"type": "text", "content": "Be concise and clear."}, ] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in span["attributes"] + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(stored_messages) == 1 assert stored_messages[0]["role"] == "user" @@ -4863,7 +4881,9 @@ def test_message_with_url_image( spans = [item.payload for item in items] (span,) = spans - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) content = stored_messages[0]["content"] assert content[1] == { @@ -4918,7 +4938,9 @@ def test_message_with_file_image( spans = [item.payload for item in items] (span,) = spans - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) content = stored_messages[0]["content"] assert content[1] == { @@ -4972,7 +4994,9 @@ def test_message_with_url_pdf( spans = [item.payload for item in items] (span,) = spans - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) content = stored_messages[0]["content"] assert content[1] == { @@ -5027,7 +5051,9 @@ def test_message_with_file_document( spans = [item.payload for item in items] (span,) = spans - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) content = stored_messages[0]["content"] assert content[1] == { @@ -5082,7 +5108,7 @@ def test_binary_content_not_stored_when_pii_disabled( (span,) = spans # Messages should not be stored - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] def test_cache_tokens_nonstreaming( @@ -5180,7 +5206,9 @@ def test_reasoning_tokens_nonstreaming( # Reasoning tokens are part of output_tokens, so totals are unchanged. assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 150 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + ) @pytest.mark.skipif( @@ -5228,7 +5256,9 @@ async def test_reasoning_tokens_nonstreaming_async( # Reasoning tokens are part of output_tokens, so totals are unchanged. assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 150 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + ) def test_input_tokens_include_cache_write_nonstreaming( @@ -5764,7 +5794,9 @@ def test_reasoning_tokens_streaming( # Reasoning tokens are part of output_tokens, so totals are unchanged. assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 150 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + ) @pytest.mark.skipif( @@ -5830,4 +5862,6 @@ def test_stream_messages_reasoning_tokens( # Reasoning tokens are part of output_tokens, so totals are unchanged. assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 150 - assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + ) diff --git a/tests/integrations/cohere/test_cohere.py b/tests/integrations/cohere/test_cohere.py index a564610dfc..fe4317c25a 100644 --- a/tests/integrations/cohere/test_cohere.py +++ b/tests/integrations/cohere/test_cohere.py @@ -8,7 +8,7 @@ from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.cohere import CohereIntegration @@ -57,17 +57,17 @@ def test_nonstreaming_chat( span = items[0].payload assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES._AI_MODEL_ID] == "some-model" assert ( '{"role": "system", "content": "some context"}' - in span["attributes"][SPANDATA.AI_INPUT_MESSAGES] + in span["attributes"][ATTRIBUTE_NAMES.AI_INPUT_MESSAGES] ) assert ( '{"role": "user", "content": "hello"}' - in span["attributes"][SPANDATA.AI_INPUT_MESSAGES] + in span["attributes"][ATTRIBUTE_NAMES.AI_INPUT_MESSAGES] ) - assert "the model response" in span["attributes"][SPANDATA.AI_RESPONSES] + assert "the model response" in span["attributes"][ATTRIBUTE_NAMES.AI_RESPONSES] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 @@ -119,10 +119,10 @@ def test_nonstreaming_chat_no_sensitive_data( span = items[0].payload assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES._AI_MODEL_ID] == "some-model" - assert SPANDATA.AI_INPUT_MESSAGES not in span["attributes"] - assert SPANDATA.AI_RESPONSES not in span["attributes"] + assert ATTRIBUTE_NAMES.AI_INPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.AI_RESPONSES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 @@ -189,17 +189,17 @@ def test_streaming_chat( span = items[0].payload assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES._AI_MODEL_ID] == "some-model" assert ( '{"role": "system", "content": "some context"}' - in span["attributes"][SPANDATA.AI_INPUT_MESSAGES] + in span["attributes"][ATTRIBUTE_NAMES.AI_INPUT_MESSAGES] ) assert ( '{"role": "user", "content": "hello"}' - in span["attributes"][SPANDATA.AI_INPUT_MESSAGES] + in span["attributes"][ATTRIBUTE_NAMES.AI_INPUT_MESSAGES] ) - assert "the model response" in span["attributes"][SPANDATA.AI_RESPONSES] + assert "the model response" in span["attributes"][ATTRIBUTE_NAMES.AI_RESPONSES] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 @@ -266,10 +266,10 @@ def test_streaming_chat_no_sensitive_data( span = items[0].payload assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES._AI_MODEL_ID] == "some-model" - assert SPANDATA.AI_INPUT_MESSAGES not in span["attributes"] - assert SPANDATA.AI_RESPONSES not in span["attributes"] + assert ATTRIBUTE_NAMES.AI_INPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.AI_RESPONSES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 @@ -319,7 +319,7 @@ def test_embed( span = items[0].payload assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS - assert "hello" in span["attributes"][SPANDATA.AI_INPUT_MESSAGES] + assert "hello" in span["attributes"][ATTRIBUTE_NAMES.AI_INPUT_MESSAGES] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 10 @@ -368,7 +368,7 @@ def test_embed_no_sensitive_data( span = items[0].payload assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS - assert SPANDATA.AI_INPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.AI_INPUT_MESSAGES not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] == 10 @@ -569,26 +569,26 @@ def test_nonstreaming_chat_data_collection( assert len(items) == 1 attributes = items[0].payload["attributes"] - assert attributes[SPANDATA.AI_MODEL_ID] == "some-model" + assert attributes[ATTRIBUTE_NAMES._AI_MODEL_ID] == "some-model" assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 - assert attributes["ai.generation_id"] == "gen-1" + assert attributes[ATTRIBUTE_NAMES.AI_GENERATION_ID] == "gen-1" if expect_inputs: assert '{"role": "user", "content": "hello"}' in str( - attributes[SPANDATA.AI_INPUT_MESSAGES] + attributes[ATTRIBUTE_NAMES.AI_INPUT_MESSAGES] ) - assert attributes[SPANDATA.AI_PREAMBLE] == "be concise" + assert attributes[ATTRIBUTE_NAMES.AI_PREAMBLE] == "be concise" else: - assert SPANDATA.AI_INPUT_MESSAGES not in attributes - assert SPANDATA.AI_PREAMBLE not in attributes + assert ATTRIBUTE_NAMES.AI_INPUT_MESSAGES not in attributes + assert ATTRIBUTE_NAMES.AI_PREAMBLE not in attributes if expect_outputs: - assert "the model response" in str(attributes[SPANDATA.AI_RESPONSES]) - assert "doc-1" in str(attributes["ai.citations"]) + assert "the model response" in str(attributes[ATTRIBUTE_NAMES.AI_RESPONSES]) + assert "doc-1" in str(attributes[ATTRIBUTE_NAMES.AI_CITATIONS]) else: - assert SPANDATA.AI_RESPONSES not in attributes - assert "ai.citations" not in attributes + assert ATTRIBUTE_NAMES.AI_RESPONSES not in attributes + assert ATTRIBUTE_NAMES.AI_CITATIONS not in attributes @pytest.mark.parametrize( @@ -657,25 +657,25 @@ def test_streaming_chat_data_collection( assert len(items) == 1 attributes = items[0].payload["attributes"] - assert attributes[SPANDATA.AI_MODEL_ID] == "some-model" + assert attributes[ATTRIBUTE_NAMES._AI_MODEL_ID] == "some-model" assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 if expect_inputs: assert '{"role": "user", "content": "hello"}' in str( - attributes[SPANDATA.AI_INPUT_MESSAGES] + attributes[ATTRIBUTE_NAMES.AI_INPUT_MESSAGES] ) - assert attributes[SPANDATA.AI_PREAMBLE] == "be concise" + assert attributes[ATTRIBUTE_NAMES.AI_PREAMBLE] == "be concise" else: - assert SPANDATA.AI_INPUT_MESSAGES not in attributes - assert SPANDATA.AI_PREAMBLE not in attributes + assert ATTRIBUTE_NAMES.AI_INPUT_MESSAGES not in attributes + assert ATTRIBUTE_NAMES.AI_PREAMBLE not in attributes if expect_outputs: - assert "the model response" in str(attributes[SPANDATA.AI_RESPONSES]) - assert "doc-1" in str(attributes["ai.citations"]) + assert "the model response" in str(attributes[ATTRIBUTE_NAMES.AI_RESPONSES]) + assert "doc-1" in str(attributes[ATTRIBUTE_NAMES.AI_CITATIONS]) else: - assert SPANDATA.AI_RESPONSES not in attributes - assert "ai.citations" not in attributes + assert ATTRIBUTE_NAMES.AI_RESPONSES not in attributes + assert ATTRIBUTE_NAMES.AI_CITATIONS not in attributes @pytest.mark.parametrize( @@ -718,10 +718,10 @@ def test_embed_data_collection( assert len(items) == 1 attributes = items[0].payload["attributes"] - assert attributes[SPANDATA.AI_MODEL_ID] == "text-embedding-3-large" + assert attributes[ATTRIBUTE_NAMES._AI_MODEL_ID] == "text-embedding-3-large" assert attributes[ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 if expect_inputs: - assert "hello" in str(attributes[SPANDATA.AI_INPUT_MESSAGES]) + assert "hello" in str(attributes[ATTRIBUTE_NAMES.AI_INPUT_MESSAGES]) else: - assert SPANDATA.AI_INPUT_MESSAGES not in attributes + assert ATTRIBUTE_NAMES.AI_INPUT_MESSAGES not in attributes diff --git a/tests/integrations/google_genai/test_google_genai.py b/tests/integrations/google_genai/test_google_genai.py index 9f5ea38059..65fca6c9d8 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -9,7 +9,7 @@ import sentry_sdk from sentry_sdk._types import BLOB_DATA_SUBSTITUTE -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.google_genai import GoogleGenAIIntegration from sentry_sdk.integrations.google_genai.utils import extract_contents_messages @@ -165,7 +165,9 @@ def test_nonstreaming_generate_content( == "gemini-1.5-flash" ) - assert json.loads(chat_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) == [ { "role": "user", "content": [ @@ -182,7 +184,7 @@ def test_nonstreaming_generate_content( ] # Response text is stored as a JSON array - response_text = chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] + response_text = chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] # Parse the JSON array response_texts = json.loads(response_text) @@ -253,7 +255,7 @@ def test_nonstreaming_generate_content_no_sensitive_data( == "gemini-1.5-flash" ) - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in chat_span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in chat_span["attributes"] # Check token usage assert chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 @@ -426,7 +428,9 @@ def get_weather(location: str) -> str: invoke_span = next(item.payload for item in items) # Check that tools are recorded (data is serialized as a string) - tools_data_str = invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS] + tools_data_str = invoke_span["attributes"][ + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS + ] # Parse the JSON string to verify content tools_data = json.loads(tools_data_str) assert len(tools_data) == 2 @@ -628,7 +632,9 @@ def test_streaming_generate_content( sentry_sdk.flush() chat_span = next(item.payload for item in items) - assert json.loads(chat_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) == [ { "role": "user", "content": [ @@ -651,7 +657,7 @@ def test_streaming_generate_content( expected_full_text = "Hello! How can I help you today?" # Response text is stored as a JSON string chat_response_text = json.loads( - chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] ) assert chat_response_text == [expected_full_text] @@ -813,7 +819,7 @@ def test_multiple_candidates( # Should capture all responses # Response text is stored as a JSON string when there are multiple responses - response_text = chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] + response_text = chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] if isinstance(response_text, str) and response_text.startswith("["): # It's a JSON array @@ -1010,7 +1016,9 @@ def test_contents_as_none( invoke_span = next(item.payload for item in items) # Should handle None contents gracefully - messages = invoke_span["attributes"].get(SPANDATA.GEN_AI_REQUEST_MESSAGES, []) + messages = invoke_span["attributes"].get( + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, [] + ) # Should only have system message if any, not user message assert all(msg["role"] != "user" or msg["content"] is not None for msg in messages) @@ -1083,11 +1091,11 @@ def test_tool_calls_extraction( chat_span = next(item.payload for item in items) # The chat span # Check that tool calls are extracted and stored - assert SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS in chat_span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS in chat_span["attributes"] # Parse the JSON string to verify content tool_calls = json.loads( - chat_span["attributes"][SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS] + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS] ) assert len(tool_calls) == 2 @@ -1762,7 +1770,9 @@ def test_generate_content_with_content_object( sentry_sdk.flush() invoke_span = next(item.payload for item in items) - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 1 assert messages[0]["role"] == "user" @@ -1804,7 +1814,9 @@ def test_generate_content_with_dict_format( sentry_sdk.flush() invoke_span = next(item.payload for item in items) - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 1 assert messages[0]["role"] == "user" @@ -1855,7 +1867,9 @@ def test_generate_content_with_file_data( sentry_sdk.flush() invoke_span = next(item.payload for item in items) - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 1 assert messages[0]["role"] == "user" @@ -1911,7 +1925,9 @@ def test_generate_content_with_inline_data( sentry_sdk.flush() invoke_span = next(item.payload for item in items) - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 1 assert messages[0]["role"] == "user" @@ -1975,7 +1991,9 @@ def test_generate_content_with_function_response( spans = [item.payload for item in items] invoke_span = spans[0] - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 2 # First message is user message assert messages[1]["role"] == "tool" @@ -2029,7 +2047,9 @@ def test_generate_content_with_mixed_string_and_content( spans = [item.payload for item in items] invoke_span = spans[0] - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 3 # User message assert messages[2]["role"] == "user" @@ -2069,7 +2089,9 @@ def test_generate_content_with_part_object_directly( sentry_sdk.flush() invoke_span = next(item.payload for item in items) - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 1 assert messages[0]["role"] == "user" @@ -2119,7 +2141,9 @@ def test_generate_content_with_list_of_dicts( spans = [item.payload for item in items] invoke_span = spans[0] - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 3 assert messages[2]["role"] == "user" assert messages[2]["content"] == [{"text": "Second user message", "type": "text"}] @@ -2164,7 +2188,9 @@ def test_generate_content_with_dict_inline_data( sentry_sdk.flush() invoke_span = next(item.payload for item in items) - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 1 assert messages[0]["role"] == "user" @@ -2212,7 +2238,9 @@ def test_generate_content_without_parts_property_inline_data( sentry_sdk.flush() invoke_span = next(item.payload for item in items) - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 1 @@ -2267,7 +2295,9 @@ def test_generate_content_without_parts_property_inline_data_and_binary_data_wit sentry_sdk.flush() invoke_span = next(item.payload for item in items) - messages = json.loads(invoke_span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages = json.loads( + invoke_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(messages) == 1 assert messages[0]["role"] == "user" @@ -2605,11 +2635,13 @@ def __init__(self): DATA_COLLECTION_CHAT_EXPECTED_VALUES = { - SPANDATA.GEN_AI_REQUEST_MESSAGES: [{"role": "user", "content": "Tell me a joke"}], + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: [ + {"role": "user", "content": "Tell me a joke"} + ], ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS: [ {"type": "text", "content": "You are a helpful assistant."} ], - SPANDATA.GEN_AI_RESPONSE_TEXT: ["Hello! How can I help you today?"], + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT: ["Hello! How can I help you today?"], } DATA_COLLECTION_EMBED_EXPECTED_VALUES = { @@ -2626,21 +2658,21 @@ def __init__(self): pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], id="gen-ai-inputs-enabled-outputs-disabled", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], id="gen-ai-outputs-enabled-inputs-disabled", @@ -2711,14 +2743,14 @@ def test_generate_content_data_collection( [ pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, - [SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS], - [SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS], + [ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS], + [ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS], id="gen-ai-inputs-enabled-outputs-disabled-drops-tool-calls-only", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, - [SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS], - [SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS], + [ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS], + [ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS], id="gen-ai-inputs-disabled-outputs-enabled-drops-available-tools-only", ), ], @@ -2804,12 +2836,14 @@ def test_generate_content_data_collection_tools( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - if SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in expected_present: - available_tools = json.loads(span_data[SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS]) + if ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS in expected_present: + available_tools = json.loads( + span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS] + ) assert [tool["name"] for tool in available_tools] == ["get_weather"] - if SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS in expected_present: - tool_calls = json.loads(span_data[SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS]) + if ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS in expected_present: + tool_calls = json.loads(span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS]) assert [tool_call["name"] for tool_call in tool_calls] == ["get_weather"] assert json.loads(tool_calls[0]["arguments"]) == {"location": "San Francisco"} @@ -2820,21 +2854,21 @@ def test_generate_content_data_collection_tools( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], id="gen-ai-inputs-enabled-outputs-disabled", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], id="gen-ai-outputs-enabled-inputs-disabled", @@ -2937,14 +2971,14 @@ def test_streaming_generate_content_data_collection( [ pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, - [SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS], - [SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS], + [ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS], + [ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS], id="gen-ai-inputs-enabled-outputs-disabled-drops-tool-calls-only", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, - [SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS], - [SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS], + [ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS], + [ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS], id="gen-ai-inputs-disabled-outputs-enabled-drops-available-tools-only", ), ], @@ -3036,12 +3070,14 @@ def test_streaming_generate_content_data_collection_tools( for key in expected_absent: assert key not in span_data, f"{key} should not have been collected" - if SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in expected_present: - available_tools = json.loads(span_data[SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS]) + if ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS in expected_present: + available_tools = json.loads( + span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS] + ) assert [tool["name"] for tool in available_tools] == ["get_weather"] - if SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS in expected_present: - tool_calls = json.loads(span_data[SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS]) + if ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS in expected_present: + tool_calls = json.loads(span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS]) assert [tool_call["name"] for tool_call in tool_calls] == ["get_weather"] @@ -3121,21 +3157,21 @@ def test_embed_content_data_collection( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], id="gen-ai-inputs-enabled-outputs-disabled", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], id="gen-ai-outputs-enabled-inputs-disabled", diff --git a/tests/integrations/huggingface_hub/test_huggingface_hub.py b/tests/integrations/huggingface_hub/test_huggingface_hub.py index 1fa2a3dd15..a10c01b87b 100644 --- a/tests/integrations/huggingface_hub/test_huggingface_hub.py +++ b/tests/integrations/huggingface_hub/test_huggingface_hub.py @@ -6,7 +6,6 @@ from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.huggingface_hub import HuggingfaceHubIntegration from sentry_sdk.utils import package_version, safe_serialize from tests.conftest import ApproxDict @@ -467,8 +466,10 @@ def test_text_generation( ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } - expected_data["gen_ai.request.messages"] = "Hello" - expected_data["gen_ai.response.text"] = "[mocked] Hello! How can i help you?" + expected_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] = "Hello" + expected_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] = ( + "[mocked] Hello! How can i help you?" + ) assert span["attributes"] == ApproxDict(expected_data) @@ -534,8 +535,8 @@ def test_text_generation_no_sensitive_data( ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } - assert "gen_ai.request.messages" not in expected_data - assert "gen_ai.response.text" not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in expected_data assert span["attributes"] == ApproxDict(expected_data) @@ -604,8 +605,8 @@ def test_text_generation_streaming( ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } - expected_data["gen_ai.request.messages"] = "Hello" - expected_data["gen_ai.response.text"] = "the mocked model response" + expected_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] = "Hello" + expected_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] = "the mocked model response" assert span["attributes"] == ApproxDict(expected_data) @@ -673,8 +674,8 @@ def test_text_generation_streaming_no_sensitive_data( ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } - assert "gen_ai.request.messages" not in expected_data - assert "gen_ai.response.text" not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in expected_data assert span["attributes"] == ApproxDict(expected_data) @@ -750,7 +751,7 @@ def test_chat_completion( ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } - expected_data["gen_ai.request.messages"] = safe_serialize( + expected_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] = safe_serialize( [ { "role": "user", @@ -759,7 +760,9 @@ def test_chat_completion( {"role": "user", "content": "Hello!"}, ] ) - expected_data["gen_ai.response.text"] = "[mocked] Hello! How can I help you today?" + expected_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] = ( + "[mocked] Hello! How can I help you today?" + ) assert span["attributes"] == ApproxDict(expected_data) @@ -831,8 +834,8 @@ def test_chat_completion_no_sensitive_data( ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } - assert "gen_ai.request.messages" not in expected_data - assert "gen_ai.response.text" not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in expected_data assert span["attributes"] == ApproxDict(expected_data) @@ -909,7 +912,7 @@ def test_chat_completion_streaming( expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] = 14 expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] = 197 - expected_data["gen_ai.request.messages"] = safe_serialize( + expected_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] = safe_serialize( [ { "role": "user", @@ -918,7 +921,7 @@ def test_chat_completion_streaming( {"role": "user", "content": "Hello!"}, ] ) - expected_data["gen_ai.response.text"] = "the mocked model response" + expected_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] = "the mocked model response" assert span["attributes"] == ApproxDict(expected_data) @@ -994,8 +997,8 @@ def test_chat_completion_streaming_no_sensitive_data( expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] = 14 expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] = 197 - assert "gen_ai.request.messages" not in expected_data - assert "gen_ai.response.text" not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in expected_data assert span["attributes"] == ApproxDict(expected_data) @@ -1166,7 +1169,7 @@ def test_chat_completion_with_tools( expected_data = { ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", - "gen_ai.request.available_tools": '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS: '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "tool_calls", ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", @@ -1178,10 +1181,10 @@ def test_chat_completion_with_tools( ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } - expected_data["gen_ai.request.messages"] = ( + expected_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] = ( '[{"role": "user", "content": "What is the weather in Paris?"}]' ) - expected_data["gen_ai.response.tool_calls"] = ( + expected_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS] = ( '[{"function": {"arguments": {"location": "Paris"}, "name": "get_weather", "description": "None"}, "id": "call_123", "type": "function"}]' ) @@ -1264,10 +1267,10 @@ def test_chat_completion_with_tools_no_sensitive_data( ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.huggingface_hub", } - assert "gen_ai.request.messages" not in expected_data - assert "gen_ai.response.text" not in expected_data - assert "gen_ai.response.tool_calls" not in expected_data - assert "gen_ai.request.available_tools" not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS not in span["attributes"] assert span["attributes"] == ApproxDict(expected_data) @@ -1341,7 +1344,7 @@ def test_chat_completion_streaming_with_tools( expected_data = { ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", - "gen_ai.request.available_tools": '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS: '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "test-model", ATTRIBUTE_NAMES.GEN_AI_RESPONSE_FINISH_REASONS: "tool_calls", ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "test-model-123", @@ -1356,11 +1359,13 @@ def test_chat_completion_streaming_with_tools( expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] = 14 expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] = 197 - expected_data["gen_ai.request.messages"] = ( + expected_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] = ( '[{"role": "user", "content": "What is the weather in Paris?"}]' ) - expected_data["gen_ai.response.text"] = "response with tool calls follows" - expected_data["gen_ai.response.tool_calls"] = ( + expected_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] = ( + "response with tool calls follows" + ) + expected_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS] = ( '[{"function": {"arguments": {"location": "Paris"}, "name": "get_weather"}, "id": "call_123", "type": "function", "index": "None"}]' ) @@ -1449,10 +1454,10 @@ def test_chat_completion_streaming_with_tools_no_sensitive_data( expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] = 14 expected_data[ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS] = 197 - assert "gen_ai.request.messages" not in expected_data - assert "gen_ai.response.text" not in expected_data - assert "gen_ai.response.tool_calls" not in expected_data - assert "gen_ai.request.available_tools" not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS not in expected_data + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS not in span["attributes"] assert span["attributes"] == ApproxDict(expected_data) @@ -1481,20 +1486,20 @@ def test_chat_completion_streaming_with_tools_no_sensitive_data( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ], [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], id="gen-ai-inputs-enabled-outputs-disabled", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -1532,8 +1537,8 @@ def test_text_generation_data_collection( span_data = span["attributes"] expected_values = { - SPANDATA.GEN_AI_REQUEST_MESSAGES: "Hello", - SPANDATA.GEN_AI_RESPONSE_TEXT: "[mocked] Hello! How can i help you?", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: "Hello", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT: "[mocked] Hello! How can i help you?", } for key in expected_present: @@ -1557,20 +1562,20 @@ def test_text_generation_data_collection( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ], [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], id="gen-ai-inputs-enabled-outputs-disabled", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, [ - SPANDATA.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, ], [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -1609,8 +1614,8 @@ def test_text_generation_streaming_data_collection( span_data = span["attributes"] expected_values = { - SPANDATA.GEN_AI_REQUEST_MESSAGES: "Hello", - SPANDATA.GEN_AI_RESPONSE_TEXT: "the mocked model response", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: "Hello", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT: "the mocked model response", } for key in expected_present: @@ -1635,22 +1640,22 @@ def test_text_generation_streaming_data_collection( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS, ], [ - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, ], id="gen-ai-inputs-enabled-outputs-disabled", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, [ - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, ], [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -1692,9 +1697,9 @@ def test_chat_completion_data_collection_tools( span_data = span["attributes"] expected_values = { - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS: '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', - SPANDATA.GEN_AI_REQUEST_MESSAGES: '[{"role": "user", "content": "What is the weather in Paris?"}]', - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS: '[{"function": {"arguments": {"location": "Paris"}, "name": "get_weather", "description": "None"}, "id": "call_123", "type": "function"}]', + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS: '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: '[{"role": "user", "content": "What is the weather in Paris?"}]', + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS: '[{"function": {"arguments": {"location": "Paris"}, "name": "get_weather", "description": "None"}, "id": "call_123", "type": "function"}]', } for key in expected_present: @@ -1705,7 +1710,7 @@ def test_chat_completion_data_collection_tools( assert key not in span_data, f"{key} should not have been collected" # This response carries only tool calls, so there is never any response text - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span_data + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in span_data assert span_data[ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" assert span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL] == "test-model" @@ -1724,24 +1729,24 @@ def test_chat_completion_data_collection_tools( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS, ], [ - SPANDATA.GEN_AI_RESPONSE_TEXT, - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, ], id="gen-ai-inputs-enabled-outputs-disabled", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, [ - SPANDATA.GEN_AI_RESPONSE_TEXT, - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS, ], [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -1785,10 +1790,10 @@ def test_chat_completion_streaming_data_collection_tools( span_data = span["attributes"] expected_values = { - SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS: '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', - SPANDATA.GEN_AI_REQUEST_MESSAGES: '[{"role": "user", "content": "What is the weather in Paris?"}]', - SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS: '[{"function": {"arguments": {"location": "Paris"}, "name": "get_weather"}, "id": "call_123", "type": "function", "index": "None"}]', - SPANDATA.GEN_AI_RESPONSE_TEXT: "response with tool calls follows", + ATTRIBUTE_NAMES.GEN_AI_REQUEST_AVAILABLE_TOOLS: '[{"type": "function", "function": {"name": "get_weather", "description": "Get current weather", "parameters": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}}]', + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: '[{"role": "user", "content": "What is the weather in Paris?"}]', + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS: '[{"function": {"arguments": {"location": "Paris"}, "name": "get_weather"}, "id": "call_123", "type": "function", "index": "None"}]', + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT: "response with tool calls follows", } for key in expected_present: diff --git a/tests/integrations/litellm/test_litellm.py b/tests/integrations/litellm/test_litellm.py index a9b1329ed1..56dca29fa8 100644 --- a/tests/integrations/litellm/test_litellm.py +++ b/tests/integrations/litellm/test_litellm.py @@ -38,7 +38,7 @@ async def __call__(self, *args, **kwargs): from openai.types import CompletionUsage from sentry_sdk._types import BLOB_DATA_SUBSTITUTE -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.litellm import ( LiteLLMIntegration, _convert_message_parts, @@ -209,14 +209,14 @@ def test_nonstreaming_chat_completion( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", }, {"role": "user", "content": "Hello!"}, ] - assert SPANDATA.GEN_AI_RESPONSE_TEXT in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 @@ -297,8 +297,8 @@ def test_nonstreaming_chat_completion_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 @@ -380,14 +380,14 @@ async def test_async_nonstreaming_chat_completion( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" - assert json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", }, {"role": "user", "content": "Hello!"}, ] - assert SPANDATA.GEN_AI_RESPONSE_TEXT in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 @@ -469,8 +469,8 @@ async def test_async_nonstreaming_chat_completion_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME] == "openai" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME] == "chat" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 20 @@ -1743,7 +1743,9 @@ def test_binary_content_encoding_image_url( ) assert len(chat_spans) == 1 span = chat_spans[0] - messages_data = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages_data = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) blob_item = next( ( @@ -1839,7 +1841,9 @@ async def test_async_binary_content_encoding_image_url( ) assert len(chat_spans) == 1 span = chat_spans[0] - messages_data = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages_data = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) blob_item = next( ( @@ -1935,7 +1939,9 @@ def test_binary_content_encoding_mixed_content( ) assert len(chat_spans) == 1 span = chat_spans[0] - messages_data = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages_data = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) content_items = [ item for msg in messages_data if "content" in msg for item in msg["content"] @@ -2020,7 +2026,9 @@ async def test_async_binary_content_encoding_mixed_content( ) assert len(chat_spans) == 1 span = chat_spans[0] - messages_data = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages_data = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) content_items = [ item for msg in messages_data if "content" in msg for item in msg["content"] @@ -2101,7 +2109,9 @@ def test_binary_content_encoding_uri_type( ) assert len(chat_spans) == 1 span = chat_spans[0] - messages_data = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages_data = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) uri_item = next( ( @@ -2191,7 +2201,9 @@ async def test_async_binary_content_encoding_uri_type( ) assert len(chat_spans) == 1 span = chat_spans[0] - messages_data = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + messages_data = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) uri_item = next( ( @@ -2312,14 +2324,14 @@ def test_convert_message_parts_image_url_missing_url(): [ pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, - [SPANDATA.GEN_AI_REQUEST_MESSAGES], - [SPANDATA.GEN_AI_RESPONSE_TEXT], + [ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES], + [ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT], id="gen-ai-inputs-enabled-outputs-disabled", ), pytest.param( {"gen_ai": {"inputs": False, "outputs": True}}, - [SPANDATA.GEN_AI_RESPONSE_TEXT], - [SPANDATA.GEN_AI_REQUEST_MESSAGES], + [ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT], + [ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES], id="gen-ai-outputs-enabled-inputs-disabled", ), ], @@ -2392,11 +2404,13 @@ def test_chat_completion_data_collection( for key in expected_present: assert key in span_data, f"{key} should have been collected" - if SPANDATA.GEN_AI_REQUEST_MESSAGES in expected_present: - assert json.loads(span_data[SPANDATA.GEN_AI_REQUEST_MESSAGES]) == messages + if ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in expected_present: + assert ( + json.loads(span_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == messages + ) - if SPANDATA.GEN_AI_RESPONSE_TEXT in expected_present: - response_text = json.loads(span_data[SPANDATA.GEN_AI_RESPONSE_TEXT]) + if ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT in expected_present: + response_text = json.loads(span_data[ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT]) assert response_text["role"] == "assistant" assert response_text["content"] == "Test response" diff --git a/tests/integrations/openai/test_openai.py b/tests/integrations/openai/test_openai.py index 395570cfa7..409bbb7ea1 100644 --- a/tests/integrations/openai/test_openai.py +++ b/tests/integrations/openai/test_openai.py @@ -83,7 +83,7 @@ from unittest import mock # python 3.3 and above -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.openai import OpenAIIntegration from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.utils import safe_serialize @@ -341,8 +341,8 @@ def test_nonstreaming_chat_completion_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 @@ -495,12 +495,14 @@ def test_nonstreaming_chat_completion( == expected_system_instructions ) - assert "hello" in span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + assert "hello" in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] assert ( "Message demonstrating the absence of truncation." - in span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) + assert ( + "the model response" in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] ) - assert "the model response" in span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 @@ -570,8 +572,8 @@ async def test_nonstreaming_chat_completion_async_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P] == 0.9 assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 @@ -722,12 +724,14 @@ async def test_nonstreaming_chat_completion_async( == expected_system_instructions ) - assert "hello" in span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + assert "hello" in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] assert ( "Message demonstrating the absence of truncation." - in span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) + assert ( + "the model response" in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] ) - assert "the model response" in span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 @@ -854,8 +858,8 @@ def test_streaming_chat_completion_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "model-id" assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in span["attributes"] try: import tiktoken # type: ignore # noqa: F401 @@ -1382,10 +1386,10 @@ def test_streaming_chat_completion( assert ( "Message demonstrating the absence of truncation." - in span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] ) - assert "hello" in span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] - assert "hello world" in span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] + assert "hello" in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + assert "hello world" in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] try: import tiktoken # type: ignore # noqa: F401 @@ -1523,8 +1527,8 @@ async def test_streaming_chat_completion_async_no_sensitive_data( assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "model-id" assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in span["attributes"] - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in span["attributes"] try: import tiktoken # type: ignore # noqa: F401 @@ -1745,10 +1749,10 @@ async def test_streaming_chat_completion_async( assert ( "Message demonstrating the absence of truncation." - in span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] + in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] ) - assert "hello" in span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] - assert "hello world" in span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] + assert "hello" in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + assert "hello world" in span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] try: import tiktoken # type: ignore # noqa: F401 @@ -2892,8 +2896,8 @@ def test_ai_client_span_responses_api_no_sensitive_data( assert spans[0]["attributes"][attr] == value assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS not in spans[0]["attributes"] - assert "gen_ai.request.messages" not in spans[0]["attributes"] - assert "gen_ai.response.text" not in spans[0]["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in spans[0]["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in spans[0]["attributes"] @pytest.mark.skipif(SKIP_RESPONSES_TESTS, reason="Responses API not available") @@ -3167,9 +3171,11 @@ def test_ai_client_span_responses_api( ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 10, ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 30, - "gen_ai.request.messages": safe_serialize(expected_request_messages), + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: safe_serialize( + expected_request_messages + ), ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "gpt-4o", - "gen_ai.response.text": "the model response", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT: "the model response", ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.openai", } @@ -3501,7 +3507,9 @@ async def test_ai_client_span_responses_async_api( ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE: 0.7, ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P: 0.9, ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL: "high", - "gen_ai.request.messages": safe_serialize(expected_request_messages), + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: safe_serialize( + expected_request_messages + ), ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "gpt-4o", ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL: "response-model-id", ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING: False, @@ -3511,7 +3519,7 @@ async def test_ai_client_span_responses_async_api( ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS: 10, ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 30, - "gen_ai.response.text": "the model response", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT: "the model response", ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.openai", } @@ -3730,7 +3738,9 @@ async def test_ai_client_span_streaming_responses_async_api( expected_data = { ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat", ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS: 100, - "gen_ai.request.messages": safe_serialize(expected_request_messages), + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: safe_serialize( + expected_request_messages + ), ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE: 0.7, ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P: 0.9, ATTRIBUTE_NAMES.GEN_AI_REQUEST_REASONING_LEVEL: "high", @@ -3744,7 +3754,7 @@ async def test_ai_client_span_streaming_responses_async_api( ATTRIBUTE_NAMES.GEN_AI_USAGE_REASONING_OUTPUT_TOKENS: 8, ATTRIBUTE_NAMES.GEN_AI_USAGE_TOTAL_TOKENS: 30, ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "gpt-4o", - "gen_ai.response.text": "hello world", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT: "hello world", ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT, ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.ai.openai", @@ -3939,8 +3949,8 @@ def test_streaming_responses_api( span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "response-model-id" ) - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '["hello"]' - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "hello world" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '["hello"]' + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] == "hello world" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 @@ -4009,8 +4019,8 @@ def test_streaming_responses_api_no_sensitive_data( span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "response-model-id" ) - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 @@ -4079,8 +4089,8 @@ async def test_streaming_responses_api_async( span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "response-model-id" ) - assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES] == '["hello"]' - assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_TEXT] == "hello world" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '["hello"]' + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT] == "hello world" assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 @@ -4149,8 +4159,8 @@ async def test_streaming_responses_api_async_no_sensitive_data( span["attributes"][ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL] == "response-model-id" ) - assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT not in span["attributes"] assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] == 20 assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS] == 10 @@ -4278,9 +4288,11 @@ def test_openai_message_role_mapping( sentry_sdk.flush() span = next(item.payload for item in items) assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" - assert SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES in span["attributes"] - stored_messages = json.loads(span["attributes"][SPANDATA.GEN_AI_REQUEST_MESSAGES]) + stored_messages = json.loads( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) assert len(stored_messages) == 1 assert stored_messages[0]["role"] == expected_role