Skip to content

Commit 2706e1b

Browse files
Fix compatibility with opentelemetry-semantic-conventions-ai 0.4.14
Version 0.4.14 removed several LLM_* attributes from SpanAttributes (LLM_SYSTEM, LLM_REQUEST_MODEL, LLM_RESPONSE_MODEL, LLM_REQUEST_MAX_TOKENS, LLM_REQUEST_TEMPERATURE, LLM_REQUEST_TOP_P, LLM_TOKEN_TYPE). Move these to the OtelAttr enum with their well-known gen_ai.* string values and update all references in observability.py and tests. Fixes microsoft#4160 Co-authored-by: Copilot <[email protected]>
1 parent 1243b77 commit 2706e1b

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

‎python/packages/core/agent_framework/observability.py‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ class OtelAttr(str, Enum):
203203
INPUT_MESSAGES = "gen_ai.input.messages"
204204
OUTPUT_MESSAGES = "gen_ai.output.messages"
205205
SYSTEM_INSTRUCTIONS = "gen_ai.system_instructions"
206+
# Attributes previously from opentelemetry-semantic-conventions-ai SpanAttributes,
207+
# removed in v0.4.14. Defined here for forward compatibility.
208+
SYSTEM = "gen_ai.system"
209+
REQUEST_MAX_TOKENS = "gen_ai.request.max_tokens"
210+
REQUEST_TEMPERATURE = "gen_ai.request.temperature"
211+
REQUEST_TOP_P = "gen_ai.request.top_p"
212+
REQUEST_MODEL = "gen_ai.request.model"
213+
RESPONSE_MODEL = "gen_ai.response.model"
206214

