Skip to content

Commit 8047f9e

Browse files
committed
use semconv constants for output type values
1 parent 8895734 commit 8047f9e

3 files changed

Lines changed: 23 additions & 18 deletions

File tree

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,14 @@ def get_llm_request_attributes(
282282
# response_format may be string, object with a string in the `type` key,
283283
# or a type (e.g. Pydantic model class used with parse())
284284
if isinstance(response_format, type):
285-
attributes[request_response_format_attr_key] = "json"
285+
if latest_experimental_enabled:
286+
attributes[request_response_format_attr_key] = (
287+
GenAIAttributes.GenAiOutputTypeValues.JSON.value
288+
)
289+
else:
290+
attributes[request_response_format_attr_key] = (
291+
GenAIAttributes.GenAiOpenaiRequestResponseFormatValues.JSON_SCHEMA.value
292+
)
286293
elif isinstance(response_format, Mapping):
287294
if (
288295
response_format_type := response_format.get("type")
@@ -369,7 +376,9 @@ def create_chat_invocation(
369376
# response_format may be string, object with a string in the `type` key,
370377
# or a type (e.g. Pydantic model class used with parse())
371378
if isinstance(response_format, type):
372-
invocation.attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] = "json"
379+
invocation.attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] = (
380+
GenAIAttributes.GenAiOutputTypeValues.JSON.value
381+
)
373382
elif isinstance(response_format, Mapping):
374383
if (
375384
response_format_type := get_value(response_format.get("type"))

instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_async_structured_outputs.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ async def test_async_structured_output_with_content(
6969
if latest_experimental_enabled
7070
else GenAIAttributes.GEN_AI_OPENAI_REQUEST_RESPONSE_FORMAT
7171
)
72-
assert spans[0].attributes[output_type_attr_key] == "json_schema"
72+
expected_value = "json" if latest_experimental_enabled else "json_schema"
73+
assert spans[0].attributes[output_type_attr_key] == expected_value
7374

7475
if latest_experimental_enabled:
7576
assert_messages_attribute(
@@ -78,18 +79,14 @@ async def test_async_structured_output_with_content(
7879
)
7980
assert_messages_attribute(
8081
spans[0].attributes["gen_ai.output.messages"],
81-
format_simple_expected_output_message(
82-
response.choices[0].message.content
83-
),
82+
format_simple_expected_output_message(response.choices[0].message.content),
8483
)
8584
else:
8685
logs = log_exporter.get_finished_logs()
8786
assert len(logs) == 2
8887

8988
user_message = {"content": STRUCTURED_OUTPUT_PROMPT[0]["content"]}
90-
assert_message_in_logs(
91-
logs[0], "gen_ai.user.message", user_message, spans[0]
92-
)
89+
assert_message_in_logs(logs[0], "gen_ai.user.message", user_message, spans[0])
9390

9491
choice_event = {
9592
"index": 0,
@@ -99,9 +96,7 @@ async def test_async_structured_output_with_content(
9996
"content": response.choices[0].message.content,
10097
},
10198
}
102-
assert_message_in_logs(
103-
logs[1], "gen_ai.choice", choice_event, spans[0]
104-
)
99+
assert_message_in_logs(logs[1], "gen_ai.choice", choice_event, spans[0])
105100

106101

107102
@pytest.mark.asyncio()
@@ -138,7 +133,8 @@ async def test_async_structured_output_no_content(
138133
if latest_experimental_enabled
139134
else GenAIAttributes.GEN_AI_OPENAI_REQUEST_RESPONSE_FORMAT
140135
)
141-
assert spans[0].attributes[output_type_attr_key] == "json_schema"
136+
expected_value = "json" if latest_experimental_enabled else "json_schema"
137+
assert spans[0].attributes[output_type_attr_key] == expected_value
142138

143139
logs = log_exporter.get_finished_logs()
144140
if latest_experimental_enabled:
@@ -155,6 +151,4 @@ async def test_async_structured_output_no_content(
155151
"finish_reason": "stop",
156152
"message": {"role": "assistant"},
157153
}
158-
assert_message_in_logs(
159-
logs[1], "gen_ai.choice", choice_event, spans[0]
160-
)
154+
assert_message_in_logs(logs[1], "gen_ai.choice", choice_event, spans[0])

instrumentation-genai/opentelemetry-instrumentation-openai-v2/tests/test_structured_outputs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def test_structured_output_with_content(
6262
if latest_experimental_enabled
6363
else GenAIAttributes.GEN_AI_OPENAI_REQUEST_RESPONSE_FORMAT
6464
)
65-
assert spans[0].attributes[output_type_attr_key] == "json"
65+
expected_value = "json" if latest_experimental_enabled else "json_schema"
66+
assert spans[0].attributes[output_type_attr_key] == expected_value
6667

6768
if latest_experimental_enabled:
6869
assert_messages_attribute(
@@ -120,7 +121,8 @@ def test_structured_output_no_content(
120121
if latest_experimental_enabled
121122
else GenAIAttributes.GEN_AI_OPENAI_REQUEST_RESPONSE_FORMAT
122123
)
123-
assert spans[0].attributes[output_type_attr_key] == "json"
124+
expected_value = "json" if latest_experimental_enabled else "json_schema"
125+
assert spans[0].attributes[output_type_attr_key] == expected_value
124126

125127
logs = log_exporter.get_finished_logs()
126128
if latest_experimental_enabled:

0 commit comments

Comments
 (0)