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 remote-tracking branch 'origin/litellm_internal_staging' into litellm_fix_azure_chat_anyof_tool_schema

mateo-berri0f35328a2176
diff --cc tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_common_utils.py
index dcc0d72df91,9ab66d55f3f..c037f928593
--- a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_common_utils.py
+++ b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_common_utils.py
@@@ -1435,75 -1435,48 +1435,122 @@@ class TestFlattenTopLevelSchemaCombinat
assert schema == snapshot
+class TestToolWithFlattenedParameters:
+ def _anyof_tool(self):
+ return {
+ "type": "function",
+ "function": {
+ "name": "automation_update",
+ "description": "Update an automation",
+ "parameters": {
+ "type": "object",
+ "anyOf": [
+ {
+ "properties": {"id": {"type": "string"}, "enabled": {"type": "boolean"}},
+ "required": ["id", "enabled"],
+ },
+ {
+ "properties": {"id": {"type": "string"}, "schedule": {"type": "string"}},
+ "required": ["id", "schedule"],
+ },
+ ],
+ "properties": {"id": {"type": "string"}},
+ "required": ["id"],
+ },
+ },
+ }
+
+ def test_flattens_anyof_parameters_into_new_tool(self):
+ from litellm.litellm_core_utils.prompt_templates.common_utils import (
+ tool_with_flattened_parameters,
+ )
+
+ tool = self._anyof_tool()
+ result = tool_with_flattened_parameters(tool)
+
+ assert result is not tool
+ parameters = result["function"]["parameters"]
+ assert "anyOf" not in parameters
+ assert parameters["type"] == "object"
+ assert set(parameters["properties"]) == {"id", "enabled", "schedule"}
+ assert parameters["required"] == ["id"]
+ assert result["function"]["name"] == "automation_update"
+ assert tool == self._anyof_tool()
+
+ def test_clean_parameters_return_the_same_tool_object(self):
+ from litellm.litellm_core_utils.prompt_templates.common_utils import (
+ tool_with_flattened_parameters,
+ )
+
+ tool = {
+ "type": "function",
+ "function": {
+ "name": "lookup",
+ "parameters": {"type": "object", "properties": {"id": {"type": "string"}}, "required": ["id"]},
+ },
+ }
+
+ assert tool_with_flattened_parameters(tool) is tool
+
+ @pytest.mark.parametrize(
+ "tool",
+ [
+ {"type": "function"},
+ {"type": "function", "function": "not-a-dict"},
+ {"type": "function", "function": {"name": "no_params"}},
+ {"type": "function", "function": {"name": "bad_params", "parameters": "not-a-dict"}},
+ ],
+ )
+ def test_non_dict_function_or_parameters_return_the_same_tool_object(self, tool):
+ from litellm.litellm_core_utils.prompt_templates.common_utils import (
+ tool_with_flattened_parameters,
+ )
+
+ assert tool_with_flattened_parameters(tool) is tool
++
++
+ class TestRequestContainsImageContent:
+ """One detector for every dialect that reaches pre-routing hooks untranslated."""
+
+ @pytest.mark.parametrize(
+ "part",
+ [
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64,aGk="}},
+ {"type": "input_image", "image_url": "data:image/png;base64,aGk="},
+ {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "aGk="}},
+ {
+ "type": "tool_result",
+ "tool_use_id": "tu_1",
+ "content": [{"type": "image", "source": {"type": "base64", "data": "aGk="}}],
+ },
+ ],
+ )
+ def test_detects_every_image_dialect_including_tool_results(self, part):
+ from litellm.litellm_core_utils.prompt_templates.common_utils import request_contains_image_content
+
+ messages = [{"role": "user", "content": [{"type": "text", "text": "hi"}, part]}]
+ assert request_contains_image_content(messages) is True
+
+ @pytest.mark.parametrize(
+ "messages",
+ [
+ [{"role": "user", "content": "plain string"}],
+ [{"role": "user", "content": [{"type": "text", "text": "hi"}]}],
+ [{"role": "user", "content": [{"type": "input_audio", "input_audio": {"data": "x"}}]}],
+ [{"role": "user", "content": [{"type": "tool_result", "content": [{"type": "text", "text": "ok"}]}]}],
+ [{"role": "user", "content": None}],
+ [],
+ ],
+ )
+ def test_ignores_text_audio_and_degenerate_shapes(self, messages):
+ from litellm.litellm_core_utils.prompt_templates.common_utils import request_contains_image_content
+
+ assert request_contains_image_content(messages) is False
+
+ def test_hostile_nesting_is_depth_bounded(self):
+ from litellm.litellm_core_utils.prompt_templates.common_utils import request_contains_image_content
+
+ nested: dict = {"type": "image", "source": {"type": "base64", "data": "aGk="}}
+ for _ in range(50):
+ nested = {"type": "tool_result", "content": [nested]}
+ assert request_contains_image_content([{"role": "user", "content": [nested]}]) is False