Skip to content

Commit c120c0e

Browse files
committed
use semconv constants for output type values
1 parent 0ba0244 commit c120c0e

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
@@ -268,7 +268,14 @@ def get_llm_request_attributes(
268268
# response_format may be string, object with a string in the `type` key,
269269
# or a type (e.g. Pydantic model class used with parse())
270270
if isinstance(response_format, type):
271-
attributes[request_response_format_attr_key] = "json"
271+
if latest_experimental_enabled:
272+
attributes[request_response_format_attr_key] = (
273+
GenAIAttributes.GenAiOutputTypeValues.JSON.value
274+
)
275+
else:
276+
attributes[request_response_format_attr_key] = (
277+
GenAIAttributes.GenAiOpenaiRequestResponseFormatValues.JSON_SCHEMA.value
278+
)
272279
elif isinstance(response_format, Mapping):
273280
if (response_format_type := response_format.get("type")) is not None:
274281
attributes[request_response_format_attr_key] = response_format_type
@@ -350,7 +357,9 @@ def create_chat_invocation(
350357
# response_format may be string, object with a string in the `type` key,
351358
# or a type (e.g. Pydantic model class used with parse())
352359
if isinstance(response_format, type):
353-
attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] = "json"
360+
attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] = (
361+
GenAIAttributes.GenAiOutputTypeValues.JSON.value
362+
)
354363
elif isinstance(response_format, Mapping):
355364
if (
356365
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)