|
22 | 22 |
|
23 | 23 | import pytest |
24 | 24 | from anthropic import Anthropic, APIConnectionError, NotFoundError |
| 25 | +from anthropic.resources.messages import Messages as _Messages |
25 | 26 |
|
26 | 27 | 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 |
39 | 28 | from opentelemetry.instrumentation.anthropic.utils import ( |
40 | 29 | MessageWrapper, |
41 | 30 | StreamWrapper, |
|
51 | 40 | ) |
52 | 41 | from opentelemetry.util.genai.types import LLMInvocation |
53 | 42 |
|
| 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 | + |
54 | 49 |
|
55 | 50 | def normalize_stop_reason(stop_reason): |
56 | 51 | """Map Anthropic stop reasons to GenAI semconv values.""" |
@@ -194,7 +189,9 @@ def test_sync_messages_create_captures_content( |
194 | 189 | assert len(spans) == 1 |
195 | 190 | span = spans[0] |
196 | 191 |
|
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 | + ) |
198 | 195 | output_messages = _load_span_messages( |
199 | 196 | span, GenAIAttributes.GEN_AI_OUTPUT_MESSAGES |
200 | 197 | ) |
@@ -473,7 +470,9 @@ def test_sync_messages_create_streaming_captures_content( |
473 | 470 | assert len(spans) == 1 |
474 | 471 | span = spans[0] |
475 | 472 |
|
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 | + ) |
477 | 476 | output_messages = _load_span_messages( |
478 | 477 | span, GenAIAttributes.GEN_AI_OUTPUT_MESSAGES |
479 | 478 | ) |
@@ -597,7 +596,9 @@ def test_sync_messages_stream_captures_content( |
597 | 596 | assert len(spans) == 1 |
598 | 597 | span = spans[0] |
599 | 598 |
|
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 | + ) |
601 | 602 | output_messages = _load_span_messages( |
602 | 603 | span, GenAIAttributes.GEN_AI_OUTPUT_MESSAGES |
603 | 604 | ) |
@@ -687,7 +688,9 @@ def test_sync_messages_create_captures_thinking_content( |
687 | 688 |
|
688 | 689 | @pytest.mark.vcr() |
689 | 690 | 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 |
691 | 694 | ): |
692 | 695 | """Fully consumed stream plus explicit close should still yield one span.""" |
693 | 696 | model = "claude-sonnet-4-20250514" |
|
0 commit comments