Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge branch 'litellm_internal_staging' into fix/anthropic-responses-optional-tool-props

mateo-berri3cdf041827c3
diff --cc tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
index 0c30d8a8322,9145829ecb2..1185893d428
--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
@@@ -3510,48 -3518,179 +3518,226 @@@ def test_translate_anthropic_tools_to_o
assert new_tools[0]["type"] == "function"
+def test_translate_anthropic_tools_to_openai_maps_strict_onto_function_not_parameters():
+ """A tool-level `strict` lands on the OpenAI function, leaving the caller's `input_schema` untouched."""
+ adapter = LiteLLMAnthropicMessagesAdapter()
+ input_schema = {
+ "type": "object",
+ "properties": {"city": {"type": "string"}},
+ "required": ["city"],
+ "additionalProperties": False,
+ }
+ tools = [{"type": "custom", "name": "get_weather", "strict": True, "input_schema": input_schema}]
+
+ new_tools, _ = adapter.translate_anthropic_tools_to_openai(tools=tools)
+
+ function = new_tools[0]["function"]
+ assert function["strict"] is True
+ assert "strict" not in function["parameters"]
+ assert input_schema == {
+ "type": "object",
+ "properties": {"city": {"type": "string"}},
+ "required": ["city"],
+ "additionalProperties": False,
+ }
+
+
+def test_translate_anthropic_tools_to_openai_omits_unset_strict():
+ """Chat Completions already defaults to non-strict, so an unset `strict` stays unset."""
+ adapter = LiteLLMAnthropicMessagesAdapter()
+ tools = [
+ {
+ "type": "custom",
+ "name": "search",
+ "input_schema": {
+ "type": "object",
+ "properties": {"query": {"type": "string"}, "cursor": {"type": "string"}},
+ "required": ["query"],
+ },
+ }
+ ]
+
+ new_tools, _ = adapter.translate_anthropic_tools_to_openai(tools=tools)
+
+ function = new_tools[0]["function"]
+ assert "strict" not in function
+ assert "strict" not in function["parameters"]
+ assert function["parameters"]["required"] == ["query"]
++
++
+ TOOL_RESULT_IMAGE_B64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
+ TOOL_RESULT_IMAGE_URL = "https://example.com/screenshot.png"
+
+
+ def _anthropic_tool_use_turn(*tool_use_ids):
+ return AnthopicMessagesAssistantMessageParam(
+ role="assistant",
+ content=[
+ {"type": "tool_use", "id": tid, "name": "read_file", "input": {"path": "img.png"}}
+ for tid in tool_use_ids
+ ],
+ )
+
+
+ def _anthropic_tool_result_turn(blocks_by_tool_use_id):
+ return AnthropicMessagesUserMessageParam(
+ role="user",
+ content=[
+ {"type": "tool_result", "tool_use_id": tid, "content": blocks}
+ for tid, blocks in blocks_by_tool_use_id.items()
+ ],
+ )
+
+
+ def _base64_image_block():
+ return {
+ "type": "image",
+ "source": {"type": "base64", "media_type": "image/png", "data": TOOL_RESULT_IMAGE_B64},
+ }
+
+
+ def _url_image_block():
+ return {"type": "image", "source": {"type": "url", "url": TOOL_RESULT_IMAGE_URL}}
+
+
+ def _run_chat_completions_pipeline(anthropic_messages):
+ """Anthropic /v1/messages input -> chat adapter -> the OpenAI-compatible
+ request transformation every OpenAIGPTConfig-based provider runs."""
+ adapter = LiteLLMAnthropicMessagesAdapter()
+ translated = adapter.translate_anthropic_messages_to_openai(messages=anthropic_messages)
+ request = OpenAIGPTConfig().transform_request(
+ model="gpt-5.4-mini", messages=translated, optional_params={}, litellm_params={}, headers={}
+ )
+ return request["messages"]
+
+
+ def _images_in_tool_messages(messages):
+ found = []
+ for message in messages:
+ if message.get("role") != "tool":
+ continue
+ content = message.get("content")
+ if isinstance(content, str) and content.startswith("data:image"):
+ found.append(content)
+ elif isinstance(content, list):
+ found.extend(p for p in content if isinstance(p, dict) and p.get("type") == "image_url")
+ return found
+
+
+ def _image_urls_in_user_messages(messages):
+ return [
+ part["image_url"]["url"]
+ for message in messages
+ if message.get("role") == "user" and isinstance(message.get("content"), list)
+ for part in message["content"]
+ if isinstance(part, dict) and part.get("type") == "image_url"
+ ]
+
+
+ @pytest.mark.parametrize(
+ "image_block,expected_url_prefix",
+ [
+ (_base64_image_block(), "data:image/png;base64,"),
+ (_url_image_block(), TOOL_RESULT_IMAGE_URL),
+ ],
+ ids=["base64_source", "url_source"],
+ )
+ def test_tool_result_single_image_visible_after_openai_transform(image_block, expected_url_prefix):
+ result = _run_chat_completions_pipeline(
+ [
+ _anthropic_tool_use_turn("toolu_01"),
+ _anthropic_tool_result_turn({"toolu_01": [image_block]}),
+ ]
+ )
+
+ assert _images_in_tool_messages(result) == []
+ user_image_urls = _image_urls_in_user_messages(result)
+ assert len(user_image_urls) == 1
+ assert user_image_urls[0].startswith(expected_url_prefix)
+
+ tool_messages = [m for m in result if m.get("role") == "tool"]
+ assert len(tool_messages) == 1
+ assert tool_messages[0]["tool_call_id"] == "toolu_01"
+ assert tool_messages[0]["content"] == TOOL_RESULT_IMAGE_PLACEHOLDER
+
+
+ def test_tool_result_text_and_image_visible_after_openai_transform():
+ result = _run_chat_completions_pipeline(
+ [
+ _anthropic_tool_use_turn("toolu_01"),
+ _anthropic_tool_result_turn(
+ {"toolu_01": [{"type": "text", "text": "screenshot saved"}, _base64_image_block()]}
+ ),
+ ]
+ )
+
+ assert _images_in_tool_messages(result) == []
+ assert len(_image_urls_in_user_messages(result)) == 1
+
+ tool_messages = [m for m in result if m.get("role") == "tool"]
+ assert tool_messages[0]["content"] == [{"type": "text", "text": "screenshot saved"}]
+
+
+ def test_tool_result_two_images_visible_after_openai_transform():
+ result = _run_chat_completions_pipeline(
+ [
+ _anthropic_tool_use_turn("toolu_01"),
+ _anthropic_tool_result_turn({"toolu_01": [_base64_image_block(), _base64_image_block()]}),
+ ]
+ )
+
+ assert _images_in_tool_messages(result) == []
+ assert len(_image_urls_in_user_messages(result)) == 2
+
+
+ def test_tool_result_parallel_tool_calls_keep_tool_message_adjacency():
+ result = _run_chat_completions_pipeline(
+ [
+ _anthropic_tool_use_turn("toolu_01", "toolu_02"),
+ _anthropic_tool_result_turn(
+ {"toolu_01": [_base64_image_block()], "toolu_02": [_url_image_block()]}
+ ),
+ ]
+ )
+
+ roles = [m.get("role") for m in result]
+ assert roles == ["assistant", "tool", "tool", "user"]
+ assert _images_in_tool_messages(result) == []
+ assert len(_image_urls_in_user_messages(result)) == 2
+
+
+ @pytest.mark.parametrize(
+ "image_block",
+ [
+ {"type": "image", "source": {"type": "unsupported"}},
+ {"type": "image"},
+ {"type": "image", "source": "https://example.com/screenshot.png"},
+ ],
+ ids=["untranslatable_source", "missing_source", "non_dict_source"],
+ )
+ def test_tool_result_malformed_image_source_keeps_empty_tool_content(image_block):
+ adapter = LiteLLMAnthropicMessagesAdapter()
+ translated = adapter.translate_anthropic_messages_to_openai(
+ messages=[
+ _anthropic_tool_use_turn("toolu_01"),
+ _anthropic_tool_result_turn({"toolu_01": [image_block]}),
+ ]
+ )
+
+ tool_messages = [m for m in translated if m.get("role") == "tool"]
+ assert len(tool_messages) == 1
+ assert tool_messages[0]["content"] == ""
+
+
+ def test_tool_result_plain_text_unchanged_by_openai_transform():
+ result = _run_chat_completions_pipeline(
+ [
+ _anthropic_tool_use_turn("toolu_01"),
+ _anthropic_tool_result_turn({"toolu_01": [{"type": "text", "text": "42 files found"}]}),
+ ]
+ )
+
+ tool_messages = [m for m in result if m.get("role") == "tool"]
+ assert len(tool_messages) == 1
+ assert tool_messages[0]["content"] == "42 files found"
+ assert _image_urls_in_user_messages(result) == []