Skip to content

Commit 8895734

Browse files
committed
fix output type semconv value and run ruff format
1 parent 55b78b2 commit 8895734

2 files changed

Lines changed: 18 additions & 46 deletions

File tree

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

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ def choice_to_event(choice, capture_content):
164164
if choice.message:
165165
message = {
166166
"role": (
167-
choice.message.role
168-
if choice.message and choice.message.role
169-
else None
167+
choice.message.role if choice.message and choice.message.role else None
170168
)
171169
}
172170
tool_calls = extract_tool_calls(choice.message, capture_content)
@@ -251,14 +249,10 @@ def get_llm_request_attributes(
251249
if operation_name == GenAIAttributes.GenAiOperationNameValues.CHAT.value:
252250
attributes.update(
253251
{
254-
GenAIAttributes.GEN_AI_REQUEST_TEMPERATURE: kwargs.get(
255-
"temperature"
256-
),
252+
GenAIAttributes.GEN_AI_REQUEST_TEMPERATURE: kwargs.get("temperature"),
257253
GenAIAttributes.GEN_AI_REQUEST_TOP_P: kwargs.get("p")
258254
or kwargs.get("top_p"),
259-
GenAIAttributes.GEN_AI_REQUEST_MAX_TOKENS: kwargs.get(
260-
"max_tokens"
261-
),
255+
GenAIAttributes.GEN_AI_REQUEST_MAX_TOKENS: kwargs.get("max_tokens"),
262256
GenAIAttributes.GEN_AI_REQUEST_PRESENCE_PENALTY: kwargs.get(
263257
"presence_penalty"
264258
),
@@ -272,16 +266,12 @@ def get_llm_request_attributes(
272266
if (choice_count := kwargs.get("n")) is not None:
273267
# Only add non default, meaningful values
274268
if isinstance(choice_count, int) and choice_count != 1:
275-
attributes[GenAIAttributes.GEN_AI_REQUEST_CHOICE_COUNT] = (
276-
choice_count
277-
)
269+
attributes[GenAIAttributes.GEN_AI_REQUEST_CHOICE_COUNT] = choice_count
278270

279271
if (stop_sequences := kwargs.get("stop")) is not None:
280272
if isinstance(stop_sequences, str):
281273
stop_sequences = [stop_sequences]
282-
attributes[GenAIAttributes.GEN_AI_REQUEST_STOP_SEQUENCES] = (
283-
stop_sequences
284-
)
274+
attributes[GenAIAttributes.GEN_AI_REQUEST_STOP_SEQUENCES] = stop_sequences
285275

286276
request_response_format_attr_key = (
287277
GenAIAttributes.GEN_AI_OUTPUT_TYPE
@@ -292,7 +282,7 @@ def get_llm_request_attributes(
292282
# response_format may be string, object with a string in the `type` key,
293283
# or a type (e.g. Pydantic model class used with parse())
294284
if isinstance(response_format, type):
295-
attributes[request_response_format_attr_key] = "json_schema"
285+
attributes[request_response_format_attr_key] = "json"
296286
elif isinstance(response_format, Mapping):
297287
if (
298288
response_format_type := response_format.get("type")
@@ -320,10 +310,7 @@ def get_llm_request_attributes(
320310
)
321311

322312
# Add embeddings-specific attributes
323-
elif (
324-
operation_name
325-
== GenAIAttributes.GenAiOperationNameValues.EMBEDDINGS.value
326-
):
313+
elif operation_name == GenAIAttributes.GenAiOperationNameValues.EMBEDDINGS.value:
327314
# Add embedding dimensions if specified
328315
if (dimensions := kwargs.get("dimensions")) is not None:
329316
# TODO: move to GEN_AI_EMBEDDINGS_DIMENSION_COUNT when 1.39.0 is baseline
@@ -378,13 +365,11 @@ def create_chat_invocation(
378365
GenAIAttributes.GEN_AI_REQUEST_CHOICE_COUNT
379366
] = choice_count
380367

381-
if (
382-
response_format := get_value(kwargs.get("response_format"))
383-
) is not None:
368+
if (response_format := get_value(kwargs.get("response_format"))) is not None:
384369
# response_format may be string, object with a string in the `type` key,
385370
# or a type (e.g. Pydantic model class used with parse())
386371
if isinstance(response_format, type):
387-
attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] = "json_schema"
372+
invocation.attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] = "json"
388373
elif isinstance(response_format, Mapping):
389374
if (
390375
response_format_type := get_value(response_format.get("type"))
@@ -426,16 +411,13 @@ def get_value(v: Any):
426411
def handle_span_exception(span, error: BaseException):
427412
span.set_status(Status(StatusCode.ERROR, str(error)))
428413
if span.is_recording():
429-
span.set_attribute(
430-
ErrorAttributes.ERROR_TYPE, type(error).__qualname__
431-
)
414+
span.set_attribute(ErrorAttributes.ERROR_TYPE, type(error).__qualname__)
432415
span.end()
433416

434417

435418
def _is_text_part(content: Any) -> bool:
436419
return isinstance(content, str) or (
437-
isinstance(content, Iterable)
438-
and all(isinstance(part, str) for part in content)
420+
isinstance(content, Iterable) and all(isinstance(part, str) for part in content)
439421
)
440422

441423

@@ -486,9 +468,7 @@ def extract_tool_calls_new(tool_calls) -> list[ToolCallRequest]:
486468
arguments = arguments_str
487469

488470
# TODO: support custom
489-
parts.append(
490-
ToolCallRequest(id=call_id, name=func_name, arguments=arguments)
491-
)
471+
parts.append(ToolCallRequest(id=call_id, name=func_name, arguments=arguments))
492472
return parts
493473

494474

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ 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_schema"
65+
assert spans[0].attributes[output_type_attr_key] == "json"
6666

6767
if latest_experimental_enabled:
6868
assert_messages_attribute(
@@ -71,18 +71,14 @@ def test_structured_output_with_content(
7171
)
7272
assert_messages_attribute(
7373
spans[0].attributes["gen_ai.output.messages"],
74-
format_simple_expected_output_message(
75-
response.choices[0].message.content
76-
),
74+
format_simple_expected_output_message(response.choices[0].message.content),
7775
)
7876
else:
7977
logs = log_exporter.get_finished_logs()
8078
assert len(logs) == 2
8179

8280
user_message = {"content": STRUCTURED_OUTPUT_PROMPT[0]["content"]}
83-
assert_message_in_logs(
84-
logs[0], "gen_ai.user.message", user_message, spans[0]
85-
)
81+
assert_message_in_logs(logs[0], "gen_ai.user.message", user_message, spans[0])
8682

8783
choice_event = {
8884
"index": 0,
@@ -92,9 +88,7 @@ def test_structured_output_with_content(
9288
"content": response.choices[0].message.content,
9389
},
9490
}
95-
assert_message_in_logs(
96-
logs[1], "gen_ai.choice", choice_event, spans[0]
97-
)
91+
assert_message_in_logs(logs[1], "gen_ai.choice", choice_event, spans[0])
9892

9993

10094
def test_structured_output_no_content(
@@ -126,7 +120,7 @@ def test_structured_output_no_content(
126120
if latest_experimental_enabled
127121
else GenAIAttributes.GEN_AI_OPENAI_REQUEST_RESPONSE_FORMAT
128122
)
129-
assert spans[0].attributes[output_type_attr_key] == "json_schema"
123+
assert spans[0].attributes[output_type_attr_key] == "json"
130124

131125
logs = log_exporter.get_finished_logs()
132126
if latest_experimental_enabled:
@@ -143,6 +137,4 @@ def test_structured_output_no_content(
143137
"finish_reason": "stop",
144138
"message": {"role": "assistant"},
145139
}
146-
assert_message_in_logs(
147-
logs[1], "gen_ai.choice", choice_event, spans[0]
148-
)
140+
assert_message_in_logs(logs[1], "gen_ai.choice", choice_event, spans[0])

0 commit comments

Comments
 (0)