nanobot/tests/providers/test_anthropic_tool_result.py
chengyongru 0a396aa6e2
Improve tool call validation strictness (#4190)
* Improve tool call validation strictness

Reject near-miss tool names without executing suggested tools. Require object-shaped tool parameters while preserving only lossless JSON wire-shape normalization.

* Tighten tool call argument validation

* Simplify tool argument validation tests

* Improve tool name suggestions

* Simplify tool suggestion helpers

* Limit tool suggestions to canonical matches

* Allow repair only for tool history replay

* Clarify non-object tool argument errors

* Inline replay tool argument normalization

* Track only successful tool executions

* Reject JSON null tool arguments
2026-06-09 14:50:40 +08:00

97 lines
3.3 KiB
Python

"""Tests for AnthropicProvider._tool_result_block image_url conversion.
Regression for: tool results containing OpenAI-format image_url blocks
(e.g. from read_file on an image file, via build_image_content_blocks)
were passed to Anthropic unconverted, causing silent image drops with a
"Non-transient LLM error with image content, retrying without images"
warning.
Also tests that bare dicts without a "type" field are coerced to text
blocks, fixing Anthropic "content.0.type: Field required" rejections (#3993).
"""
from nanobot.providers.anthropic_provider import AnthropicProvider
def test_tool_result_block_converts_image_url_in_list_content():
"""image_url blocks inside tool_result list content must be translated
to Anthropic-native image blocks; sibling text blocks pass through."""
msg = {
"role": "tool",
"tool_call_id": "call_1",
"content": [
{
"type": "image_url",
"image_url": {"url": "data:image/png;base64,AAAA"},
"_meta": {"path": "/tmp/x.png"},
},
{"type": "text", "text": "(Image file: /tmp/x.png)"},
],
}
block = AnthropicProvider._tool_result_block(msg)
assert block["type"] == "tool_result"
assert block["tool_use_id"] == "call_1"
content = block["content"]
assert isinstance(content, list)
assert content[0] == {
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "AAAA",
},
}
assert content[1] == {"type": "text", "text": "(Image file: /tmp/x.png)"}
def test_tool_result_block_preserves_string_content():
"""String content must be passed through unchanged; the image-conversion
path for lists must not affect the string path."""
msg = {
"role": "tool",
"tool_call_id": "call_2",
"content": "plain tool output",
}
block = AnthropicProvider._tool_result_block(msg)
assert block["type"] == "tool_result"
assert block["tool_use_id"] == "call_2"
assert block["content"] == "plain tool output"
def test_convert_user_content_coerces_typeless_dict():
"""Bare dicts without a "type" field must be coerced to text blocks.
Regression for #3993: tools returning plain dicts caused Anthropic to
reject the request with "content.0.type: Field required"."""
result = AnthropicProvider._convert_user_content([
{"foo": "bar"},
{"type": "text", "text": "ok"},
])
assert result[0] == {"type": "text", "text": str({"foo": "bar"})}
assert result[1] == {"type": "text", "text": "ok"}
def test_convert_user_content_coerces_mixed_typeless():
"""Multiple typeless items and non-dict items are all handled."""
result = AnthropicProvider._convert_user_content([
42,
{"key": "val"},
])
assert result[0] == {"type": "text", "text": "42"}
assert result[1] == {"type": "text", "text": str({"key": "val"})}
def test_convert_assistant_message_repairs_history_tool_arguments():
blocks = AnthropicProvider._assistant_blocks({
"role": "assistant",
"content": None,
"tool_calls": [{
"id": "toolu_1",
"function": {"name": "read_file", "arguments": '{path:"foo.txt"}'},
}],
})
assert blocks[0]["type"] == "tool_use"
assert blocks[0]["input"] == {"path": "foo.txt"}