|
3 | 3 |
|
4 | 4 | """Tests for Anthropic message parameter extraction.""" |
5 | 5 |
|
| 6 | +import pytest |
| 7 | + |
6 | 8 | from opentelemetry.instrumentation.genai.anthropic.messages_extractors import ( |
7 | 9 | extract_params, |
| 10 | + get_tool_definitions, |
| 11 | +) |
| 12 | +from opentelemetry.util.genai.types import ( |
| 13 | + FunctionToolDefinition, |
| 14 | + GenericToolDefinition, |
8 | 15 | ) |
9 | 16 |
|
10 | 17 |
|
@@ -47,3 +54,115 @@ def test_extract_params_ignores_non_mapping_extra_body(): |
47 | 54 | assert params.temperature is None |
48 | 55 | assert params.top_p is None |
49 | 56 | assert params.top_k is None |
| 57 | + |
| 58 | + |
| 59 | +def test_extract_params_keeps_tools(): |
| 60 | + tools = [{"name": "get_weather", "input_schema": {"type": "object"}}] |
| 61 | + |
| 62 | + params = extract_params(tools=tools) |
| 63 | + |
| 64 | + assert params.tools is tools |
| 65 | + |
| 66 | + |
| 67 | +def test_get_tool_definitions_maps_custom_tool_to_function(): |
| 68 | + schema = { |
| 69 | + "type": "object", |
| 70 | + "properties": {"city": {"type": "string"}}, |
| 71 | + "required": ["city"], |
| 72 | + } |
| 73 | + |
| 74 | + definitions = get_tool_definitions( |
| 75 | + [ |
| 76 | + { |
| 77 | + "name": "get_weather", |
| 78 | + "description": "Get weather by city", |
| 79 | + "input_schema": schema, |
| 80 | + } |
| 81 | + ] |
| 82 | + ) |
| 83 | + |
| 84 | + assert definitions == [ |
| 85 | + FunctionToolDefinition( |
| 86 | + name="get_weather", |
| 87 | + description="Get weather by city", |
| 88 | + parameters=schema, |
| 89 | + ) |
| 90 | + ] |
| 91 | + assert definitions[0].type == "function" |
| 92 | + |
| 93 | + |
| 94 | +def test_get_tool_definitions_maps_explicit_custom_type_to_function(): |
| 95 | + definitions = get_tool_definitions([{"type": "custom", "name": "noop"}]) |
| 96 | + |
| 97 | + assert definitions == [ |
| 98 | + FunctionToolDefinition(name="noop", description=None, parameters=None) |
| 99 | + ] |
| 100 | + |
| 101 | + |
| 102 | +def test_get_tool_definitions_maps_server_tool_to_generic(): |
| 103 | + definitions = get_tool_definitions( |
| 104 | + [ |
| 105 | + { |
| 106 | + "name": "web_search", |
| 107 | + "type": "web_search_20250305", |
| 108 | + "max_uses": 3, |
| 109 | + } |
| 110 | + ] |
| 111 | + ) |
| 112 | + |
| 113 | + assert definitions == [ |
| 114 | + GenericToolDefinition(name="web_search", type="web_search_20250305") |
| 115 | + ] |
| 116 | + |
| 117 | + |
| 118 | +def test_get_tool_definitions_falls_back_to_type_for_unnamed_toolset(): |
| 119 | + definitions = get_tool_definitions( |
| 120 | + [{"type": "computer_toolset_20260801", "configs": []}] |
| 121 | + ) |
| 122 | + |
| 123 | + assert definitions == [ |
| 124 | + GenericToolDefinition( |
| 125 | + name="computer_toolset_20260801", |
| 126 | + type="computer_toolset_20260801", |
| 127 | + ) |
| 128 | + ] |
| 129 | + |
| 130 | + |
| 131 | +def test_get_tool_definitions_reads_tool_objects(): |
| 132 | + class _Tool: |
| 133 | + name = "get_weather" |
| 134 | + description = "Get weather by city" |
| 135 | + input_schema = {"type": "object"} |
| 136 | + |
| 137 | + definitions = get_tool_definitions([_Tool()]) |
| 138 | + |
| 139 | + assert definitions == [ |
| 140 | + FunctionToolDefinition( |
| 141 | + name="get_weather", |
| 142 | + description="Get weather by city", |
| 143 | + parameters={"type": "object"}, |
| 144 | + ) |
| 145 | + ] |
| 146 | + |
| 147 | + |
| 148 | +def test_get_tool_definitions_mixes_custom_and_server_tools(): |
| 149 | + definitions = get_tool_definitions( |
| 150 | + [ |
| 151 | + {"name": "get_weather", "input_schema": {"type": "object"}}, |
| 152 | + {"name": "bash", "type": "bash_20250124"}, |
| 153 | + ] |
| 154 | + ) |
| 155 | + |
| 156 | + assert definitions == [ |
| 157 | + FunctionToolDefinition( |
| 158 | + name="get_weather", |
| 159 | + description=None, |
| 160 | + parameters={"type": "object"}, |
| 161 | + ), |
| 162 | + GenericToolDefinition(name="bash", type="bash_20250124"), |
| 163 | + ] |
| 164 | + |
| 165 | + |
| 166 | +@pytest.mark.parametrize("tools", [None, []]) |
| 167 | +def test_get_tool_definitions_without_tools(tools): |
| 168 | + assert get_tool_definitions(tools) is None |
0 commit comments