Skip to content

Commit d48c7e3

Browse files
committed
Refactor tests for sync message handling in Anthropic instrumentation
- Simplified detection of 'tools' and 'thinking' parameters by directly accessing the _Messages class. - Improved readability of test cases by formatting input message loading. - Enhanced test function signatures for better clarity and maintainability.
1 parent 8b5b20f commit d48c7e3

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,9 @@
2222

2323
import pytest
2424
from anthropic import Anthropic, APIConnectionError, NotFoundError
25+
from anthropic.resources.messages import Messages as _Messages
2526

2627
from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
27-
28-
# Detect whether the installed anthropic SDK supports tools / thinking params.
29-
# Older SDK versions (e.g. 0.16.0) do not accept these keyword arguments.
30-
try:
31-
from anthropic.resources.messages import Messages as _Messages
32-
33-
_create_params = set(inspect.signature(_Messages.create).parameters)
34-
except Exception: # pylint: disable=broad-except
35-
_create_params = set()
36-
37-
_has_tools_param = "tools" in _create_params
38-
_has_thinking_param = "thinking" in _create_params
3928
from opentelemetry.instrumentation.anthropic.utils import (
4029
MessageWrapper,
4130
StreamWrapper,
@@ -51,6 +40,12 @@
5140
)
5241
from opentelemetry.util.genai.types import LLMInvocation
5342

43+
# Detect whether the installed anthropic SDK supports tools / thinking params.
44+
# Older SDK versions (e.g. 0.16.0) do not accept these keyword arguments.
45+
_create_params = set(inspect.signature(_Messages.create).parameters)
46+
_has_tools_param = "tools" in _create_params
47+
_has_thinking_param = "thinking" in _create_params
48+
5449

5550
def normalize_stop_reason(stop_reason):
5651
"""Map Anthropic stop reasons to GenAI semconv values."""
@@ -194,7 +189,9 @@ def test_sync_messages_create_captures_content(
194189
assert len(spans) == 1
195190
span = spans[0]
196191

197-
input_messages = _load_span_messages(span, GenAIAttributes.GEN_AI_INPUT_MESSAGES)
192+
input_messages = _load_span_messages(
193+
span, GenAIAttributes.GEN_AI_INPUT_MESSAGES
194+
)
198195
output_messages = _load_span_messages(
199196
span, GenAIAttributes.GEN_AI_OUTPUT_MESSAGES
200197
)
@@ -473,7 +470,9 @@ def test_sync_messages_create_streaming_captures_content(
473470
assert len(spans) == 1
474471
span = spans[0]
475472

476-
input_messages = _load_span_messages(span, GenAIAttributes.GEN_AI_INPUT_MESSAGES)
473+
input_messages = _load_span_messages(
474+
span, GenAIAttributes.GEN_AI_INPUT_MESSAGES
475+
)
477476
output_messages = _load_span_messages(
478477
span, GenAIAttributes.GEN_AI_OUTPUT_MESSAGES
479478
)
@@ -597,7 +596,9 @@ def test_sync_messages_stream_captures_content(
597596
assert len(spans) == 1
598597
span = spans[0]
599598

600-
input_messages = _load_span_messages(span, GenAIAttributes.GEN_AI_INPUT_MESSAGES)
599+
input_messages = _load_span_messages(
600+
span, GenAIAttributes.GEN_AI_INPUT_MESSAGES
601+
)
601602
output_messages = _load_span_messages(
602603
span, GenAIAttributes.GEN_AI_OUTPUT_MESSAGES
603604
)
@@ -687,7 +688,9 @@ def test_sync_messages_create_captures_thinking_content(
687688

688689
@pytest.mark.vcr()
689690
def test_stream_wrapper_finalize_idempotent(
690-
span_exporter, anthropic_client, instrument_no_content # pylint: disable=too-many-locals
691+
span_exporter,
692+
anthropic_client,
693+
instrument_no_content, # pylint: disable=too-many-locals
691694
):
692695
"""Fully consumed stream plus explicit close should still yield one span."""
693696
model = "claude-sonnet-4-20250514"

0 commit comments

Comments
 (0)