207215
# Workflow attributes
208216
WORKFLOW_ID = "workflow.id"
@@ -1167,7 +1175,7 @@ def get_response(
11671175
# in a different async context than creation — using use_span() would
11681176
# cause "Failed to detach context" errors from OpenTelemetry.
11691177
operation = attributes.get(OtelAttr.OPERATION, "operation")
1170-
span_name = attributes.get(SpanAttributes.LLM_REQUEST_MODEL, "unknown")
1178+
span_name = attributes.get(OtelAttr.REQUEST_MODEL, "unknown")
11711179
span = get_tracer().start_span(f"{operation} {span_name}")
11721180
span.set_attributes(attributes)
11731181
if OBSERVABILITY_SETTINGS.SENSITIVE_DATA_ENABLED and messages:
@@ -1229,7 +1237,7 @@ async def _finalize_stream() -> None:
12291237
return wrapped_stream
12301238

12311239
async def _get_response() -> ChatResponse:
1232-
with _get_span(attributes=attributes, span_name_attribute=SpanAttributes.LLM_REQUEST_MODEL) as span:
1240+
with _get_span(attributes=attributes, span_name_attribute=OtelAttr.REQUEST_MODEL) as span:
12331241
if OBSERVABILITY_SETTINGS.SENSITIVE_DATA_ENABLED and messages:
12341242
_capture_messages(
12351243
span=span,
@@ -1539,16 +1547,16 @@ def _get_instructions_from_options(options: Any) -> str | None:
15391547
OTEL_ATTR_MAP: dict[str | tuple[str, ...], tuple[str, Callable[[Any], Any] | None, bool, Any]] = {
15401548
"choice_count": (OtelAttr.CHOICE_COUNT, None, False, 1),
15411549
"operation_name": (OtelAttr.OPERATION, None, False, None),
1542-
"system_name": (SpanAttributes.LLM_SYSTEM, None, False, None),
1550+
"system_name": (OtelAttr.SYSTEM, None, False, None),
15431551
"provider_name": (OtelAttr.PROVIDER_NAME, None, False, None),
15441552
"service_url": (OtelAttr.ADDRESS, None, False, None),
15451553
"conversation_id": (OtelAttr.CONVERSATION_ID, None, True, None),
15461554
"seed": (OtelAttr.SEED, None, True, None),
15471555
"frequency_penalty": (OtelAttr.FREQUENCY_PENALTY, None, True, None),
1548-
"max_tokens": (SpanAttributes.LLM_REQUEST_MAX_TOKENS, None, True, None),
1556+
"max_tokens": (OtelAttr.REQUEST_MAX_TOKENS, None, True, None),
15491557
"stop": (OtelAttr.STOP_SEQUENCES, None, True, None),
1550-
"temperature": (SpanAttributes.LLM_REQUEST_TEMPERATURE, None, True, None),
1551-
"top_p": (SpanAttributes.LLM_REQUEST_TOP_P, None, True, None),
1558+
"temperature": (OtelAttr.REQUEST_TEMPERATURE, None, True, None),
1559+
"top_p": (OtelAttr.REQUEST_TOP_P, None, True, None),
15521560
"presence_penalty": (OtelAttr.PRESENCE_PENALTY, None, True, None),
15531561
"top_k": (OtelAttr.TOP_K, None, True, None),
15541562
"encoding_formats": (
@@ -1561,7 +1569,7 @@ def _get_instructions_from_options(options: Any) -> str | None:
15611569
"agent_name": (OtelAttr.AGENT_NAME, None, False, None),
15621570
"agent_description": (OtelAttr.AGENT_DESCRIPTION, None, False, None),
15631571
# Multiple source keys - checks model_id in options, then model in kwargs, then model_id in kwargs
1564-
("model_id", "model"): (SpanAttributes.LLM_REQUEST_MODEL, None, True, None),
1572+
("model_id", "model"): (OtelAttr.REQUEST_MODEL, None, True, None),
15651573
# Tools with validation - returns None if no valid tools
15661574
"tools": (
15671575
OtelAttr.TOOL_DEFINITIONS,
@@ -1718,7 +1726,7 @@ def _get_response_attributes(
17181726
if finish_reason:
17191727
attributes[OtelAttr.FINISH_REASONS] = json.dumps([finish_reason])
17201728
if model_id := getattr(response, "model_id", None):
1721-
attributes[SpanAttributes.LLM_RESPONSE_MODEL] = model_id
1729+
attributes[OtelAttr.RESPONSE_MODEL] = model_id
17221730
if capture_usage and (usage := response.usage_details):
17231731
if usage.get("input_token_count"):
17241732
attributes[OtelAttr.INPUT_TOKENS] = usage["input_token_count"]
@@ -1730,8 +1738,8 @@ def _get_response_attributes(
17301738
GEN_AI_METRIC_ATTRIBUTES = (
17311739
OtelAttr.OPERATION,
17321740
OtelAttr.PROVIDER_NAME,
1733-
SpanAttributes.LLM_REQUEST_MODEL,
1734-
SpanAttributes.LLM_RESPONSE_MODEL,
1741+
OtelAttr.REQUEST_MODEL,
1742+
OtelAttr.RESPONSE_MODEL,
17351743
OtelAttr.ADDRESS,
17361744
OtelAttr.PORT,
17371745
)
@@ -1749,10 +1757,10 @@ def _capture_response(
17491757
attrs: dict[str, Any] = {k: v for k, v in attributes.items() if k in GEN_AI_METRIC_ATTRIBUTES}
17501758
if token_usage_histogram and (input_tokens := attributes.get(OtelAttr.INPUT_TOKENS)):
17511759
token_usage_histogram.record(
1752-
input_tokens, attributes={**attrs, SpanAttributes.LLM_TOKEN_TYPE: OtelAttr.T_TYPE_INPUT}
1760+
input_tokens, attributes={**attrs, OtelAttr.T_TYPE: OtelAttr.T_TYPE_INPUT}
17531761
)
17541762
if token_usage_histogram and (output_tokens := attributes.get(OtelAttr.OUTPUT_TOKENS)):
1755-
token_usage_histogram.record(output_tokens, {**attrs, SpanAttributes.LLM_TOKEN_TYPE: OtelAttr.T_TYPE_OUTPUT})
1763+
token_usage_histogram.record(output_tokens, {**attrs, OtelAttr.T_TYPE: OtelAttr.T_TYPE_OUTPUT})
17561764
if operation_duration_histogram and duration is not None:
17571765
if OtelAttr.ERROR_TYPE in attributes:
17581766
attrs[OtelAttr.ERROR_TYPE] = attributes[OtelAttr.ERROR_TYPE]

‎python/packages/core/tests/core/test_observability.py‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import pytest
99
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
10-
from opentelemetry.semconv_ai import SpanAttributes
1110
from opentelemetry.trace import StatusCode
1211

1312
from agent_framework import (
@@ -48,8 +47,8 @@ def test_role_event_map():
4847
def test_enum_values():
4948
"""Test that OtelAttr enum has expected values."""
5049
assert OtelAttr.OPERATION == "gen_ai.operation.name"
51-
assert SpanAttributes.LLM_SYSTEM == "gen_ai.system"
52-
assert SpanAttributes.LLM_REQUEST_MODEL == "gen_ai.request.model"
50+
assert OtelAttr.SYSTEM == "gen_ai.system"
51+
assert OtelAttr.REQUEST_MODEL == "gen_ai.request.model"
5352
assert OtelAttr.CHAT_COMPLETION_OPERATION == "chat"
5453
assert OtelAttr.TOOL_EXECUTION_OPERATION == "execute_tool"
5554
assert OtelAttr.AGENT_INVOKE_OPERATION == "invoke_agent"
@@ -213,7 +212,7 @@ async def test_chat_client_observability(mock_chat_client, span_exporter: InMemo
213212
span = spans[0]
214213
assert span.name == "chat Test"
215214
assert span.attributes[OtelAttr.OPERATION.value] == OtelAttr.CHAT_COMPLETION_OPERATION
216-
assert span.attributes[SpanAttributes.LLM_REQUEST_MODEL] == "Test"
215+
assert span.attributes[OtelAttr.REQUEST_MODEL] == "Test"
217216
assert span.attributes[OtelAttr.INPUT_TOKENS] == 10
218217
assert span.attributes[OtelAttr.OUTPUT_TOKENS] == 20
219218
if enable_sensitive_data:
@@ -243,7 +242,7 @@ async def test_chat_client_streaming_observability(
243242
span = spans[0]
244243
assert span.name == "chat Test"
245244
assert span.attributes[OtelAttr.OPERATION.value] == OtelAttr.CHAT_COMPLETION_OPERATION
246-
assert span.attributes[SpanAttributes.LLM_REQUEST_MODEL] == "Test"
245+
assert span.attributes[OtelAttr.REQUEST_MODEL] == "Test"
247246
if enable_sensitive_data:
248247
assert span.attributes[OtelAttr.INPUT_MESSAGES] is not None
249248
assert span.attributes[OtelAttr.OUTPUT_MESSAGES] is not None
@@ -392,7 +391,7 @@ async def test_chat_client_without_model_id_observability(mock_chat_client, span
392391

393392
assert span.name == "chat unknown"
394393
assert span.attributes[OtelAttr.OPERATION.value] == OtelAttr.CHAT_COMPLETION_OPERATION
395-
assert span.attributes[SpanAttributes.LLM_REQUEST_MODEL] == "unknown"
394+
assert span.attributes[OtelAttr.REQUEST_MODEL] == "unknown"
396395

397396

398397
async def test_chat_client_streaming_without_model_id_observability(
@@ -416,7 +415,7 @@ async def test_chat_client_streaming_without_model_id_observability(
416415
span = spans[0]
417416
assert span.name == "chat unknown"
418417
assert span.attributes[OtelAttr.OPERATION.value] == OtelAttr.CHAT_COMPLETION_OPERATION
419-
assert span.attributes[SpanAttributes.LLM_REQUEST_MODEL] == "unknown"
418+
assert span.attributes[OtelAttr.REQUEST_MODEL] == "unknown"
420419

421420

422421
def test_prepend_user_agent_with_none_value():
@@ -491,7 +490,7 @@ async def test_agent_instrumentation_enabled(
491490
assert span.attributes[OtelAttr.AGENT_ID] == "test_agent_id"
492491
assert span.attributes[OtelAttr.AGENT_NAME] == "test_agent"
493492
assert span.attributes[OtelAttr.AGENT_DESCRIPTION] == "Test agent description"
494-
assert span.attributes[SpanAttributes.LLM_REQUEST_MODEL] == "TestModel"
493+
assert span.attributes[OtelAttr.REQUEST_MODEL] == "TestModel"
495494
assert span.attributes[OtelAttr.INPUT_TOKENS] == 15
496495
assert span.attributes[OtelAttr.OUTPUT_TOKENS] == 25
497496
if enable_sensitive_data:
@@ -521,7 +520,7 @@ async def test_agent_streaming_response_with_diagnostics_enabled(
521520
assert span.attributes[OtelAttr.AGENT_ID] == "test_agent_id"
522521
assert span.attributes[OtelAttr.AGENT_NAME] == "test_agent"
523522
assert span.attributes[OtelAttr.AGENT_DESCRIPTION] == "Test agent description"
524-
assert span.attributes[SpanAttributes.LLM_REQUEST_MODEL] == "TestModel"
523+
assert span.attributes[OtelAttr.REQUEST_MODEL] == "TestModel"
525524
if enable_sensitive_data:
526525
assert span.attributes.get(OtelAttr.OUTPUT_MESSAGES) is not None # Streaming, so no usage yet
527526

@@ -1381,8 +1380,6 @@ def test_get_response_attributes_with_model_id():
13811380
"""Test _get_response_attributes includes model_id."""
13821381
from unittest.mock import Mock
13831382

1384-
from opentelemetry.semconv_ai import SpanAttributes
1385-
13861383
from agent_framework.observability import _get_response_attributes
13871384

13881385
response = Mock()
@@ -1395,7 +1392,7 @@ def test_get_response_attributes_with_model_id():
13951392
attrs = {}
13961393
result = _get_response_attributes(attrs, response)
13971394

1398-
assert result[SpanAttributes.LLM_RESPONSE_MODEL] == "gpt-4"
1395+
assert result[OtelAttr.RESPONSE_MODEL] == "gpt-4"
13991396

14001397

14011398
def test_get_response_attributes_with_usage():

0 commit comments

Comments
 (0)