-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathtest_anthropic_model_ext.py
More file actions
522 lines (425 loc) · 19.9 KB
/
Copy pathtest_anthropic_model_ext.py
File metadata and controls
522 lines (425 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
"""Extended tests for Anthropic model — covers helper methods, edge cases, and streaming paths."""
import base64
import json
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from anthropic import types as anthropic_types
from google.genai.types import Blob, CodeExecutionResult, ExecutableCode
from trpc_agent_sdk.models import AnthropicModel, LlmRequest, LlmResponse
from trpc_agent_sdk.models._anthropic_model import _FinishReason
from trpc_agent_sdk.types import (
Content,
FunctionDeclaration,
GenerateContentConfig,
Part,
Schema,
Tool,
Type,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _model(**kwargs):
defaults = dict(model_name="claude-3-5-sonnet-20241022", api_key="test-key")
defaults.update(kwargs)
return AnthropicModel(**defaults)
# ---------------------------------------------------------------------------
# _to_claude_role
# ---------------------------------------------------------------------------
class TestToClaudeRole:
def test_model_role_maps_to_assistant(self):
"""'model' role is converted to 'assistant'."""
model = _model()
assert model._to_claude_role("model") == "assistant"
def test_assistant_role_stays_assistant(self):
"""'assistant' role stays 'assistant'."""
model = _model()
assert model._to_claude_role("assistant") == "assistant"
def test_user_role_stays_user(self):
"""'user' role stays 'user'."""
model = _model()
assert model._to_claude_role("user") == "user"
def test_none_role_defaults_to_user(self):
"""None role defaults to 'user'."""
model = _model()
assert model._to_claude_role(None) == "user"
def test_unknown_role_defaults_to_user(self):
"""Unknown role defaults to 'user'."""
model = _model()
assert model._to_claude_role("system") == "user"
# ---------------------------------------------------------------------------
# _is_image_part
# ---------------------------------------------------------------------------
class TestIsImagePart:
def test_image_part_detected(self):
"""Image inline data is recognised as image."""
model = _model()
part = Part(inline_data=Blob(mime_type="image/png", data=b"abc"))
assert model._is_image_part(part) is True
def test_non_image_inline_data(self):
"""Non-image MIME type is not an image part."""
model = _model()
part = Part(inline_data=Blob(mime_type="application/pdf", data=b"abc"))
assert model._is_image_part(part) is False
def test_text_part_not_image(self):
"""Plain text part is not an image."""
model = _model()
part = Part.from_text(text="hello")
assert not model._is_image_part(part)
# ---------------------------------------------------------------------------
# _part_to_message_block
# ---------------------------------------------------------------------------
class TestPartToMessageBlock:
def test_text_part(self):
"""Text part converts to TextBlockParam."""
model = _model()
part = Part.from_text(text="hello")
block = model._part_to_message_block(part)
assert block["type"] == "text"
assert block["text"] == "hello"
def test_function_call_part(self):
"""Function call part converts to ToolUseBlockParam."""
model = _model()
part = Part.from_function_call(name="fn", args={"x": 1})
part.function_call.id = "id1"
block = model._part_to_message_block(part)
assert block["type"] == "tool_use"
assert block["name"] == "fn"
assert block["input"] == {"x": 1}
def test_function_response_with_content_array(self):
"""Function response with content array is serialised properly."""
model = _model()
part = Part.from_function_response(
name="fn",
response={"content": [{"type": "text", "text": "ok"}, {"type": "other", "data": 1}]},
)
part.function_response.id = "id1"
block = model._part_to_message_block(part)
assert block["type"] == "tool_result"
assert "ok" in block["content"]
def test_function_response_with_result_field(self):
"""Function response with 'result' field uses that value."""
model = _model()
part = Part.from_function_response(name="fn", response={"result": "done"})
part.function_response.id = "id1"
block = model._part_to_message_block(part)
assert block["content"] == "done"
def test_function_response_simple_json(self):
"""Function response fallback serialises the full dict."""
model = _model()
part = Part.from_function_response(name="fn", response={"key": "val"})
part.function_response.id = "id1"
block = model._part_to_message_block(part)
assert json.loads(block["content"]) == {"key": "val"}
def test_executable_code_part(self):
"""Executable code is wrapped in code block."""
model = _model()
part = Part(executable_code=ExecutableCode(code="print(1)", language="PYTHON"))
block = model._part_to_message_block(part)
assert block["type"] == "text"
assert "print(1)" in block["text"]
def test_code_execution_result_part(self):
"""Code execution result is wrapped in output block."""
model = _model()
part = Part(code_execution_result=CodeExecutionResult(output="42", outcome="OUTCOME_OK"))
block = model._part_to_message_block(part)
assert "42" in block["text"]
def test_unsupported_part_raises(self):
"""Unsupported part type raises NotImplementedError."""
model = _model()
part = Part()
with pytest.raises(NotImplementedError):
model._part_to_message_block(part)
# ---------------------------------------------------------------------------
# _parse_finish_reason
# ---------------------------------------------------------------------------
class TestParseFinishReason:
def test_end_turn_maps_to_stop(self):
"""'end_turn' maps to STOP."""
model = _model()
assert model._parse_finish_reason("end_turn") == _FinishReason.STOP
def test_stop_sequence_maps_to_stop(self):
"""'stop_sequence' maps to STOP."""
model = _model()
assert model._parse_finish_reason("stop_sequence") == _FinishReason.STOP
def test_max_tokens(self):
"""'max_tokens' maps to MAX_TOKENS."""
model = _model()
assert model._parse_finish_reason("max_tokens") == _FinishReason.MAX_TOKENS
def test_tool_use(self):
"""'tool_use' maps to TOOL_USE."""
model = _model()
assert model._parse_finish_reason("tool_use") == _FinishReason.TOOL_USE
def test_unknown_maps_to_error(self):
"""Unknown reason maps to ERROR."""
model = _model()
assert model._parse_finish_reason("something_else") == _FinishReason.ERROR
def test_none_maps_to_error(self):
"""None maps to ERROR."""
model = _model()
assert model._parse_finish_reason(None) == _FinishReason.ERROR
# ---------------------------------------------------------------------------
# _update_type_string
# ---------------------------------------------------------------------------
class TestUpdateTypeString:
def test_lowercases_type_field(self):
"""Type field is lowered."""
model = _model()
d = {"type": "STRING"}
model._update_type_string(d)
assert d["type"] == "string"
def test_recursive_items(self):
"""Nested items type is lowered."""
model = _model()
d = {"type": "ARRAY", "items": {"type": "STRING"}}
model._update_type_string(d)
assert d["items"]["type"] == "string"
def test_recursive_properties_in_items(self):
"""Properties inside items are recursively processed."""
model = _model()
d = {
"type": "ARRAY",
"items": {
"type": "OBJECT",
"properties": {
"name": {"type": "STRING"},
},
},
}
model._update_type_string(d)
assert d["items"]["properties"]["name"]["type"] == "string"
# ---------------------------------------------------------------------------
# _function_declaration_to_tool_param
# ---------------------------------------------------------------------------
class TestFunctionDeclarationToToolParam:
def test_with_parameters_json_schema(self):
"""Uses parameters_json_schema when available."""
model = _model()
decl = MagicMock()
decl.name = "my_fn"
decl.description = "desc"
decl.parameters_json_schema = {"type": "object", "properties": {"x": {"type": "string"}}}
result = model._function_declaration_to_tool_param(decl)
assert result["name"] == "my_fn"
assert result["input_schema"] == decl.parameters_json_schema
def test_with_parameters_properties(self):
"""Converts from parameters when json_schema not available."""
model = _model()
decl = FunctionDeclaration(
name="fn",
description="d",
parameters=Schema(
type=Type.OBJECT,
properties={"a": Schema(type=Type.STRING)},
required=["a"],
),
)
result = model._function_declaration_to_tool_param(decl)
assert result["name"] == "fn"
assert "a" in result["input_schema"]["properties"]
assert result["input_schema"]["required"] == ["a"]
def test_no_parameters(self):
"""Works with no parameters at all."""
model = _model()
decl = FunctionDeclaration(name="fn", description="d")
result = model._function_declaration_to_tool_param(decl)
assert result["input_schema"]["type"] == "object"
assert result["input_schema"]["properties"] == {}
# ---------------------------------------------------------------------------
# _content_block_to_part
# ---------------------------------------------------------------------------
class TestContentBlockToPart:
def test_text_block(self):
"""TextBlock converts to text Part."""
model = _model()
block = anthropic_types.TextBlock(text="hello", type="text")
part = model._content_block_to_part(block)
assert part.text == "hello"
def test_tool_use_block(self):
"""ToolUseBlock converts to function call Part."""
model = _model()
block = anthropic_types.ToolUseBlock(id="id1", name="fn", input={"a": 1}, type="tool_use")
part = model._content_block_to_part(block)
assert part.function_call.name == "fn"
assert part.function_call.args == {"a": 1}
assert part.function_call.id == "id1"
def test_thinking_block(self):
"""ThinkingBlock converts to Part with thought=True."""
model = _model()
block = anthropic_types.ThinkingBlock(thinking="deep thoughts", type="thinking", signature="sig")
part = model._content_block_to_part(block)
assert part.text == "deep thoughts"
assert part.thought is True
def test_redacted_thinking_block(self):
"""RedactedThinkingBlock converts to Part with thought=True."""
model = _model()
block = anthropic_types.RedactedThinkingBlock(type="redacted_thinking", data="data")
part = model._content_block_to_part(block)
assert "redacted" in part.text.lower()
assert part.thought is True
def test_unsupported_block_raises(self):
"""Unsupported block raises NotImplementedError."""
model = _model()
block = MagicMock(spec=[])
with pytest.raises(NotImplementedError):
model._content_block_to_part(block)
# ---------------------------------------------------------------------------
# _format_messages — edge cases
# ---------------------------------------------------------------------------
class TestFormatMessagesEdgeCases:
def test_image_in_assistant_turn_skipped(self):
"""Image data in assistant turn is skipped with warning."""
model = _model()
img_part = Part(inline_data=Blob(mime_type="image/png", data=b"\x89PNG"))
request = LlmRequest(contents=[Content(parts=[img_part], role="model")])
messages = model._format_messages(request)
assert messages == []
def test_empty_message_blocks_filtered(self):
"""Content that produces no blocks is not added to messages."""
model = _model()
img_part = Part(inline_data=Blob(mime_type="image/jpeg", data=b"\xff"))
request = LlmRequest(contents=[Content(parts=[img_part], role="assistant")])
messages = model._format_messages(request)
assert messages == []
# ---------------------------------------------------------------------------
# _create_streaming_tool_call_response
# ---------------------------------------------------------------------------
class TestCreateStreamingToolCallResponse:
def test_returns_none_when_no_tool_uses(self):
"""Returns None with empty accumulated_tool_uses."""
model = _model()
assert model._create_streaming_tool_call_response([], '{"a":1}') is None
def test_returns_none_when_tool_name_empty(self):
"""Returns None when current tool has no name."""
model = _model()
result = model._create_streaming_tool_call_response(
[{"id": "id1", "name": "", "accumulated_input": ""}],
'{"x":1}',
)
assert result is None
def test_returns_none_when_tool_not_in_streaming_set(self):
"""Returns None when tool name is not in streaming_tool_names."""
model = _model()
result = model._create_streaming_tool_call_response(
[{"id": "id1", "name": "my_tool", "accumulated_input": "{}"}],
'{"x":1}',
streaming_tool_names={"other_tool"},
)
assert result is None
def test_returns_response_when_tool_in_streaming_set(self):
"""Returns LlmResponse when tool name is in streaming_tool_names."""
model = _model()
result = model._create_streaming_tool_call_response(
[{"id": "id1", "name": "my_tool", "accumulated_input": '{"x":'}],
'1}',
streaming_tool_names={"my_tool"},
)
assert result is not None
assert result.partial is True
assert result.content.parts[0].function_call.name == "my_tool"
def test_returns_response_when_streaming_names_none(self):
"""Returns response for any tool when streaming_tool_names is None."""
model = _model()
result = model._create_streaming_tool_call_response(
[{"id": "id1", "name": "any_tool", "accumulated_input": ""}],
'{}',
streaming_tool_names=None,
)
assert result is not None
# ---------------------------------------------------------------------------
# _merge_configs — additional cases
# ---------------------------------------------------------------------------
class TestMergeConfigsExtended:
def test_no_request_no_default_returns_empty(self):
"""No request config and no default yields empty GenerateContentConfig."""
model = _model()
merged = model._merge_configs(None)
assert isinstance(merged, GenerateContentConfig)
def test_default_fields_applied_to_request(self):
"""Default config fields fill in unset request config fields."""
default = GenerateContentConfig(temperature=0.3, top_p=0.9, max_output_tokens=500)
model = _model(generate_content_config=default)
req = GenerateContentConfig(temperature=0.7)
merged = model._merge_configs(req)
assert merged.temperature == 0.7
assert merged.max_output_tokens == 500
assert merged.top_p == 0.9
# ---------------------------------------------------------------------------
# _log_unsupported_config_options
# ---------------------------------------------------------------------------
class TestLogUnsupportedConfigOptions:
def test_logs_warning_for_unsupported_options(self):
"""Logs warnings for every unsupported config option."""
model = _model()
config = GenerateContentConfig(
frequency_penalty=0.5,
presence_penalty=0.5,
seed=42,
)
with patch("trpc_agent_sdk.models._anthropic_model.logger") as mock_logger:
model._log_unsupported_config_options(config)
mock_logger.warning.assert_called_once()
logged_msg = mock_logger.warning.call_args[0][1]
assert "frequency_penalty" in logged_msg
assert "presence_penalty" in logged_msg
assert "seed" in logged_msg
def test_no_warning_for_supported_options(self):
"""No warning when only supported options are set."""
model = _model()
config = GenerateContentConfig(temperature=0.5)
with patch("trpc_agent_sdk.models._anthropic_model.logger") as mock_logger:
model._log_unsupported_config_options(config)
mock_logger.warning.assert_not_called()
# ---------------------------------------------------------------------------
# _generate_single — error path
# ---------------------------------------------------------------------------
class TestGenerateSingleError:
@pytest.mark.asyncio
async def test_api_error_raises_and_closes_client(self):
"""API error during single generation is raised to the retry layer."""
model = _model()
mock_client = AsyncMock()
mock_client.messages.create = AsyncMock(side_effect=RuntimeError("timeout"))
model._http_client_provider.close_http_client = AsyncMock()
with patch.object(model, "_create_async_client", return_value=mock_client):
with pytest.raises(RuntimeError, match="timeout"):
await model._generate_single({}, LlmRequest(contents=[]))
model._http_client_provider.close_http_client.assert_awaited_once_with(mock_client)
# ---------------------------------------------------------------------------
# _create_async_client
# ---------------------------------------------------------------------------
class TestCreateAsyncClient:
def test_client_created_with_api_key(self):
"""Client is created with the model's api_key."""
model = _model(api_key="sk-test", base_url="https://example.com")
with patch("trpc_agent_sdk.models._anthropic_model.AsyncAnthropic") as MockClient:
model._create_async_client()
MockClient.assert_called_once()
kwargs = MockClient.call_args.kwargs
assert kwargs["api_key"] == "sk-test"
assert kwargs["base_url"] == "https://example.com"
assert kwargs["max_retries"] == 0
def test_client_without_base_url(self):
"""Client is created without base_url when not set."""
model = _model(api_key="sk-test")
with patch("trpc_agent_sdk.models._anthropic_model.AsyncAnthropic") as MockClient:
model._create_async_client()
kwargs = MockClient.call_args.kwargs
assert kwargs["base_url"] is None
# ---------------------------------------------------------------------------
# validate_request — edge cases
# ---------------------------------------------------------------------------
class TestValidateRequestExtended:
def test_part_with_inline_data_is_valid(self):
"""Part with inline_data passes validation."""
model = _model()
part = Part(inline_data=Blob(mime_type="image/png", data=b"img"))
request = LlmRequest(contents=[Content(parts=[part], role="user")])
model.validate_request(request)
if __name__ == "__main__":
pytest.main([__file__, "-v"])