Skip to content

Commit 5148d03

Browse files
committed
Refactor token usage constants and streamline assertions in async message tests
- Simplify the definition of cache read input tokens constant. - Consolidate assertions in async message tests for better readability and maintainability. - Ensure consistent formatting in token usage assertions across test cases.
1 parent ca4f6a8 commit 5148d03

2 files changed

Lines changed: 17 additions & 26 deletions

File tree

instrumentation-genai/opentelemetry-instrumentation-anthropic/src/opentelemetry/instrumentation/anthropic/utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ class MessageRequestParams:
5555
_GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS = (
5656
"gen_ai.usage.cache_creation.input_tokens"
5757
)
58-
_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS = (
59-
"gen_ai.usage.cache_read.input_tokens"
60-
)
58+
_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS = "gen_ai.usage.cache_read.input_tokens"
6159

6260

6361
def _normalize_finish_reason(stop_reason: str | None) -> str | None:
@@ -474,9 +472,7 @@ async def _extract_telemetry_from_stream(self) -> None:
474472
if final_message.id:
475473
self._invocation.response_id = final_message.id
476474

477-
finish_reason = _normalize_finish_reason(
478-
final_message.stop_reason
479-
)
475+
finish_reason = _normalize_finish_reason(final_message.stop_reason)
480476
if finish_reason:
481477
self._invocation.finish_reasons = [finish_reason]
482478

instrumentation-genai/opentelemetry-instrumentation-anthropic/tests/test_async_messages.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import pytest
1818
from anthropic import APIConnectionError, AsyncAnthropic, NotFoundError
19+
1920
from opentelemetry.semconv._incubating.attributes import (
2021
error_attributes as ErrorAttributes,
2122
)
@@ -189,21 +190,18 @@ async def test_async_messages_create_token_usage(
189190
span = spans[0]
190191
assert GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS in span.attributes
191192
assert GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS in span.attributes
192-
assert (
193-
span.attributes[GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS]
194-
== expected_input_tokens(response.usage)
195-
)
193+
assert span.attributes[
194+
GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS
195+
] == expected_input_tokens(response.usage)
196196
assert (
197197
span.attributes[GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS]
198198
== response.usage.output_tokens
199199
)
200-
assert (
201-
span.attributes["gen_ai.usage.cache_creation.input_tokens"]
202-
== (response.usage.cache_creation_input_tokens or 0)
200+
assert span.attributes["gen_ai.usage.cache_creation.input_tokens"] == (
201+
response.usage.cache_creation_input_tokens or 0
203202
)
204-
assert (
205-
span.attributes["gen_ai.usage.cache_read.input_tokens"]
206-
== (response.usage.cache_read_input_tokens or 0)
203+
assert span.attributes["gen_ai.usage.cache_read.input_tokens"] == (
204+
response.usage.cache_read_input_tokens or 0
207205
)
208206

209207

@@ -509,21 +507,18 @@ async def test_async_messages_stream_token_usage(
509507
span = spans[0]
510508
assert GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS in span.attributes
511509
assert GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS in span.attributes
512-
assert (
513-
span.attributes[GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS]
514-
== expected_input_tokens(final_message.usage)
515-
)
510+
assert span.attributes[
511+
GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS
512+
] == expected_input_tokens(final_message.usage)
516513
assert (
517514
span.attributes[GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS]
518515
== final_message.usage.output_tokens
519516
)
520-
assert (
521-
span.attributes["gen_ai.usage.cache_creation.input_tokens"]
522-
== (final_message.usage.cache_creation_input_tokens or 0)
517+
assert span.attributes["gen_ai.usage.cache_creation.input_tokens"] == (
518+
final_message.usage.cache_creation_input_tokens or 0
523519
)
524-
assert (
525-
span.attributes["gen_ai.usage.cache_read.input_tokens"]
526-
== (final_message.usage.cache_read_input_tokens or 0)
520+
assert span.attributes["gen_ai.usage.cache_read.input_tokens"] == (
521+
final_message.usage.cache_read_input_tokens or 0
527522
)
528523

529524

0 commit comments

Comments
 (0)