Skip to content

Commit ecc3774

Browse files
WIP: fixing the precommit stuff.
1 parent 0c277ac commit ecc3774

4 files changed

Lines changed: 59 additions & 30 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
OutputMessage,
6363
Reasoning,
6464
Text,
65+
)
66+
from opentelemetry.util.genai.types import (
6567
ToolCallRequest as ToolCall,
6668
)
6769
except ImportError:
@@ -511,7 +513,9 @@ def _get_request_attributes(
511513
if port is not None:
512514
attributes[ServerAttributes.SERVER_PORT] = port
513515

514-
return {key: value for key, value in attributes.items() if value_is_set(value)}
516+
return {
517+
key: value for key, value in attributes.items() if value_is_set(value)
518+
}
515519

516520

517521
def _get_inference_creation_kwargs(
@@ -554,9 +558,7 @@ def _apply_request_attributes(
554558

555559
output_type = _extract_output_type(kwargs)
556560
if output_type is not None:
557-
invocation.attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] = (
558-
output_type
559-
)
561+
invocation.attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] = output_type
560562

561563
if capture_content:
562564
invocation.system_instruction = _extract_system_instruction(kwargs)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,7 @@ def instrument_with_content_unsampled(
227227

228228

229229
@pytest.fixture(scope="function")
230-
def instrument_event_only(
231-
tracer_provider, logger_provider, meter_provider
232-
):
230+
def instrument_event_only(tracer_provider, logger_provider, meter_provider):
233231
_OpenTelemetrySemanticConventionStability._initialized = False
234232

235233
os.environ.update(

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

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ def _assert_response_content(span, response, log_exporter):
9898
span.attributes[GenAIAttributes.GEN_AI_INPUT_MESSAGES],
9999
USER_ONLY_EXPECTED_INPUT_MESSAGES,
100100
)
101-
assert json.loads(
102-
span.attributes[GenAIAttributes.GEN_AI_SYSTEM_INSTRUCTIONS]
103-
) == EXPECTED_SYSTEM_INSTRUCTIONS
101+
assert (
102+
json.loads(span.attributes[GenAIAttributes.GEN_AI_SYSTEM_INSTRUCTIONS])
103+
== EXPECTED_SYSTEM_INSTRUCTIONS
104+
)
104105
assert_messages_attribute(
105106
span.attributes[GenAIAttributes.GEN_AI_OUTPUT_MESSAGES],
106107
format_simple_expected_output_message(response.output_text),
@@ -129,7 +130,9 @@ def _assert_request_attrs(
129130
== max_tokens
130131
)
131132
if output_type is not None:
132-
assert span.attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] == output_type
133+
assert (
134+
span.attributes[GenAIAttributes.GEN_AI_OUTPUT_TYPE] == output_type
135+
)
133136

134137

135138
def _collect_completed_response(stream):
@@ -206,17 +209,20 @@ def test_responses_create_basic(
206209
response.usage.output_tokens,
207210
response_service_tier=getattr(response, "service_tier", None),
208211
)
209-
assert (
210-
span.attributes[GenAIAttributes.GEN_AI_RESPONSE_FINISH_REASONS]
211-
== ("stop",)
212+
assert span.attributes[GenAIAttributes.GEN_AI_RESPONSE_FINISH_REASONS] == (
213+
"stop",
212214
)
213215
assert GenAIAttributes.GEN_AI_INPUT_MESSAGES not in span.attributes
214216
assert GenAIAttributes.GEN_AI_OUTPUT_MESSAGES not in span.attributes
215217

216218

217219
@pytest.mark.vcr()
218220
def test_responses_create_captures_content(
219-
request, span_exporter, log_exporter, openai_client, instrument_with_content
221+
request,
222+
span_exporter,
223+
log_exporter,
224+
openai_client,
225+
instrument_with_content,
220226
):
221227
_skip_if_not_latest()
222228

@@ -327,7 +333,9 @@ def test_responses_create_stop_reason(
327333
)
328334

329335

330-
def test_responses_create_connection_error(span_exporter, instrument_no_content):
336+
def test_responses_create_connection_error(
337+
span_exporter, instrument_no_content
338+
):
331339
_skip_if_not_latest()
332340

333341
client = OpenAI(base_url="http://localhost:4242")
@@ -340,7 +348,9 @@ def test_responses_create_connection_error(span_exporter, instrument_no_content)
340348
)
341349

342350
(span,) = span_exporter.get_finished_spans()
343-
assert span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
351+
assert (
352+
span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
353+
)
344354
assert span.attributes[ServerAttributes.SERVER_ADDRESS] == "localhost"
345355
assert span.attributes[ServerAttributes.SERVER_PORT] == 4242
346356
assert span.attributes[ErrorAttributes.ERROR_TYPE] == "APIConnectionError"
@@ -359,7 +369,9 @@ def test_responses_create_api_error(
359369
)
360370

361371
(span,) = span_exporter.get_finished_spans()
362-
assert span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == INVALID_MODEL
372+
assert (
373+
span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == INVALID_MODEL
374+
)
363375
assert (
364376
span.attributes[ErrorAttributes.ERROR_TYPE]
365377
== type(exc_info.value).__name__
@@ -415,7 +427,11 @@ def test_responses_create_streaming_aggregates_cache_tokens(
415427

416428
@pytest.mark.vcr()
417429
def test_responses_create_streaming_captures_content(
418-
request, span_exporter, log_exporter, openai_client, instrument_with_content
430+
request,
431+
span_exporter,
432+
log_exporter,
433+
openai_client,
434+
instrument_with_content,
419435
):
420436
_skip_if_not_latest()
421437

@@ -448,7 +464,9 @@ def test_responses_create_streaming_iteration(
448464
assert len(events) > 0
449465

450466
(span,) = span_exporter.get_finished_spans()
451-
assert span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
467+
assert (
468+
span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
469+
)
452470
assert GenAIAttributes.GEN_AI_RESPONSE_ID in span.attributes
453471
assert GenAIAttributes.GEN_AI_RESPONSE_MODEL in span.attributes
454472

@@ -488,7 +506,9 @@ def test_responses_create_streaming_connection_error(
488506
)
489507

490508
(span,) = span_exporter.get_finished_spans()
491-
assert span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
509+
assert (
510+
span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
511+
)
492512
assert span.attributes[ErrorAttributes.ERROR_TYPE] == "APIConnectionError"
493513

494514

@@ -559,13 +579,17 @@ def __getattr__(self, name):
559579
stream, "stream", ErrorInjectingStreamDelegate(stream.stream)
560580
)
561581

562-
with pytest.raises(ConnectionError, match="connection reset during stream"):
582+
with pytest.raises(
583+
ConnectionError, match="connection reset during stream"
584+
):
563585
with stream:
564586
for _ in stream:
565587
pass
566588

567589
(span,) = span_exporter.get_finished_spans()
568-
assert span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
590+
assert (
591+
span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
592+
)
569593
assert span.attributes[ErrorAttributes.ERROR_TYPE] == "ConnectionError"
570594

571595

@@ -586,7 +610,9 @@ def test_responses_create_streaming_user_exception(
586610
raise ValueError("User raised exception")
587611

588612
(span,) = span_exporter.get_finished_spans()
589-
assert span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
613+
assert (
614+
span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
615+
)
590616
assert span.attributes[ErrorAttributes.ERROR_TYPE] == "ValueError"
591617

592618

@@ -616,7 +642,9 @@ def exploding_process_event(self, event):
616642
assert len(events) > 0
617643

618644
(span,) = span_exporter.get_finished_spans()
619-
assert span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
645+
assert (
646+
span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == DEFAULT_MODEL
647+
)
620648
assert ErrorAttributes.ERROR_TYPE not in span.attributes
621649

622650

@@ -713,7 +741,11 @@ def test_responses_create_with_content_span_unsampled(
713741

714742
@pytest.mark.vcr()
715743
def test_responses_create_with_content_shapes(
716-
request, span_exporter, log_exporter, openai_client, instrument_with_content
744+
request,
745+
span_exporter,
746+
log_exporter,
747+
openai_client,
748+
instrument_with_content,
717749
):
718750
_skip_if_not_latest()
719751

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,7 @@ def assert_cache_attributes(span, usage):
289289

290290
cache_creation = getattr(details, "cache_creation_input_tokens", None)
291291
if cache_creation is None:
292-
assert (
293-
GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS
294-
not in span.attributes
295-
)
292+
assert GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS not in span.attributes
296293
else:
297294
assert (
298295
span.attributes[GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS]

0 commit comments

Comments
 (0)