diff --git a/sentry_sdk/integrations/anthropic.py b/sentry_sdk/integrations/anthropic.py index c09c0fdb9e..07264f20f8 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, @@ -11,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 ( @@ -531,24 +533,24 @@ 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 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 @@ -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)), ) @@ -607,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, ) @@ -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]]" = [] @@ -734,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, ) @@ -742,36 +748,38 @@ 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( - 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: 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 +807,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 +908,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 +1163,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 +1267,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..306177ecf6 100644 --- a/sentry_sdk/integrations/cohere.py +++ b/sentry_sdk/integrations/cohere.py @@ -2,9 +2,10 @@ 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 from sentry_sdk.traces import Span if TYPE_CHECKING: @@ -44,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, } @@ -105,28 +106,28 @@ 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"): 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 +136,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,12 +158,14 @@ 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, ) 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": @@ -179,8 +183,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, }, ) @@ -198,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: { @@ -217,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 @@ -273,8 +277,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 ( @@ -284,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: @@ -310,12 +316,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..00912e6b48 100644 --- a/sentry_sdk/integrations/google_genai/streaming.py +++ b/sentry_sdk/integrations/google_genai/streaming.py @@ -1,8 +1,9 @@ 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 from sentry_sdk.traces import Span from sentry_sdk.utils import ( safe_serialize, @@ -106,61 +107,61 @@ 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"], ) 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 c9cb1e3f51..87bf0f95d2 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 @@ -26,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, @@ -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") @@ -919,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, ) @@ -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)), ) @@ -952,20 +957,20 @@ 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, ) # 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,58 +991,58 @@ 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) 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) ) @@ -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..283b51b988 100644 --- a/sentry_sdk/integrations/huggingface_hub.py +++ b/sentry_sdk/integrations/huggingface_hub.py @@ -3,9 +3,11 @@ 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 +from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.utils import ( capture_internal_exceptions, @@ -89,30 +91,30 @@ 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"]: - 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(): @@ -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, ) @@ -193,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, ) @@ -204,24 +208,25 @@ 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, ) 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, ) @@ -295,13 +300,13 @@ 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, ) 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, ) @@ -384,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, ) @@ -397,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, ) @@ -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..88f7b0d44f 100644 --- a/sentry_sdk/integrations/litellm.py +++ b/sentry_sdk/integrations/litellm.py @@ -1,13 +1,14 @@ 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 ( 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, @@ -95,20 +96,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 +125,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, ) @@ -135,20 +136,20 @@ 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, ) # 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 +182,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"]: @@ -206,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 @@ -215,11 +216,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 +235,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..656c97cd62 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 ( @@ -38,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 ( @@ -233,22 +234,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 +258,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 +325,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 +349,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 +357,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 +383,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 +398,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 +412,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 +439,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), ) @@ -447,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, ) @@ -461,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, ) @@ -472,35 +473,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 +515,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)), ) @@ -523,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, ) @@ -539,7 +546,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)), ) @@ -552,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, ) @@ -564,11 +571,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 +592,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 +611,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 +626,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: @@ -630,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, @@ -668,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( @@ -733,10 +744,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 +822,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 +907,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,13 +929,15 @@ 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: 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, @@ -959,7 +972,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,13 +994,15 @@ 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: 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, @@ -1031,7 +1046,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,12 +1064,14 @@ 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] 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( @@ -1096,7 +1115,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,13 +1133,15 @@ 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] 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( @@ -1217,9 +1240,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 +1277,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 +1344,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 +1412,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( [ { diff --git a/tests/integrations/anthropic/test_anthropic.py b/tests/integrations/anthropic/test_anthropic.py index 40bcfd55de..7c5ab7e638 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 @@ -65,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, @@ -114,10 +115,12 @@ 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"}], + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: [ + {"role": "user", "content": "Hello, Claude"} + ], } DATA_COLLECTION_INPUT_DATA_KEYS = list(DATA_COLLECTION_EXPECTED_INPUT_DATA.keys()) @@ -344,13 +347,13 @@ 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]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -371,19 +374,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 +436,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_REQUEST_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,13 +506,13 @@ 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]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -526,16 +533,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 +595,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_REQUEST_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,16 +675,18 @@ 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]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -698,19 +707,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 +785,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_REQUEST_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 +862,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] + span["attributes"][ATTRIBUTE_NAMES.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 +943,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] + span["attributes"][ATTRIBUTE_NAMES.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,16 +1035,18 @@ 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]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -1046,19 +1067,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 +1145,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_REQUEST_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 +1226,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] + span["attributes"][ATTRIBUTE_NAMES.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 +1307,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] + span["attributes"][ATTRIBUTE_NAMES.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,13 +1407,13 @@ 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]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -1403,19 +1434,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 +1520,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_REQUEST_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 +1593,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] + span["attributes"][ATTRIBUTE_NAMES.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 +1676,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] + span["attributes"][ATTRIBUTE_NAMES.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,13 +1776,13 @@ 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]) == [ + assert json.loads(span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES]) == [ { "role": "user", "content": "Message demonstrating the absence of truncation.", @@ -1764,16 +1803,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 +1886,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_REQUEST_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 +1960,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] + span["attributes"][ATTRIBUTE_NAMES.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 +2050,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] + span["attributes"][ATTRIBUTE_NAMES.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 +2196,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] + span["attributes"][ATTRIBUTE_NAMES.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 +2336,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_REQUEST_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 +2468,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] + span["attributes"][ATTRIBUTE_NAMES.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 +2608,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_REQUEST_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 +2747,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] + span["attributes"][ATTRIBUTE_NAMES.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 +2894,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_REQUEST_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 +3034,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] + span["attributes"][ATTRIBUTE_NAMES.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 +3182,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_REQUEST_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 +3252,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 +3285,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 +3342,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 +3373,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 +3446,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,13 +3517,15 @@ 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 SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] + 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 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 @@ -3532,22 +3577,24 @@ 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."} ] - 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" @@ -3562,15 +3609,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 +3669,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 SPANDATA.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 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 ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS 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 + 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,22 +3736,24 @@ 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."} ] - 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" @@ -3715,15 +3768,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 +3829,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 SPANDATA.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 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 ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS 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 + 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,22 +3913,24 @@ 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."} ] - 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" @@ -3886,14 +3945,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 +4020,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 SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS 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"][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,21 +4098,23 @@ 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."} ] - 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" @@ -4068,14 +4129,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 +4201,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 SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS 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"][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,22 +4284,24 @@ 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."} ] - 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" @@ -4253,14 +4316,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 +4393,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 SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS 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"][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,22 +4478,24 @@ 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."} ] - 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" @@ -4446,14 +4511,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 +4590,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 SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_OUTPUT_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS 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"][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 +4651,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 @@ -4601,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" @@ -4814,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] == { @@ -4869,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] == { @@ -4923,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] == { @@ -4978,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] == { @@ -5033,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( @@ -5075,11 +5150,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,9 +5204,11 @@ 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"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + 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"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + ) @pytest.mark.skipif( @@ -5172,9 +5254,11 @@ 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"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + 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"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + ) def test_input_tokens_include_cache_write_nonstreaming( @@ -5227,10 +5311,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 +5372,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 +5450,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 +5526,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 +5581,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 +5647,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 +5718,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,9 +5792,11 @@ 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"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + 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"][ATTRIBUTE_NAMES.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + ) @pytest.mark.skipif( @@ -5736,6 +5860,8 @@ 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"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + 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"][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 f4ff3dbd5a..fe4317c25a 100644 --- a/tests/integrations/cohere/test_cohere.py +++ b/tests/integrations/cohere/test_cohere.py @@ -5,9 +5,10 @@ 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 +from sentry_sdk.consts import OP from sentry_sdk.integrations.cohere import CohereIntegration @@ -55,22 +56,22 @@ def test_nonstreaming_chat( assert len(items) == 1 span = items[0].payload - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + 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"]["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"][SPANDATA.AI_MODEL_ID] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + 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"]["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,22 +188,22 @@ def test_streaming_chat( assert len(items) == 1 span = items[0].payload - assert span["attributes"]["sentry.op"] == OP.GEN_AI_CHAT - assert span["attributes"][SPANDATA.AI_MODEL_ID] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + 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"]["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"][SPANDATA.AI_MODEL_ID] == "some-model" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_CHAT + 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"]["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 "hello" in span["attributes"][SPANDATA.AI_INPUT_MESSAGES] + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + assert "hello" in span["attributes"][ATTRIBUTE_NAMES.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 SPANDATA.AI_INPUT_MESSAGES not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GEN_AI_EMBEDDINGS + assert ATTRIBUTE_NAMES.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 @@ -568,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["gen_ai.usage.input_tokens"] == 20 - assert attributes["gen_ai.usage.output_tokens"] == 10 - assert attributes["ai.generation_id"] == "gen-1" + 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[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( @@ -656,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["gen_ai.usage.input_tokens"] == 20 - assert attributes["gen_ai.usage.output_tokens"] == 10 + 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( @@ -717,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["gen_ai.usage.input_tokens"] == 10 + 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 e268be4230..65fca6c9d8 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -5,10 +5,11 @@ 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 -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 @@ -155,13 +156,18 @@ 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]) == [ + assert json.loads( + chat_span["attributes"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] + ) == [ { "role": "user", "content": [ @@ -178,19 +184,25 @@ 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) 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 +246,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"] + assert ATTRIBUTE_NAMES.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 +343,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 == [ @@ -405,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 @@ -461,11 +486,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" ) @@ -607,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": [ @@ -624,29 +651,41 @@ 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?" # 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] # 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 +712,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 +756,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( @@ -778,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 @@ -790,7 +831,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 +870,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 +957,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" ) @@ -969,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) @@ -1042,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 @@ -1131,15 +1180,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 +1203,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 +1247,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 +1316,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 +1398,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 +1425,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 +1472,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 +1495,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 +1541,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 +1613,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 +1698,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 +1729,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 @@ -1689,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" @@ -1731,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" @@ -1782,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" @@ -1838,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" @@ -1902,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" @@ -1956,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" @@ -1996,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" @@ -2046,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"}] @@ -2091,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" @@ -2139,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 @@ -2194,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" @@ -2532,15 +2635,17 @@ 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_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 = { - SPANDATA.GEN_AI_EMBEDDINGS_INPUT: [ + ATTRIBUTE_NAMES.GEN_AI_EMBEDDINGS_INPUT: [ "What is your name?", "What is your favorite color?", ], @@ -2553,22 +2658,22 @@ def __init__(self): pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + 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, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -2609,7 +2714,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 +2728,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( @@ -2634,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", ), ], @@ -2714,7 +2823,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: @@ -2723,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"} @@ -2739,22 +2854,22 @@ def test_generate_content_data_collection_tools( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + 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, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -2830,7 +2945,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 +2959,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( @@ -2852,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", ), ], @@ -2938,7 +3057,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: @@ -2947,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"] @@ -2962,7 +3087,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 +3096,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 +3133,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 +3146,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 @@ -3030,22 +3157,22 @@ def test_embed_content_data_collection( pytest.param( {"gen_ai": {"inputs": True, "outputs": False}}, [ - SPANDATA.GEN_AI_REQUEST_MESSAGES, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + 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, - SPANDATA.GEN_AI_SYSTEM_INSTRUCTIONS, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES, + ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS, ], id="gen-ai-outputs-enabled-inputs-disabled", ), @@ -3084,7 +3211,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 +3225,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 +3236,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 +3245,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 +3282,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 +3295,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..a10c01b87b 100644 --- a/tests/integrations/huggingface_hub/test_huggingface_hub.py +++ b/tests/integrations/huggingface_hub/test_huggingface_hub.py @@ -3,9 +3,9 @@ import pytest from huggingface_hub import InferenceClient +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 @@ -438,39 +438,43 @@ 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" - 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) # 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,39 +507,41 @@ 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 - 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) # 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,40 +576,42 @@ 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" - 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) # 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,40 +645,42 @@ 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 - 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) # 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,37 +719,39 @@ 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( + expected_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] = safe_serialize( [ { "role": "user", @@ -748,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) @@ -788,38 +802,40 @@ 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 - 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) @@ -862,39 +878,41 @@ 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( + expected_data[ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] = safe_serialize( [ { "role": "user", @@ -903,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) @@ -945,40 +963,42 @@ 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 + 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) @@ -1013,30 +1033,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 +1089,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,40 +1149,42 @@ 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", - "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_OPERATION_NAME: "chat", + 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", + 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"] = ( + 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"}]' ) @@ -1212,39 +1236,41 @@ 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 - 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) @@ -1298,44 +1324,48 @@ 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", - "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_OPERATION_NAME: "chat", + 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", + 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"] = ( + 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"}]' ) @@ -1390,42 +1420,44 @@ 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 - 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) @@ -1454,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", ), @@ -1497,12 +1529,16 @@ 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 = { - 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: @@ -1512,10 +1548,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) @@ -1526,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", ), @@ -1570,12 +1606,16 @@ 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 = { - 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: @@ -1585,11 +1625,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) @@ -1600,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", ), @@ -1649,13 +1689,17 @@ 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 = { - 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: @@ -1666,15 +1710,15 @@ 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[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) @@ -1685,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", ), @@ -1738,14 +1782,18 @@ 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 = { - 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: @@ -1755,13 +1803,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..56dca29fa8 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 @@ -37,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, @@ -195,31 +196,31 @@ 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]) == [ + 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"][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 ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.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,31 +367,31 @@ 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]) == [ + 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"][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 ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.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,12 +1738,14 @@ 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] - 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( ( @@ -1833,12 +1836,14 @@ 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] - 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( ( @@ -1929,12 +1934,14 @@ 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] - 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"] @@ -2014,12 +2021,14 @@ 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] - 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"] @@ -2095,12 +2104,14 @@ 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] - 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( ( @@ -2185,12 +2196,14 @@ 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] - 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( ( @@ -2311,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", ), ], @@ -2383,26 +2396,28 @@ 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"] 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" 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..409bbb7ea1 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 @@ -82,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 @@ -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"][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 SPANDATA.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_SYSTEM_INSTRUCTIONS 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"]["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,32 +479,34 @@ 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 ) - 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"]["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 +560,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"][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 SPANDATA.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_SYSTEM_INSTRUCTIONS 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"]["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,32 +708,34 @@ 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 ) - 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"]["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 +844,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 SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS 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 - 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 +951,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 +1018,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 +1089,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 +1179,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,40 +1366,44 @@ 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." - 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 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 +1513,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 SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["attributes"] - assert SPANDATA.GEN_AI_RESPONSE_TEXT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS 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 - 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,40 +1729,44 @@ 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 ) 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 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"][ATTRIBUTE_NAMES.GEN_AI_USAGE_INPUT_TOKENS] + == expected_input_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_TOTAL_TOKENS] == expected_output_tokens + expected_input_tokens ) @@ -1884,14 +1897,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 +2020,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 +2076,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 +2200,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 +2303,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 +2340,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 +2401,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 +2471,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 +2503,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 +2536,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 +2590,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 +2648,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 +2708,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 +2777,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 +2832,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,30 +2874,30 @@ 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 "gen_ai.request.messages" not in spans[0]["attributes"] - assert "gen_ai.response.text" not in spans[0]["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_SYSTEM_INSTRUCTIONS 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") @@ -2921,7 +2946,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 +3158,30 @@ 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, - "gen_ai.request.messages": safe_serialize(expected_request_messages), - "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.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, + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES: safe_serialize( + expected_request_messages + ), + ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL: "gpt-4o", + ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT: "the model response", + 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 +3225,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 +3266,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 +3307,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 +3502,30 @@ 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", - "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, - "gen_ai.response.text": "the model response", - "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_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, + 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.GEN_AI_RESPONSE_TEXT: "the model response", + 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 +3728,40 @@ 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, - "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", - "gen_ai.response.text": "hello world", - "sentry.environment": "production", - "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_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", + 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", + 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", } 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 +3803,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 +3938,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"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '["hello"]' + assert span["attributes"][ATTRIBUTE_NAMES.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 +4008,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 ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.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 +4078,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"][ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES] == '["hello"]' + assert span["attributes"][ATTRIBUTE_NAMES.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 +4148,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 ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES not in span["attributes"] + assert ATTRIBUTE_NAMES.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 +4220,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,10 +4287,12 @@ 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 SPANDATA.GEN_AI_REQUEST_MESSAGES in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "gen_ai.chat" + 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 @@ -4321,11 +4366,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 +4448,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 +4497,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 +4548,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": [