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]

fix: preserve $defs for Anthropic tools input schema (#16648)

Luka Pečnikd84e97f211b2
diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.py
index 1f20d4b0c77..903d4297908 100644
--- a/litellm/types/llms/anthropic.py
+++ b/litellm/types/llms/anthropic.py
@@ -17,11 +17,17 @@ class AnthropicMessagesToolChoice(TypedDict, total=False):
disable_parallel_tool_use: bool # default is false
-class AnthropicInputSchema(TypedDict, total=False):
- type: Optional[str]
- properties: Optional[dict]
- additionalProperties: Optional[bool]
- required: Optional[List[str]]
+AnthropicInputSchema = TypedDict(
+ "AnthropicInputSchema",
+ {
+ "type": Optional[str],
+ "properties": Optional[dict],
+ "additionalProperties": Optional[bool],
+ "required": Optional[List[str]],
+ "$defs": Optional[Dict],
+ },
+ total=False,
+)
class AnthropicMessagesTool(TypedDict, total=False):
diff --git a/tests/llm_translation/test_anthropic_completion.py b/tests/llm_translation/test_anthropic_completion.py
index 42a3cbfca38..27841140e5b 100644
--- a/tests/llm_translation/test_anthropic_completion.py
+++ b/tests/llm_translation/test_anthropic_completion.py
@@ -768,6 +768,52 @@ def test_anthropic_map_openai_params_tools_and_json_schema():
assert "Question" in json.dumps(mapped_params)
+def test_anthropic_map_openai_params_tools_with_defs():
+ args = {
+ "non_default_params": {
+ "tools": [
+ {
+ "type": "function",
+ "function": {
+ "name": "create_user",
+ "description": "Create a user from provided profile data.",
+ "parameters": {
+ "type": "object",
+ "properties": {
+ "user": {"$ref": "#/$defs/User"},
+ },
+ "required": ["user"],
+ "$defs": {
+ "User": {
+ "type": "object",
+ "properties": {
+ "name": {"type": "string"},
+ "email": {"type": "string"},
+ },
+ "required": ["name", "email"],
+ }
+ },
+ },
+ },
+ }
+ ]
+ }
+ }
+
+ mapped_params = litellm.AnthropicConfig().map_openai_params(
+ non_default_params=args["non_default_params"],
+ optional_params={},
+ model="claude-3-5-sonnet-20240620",
+ drop_params=False,
+ )
+
+ tool = mapped_params["tools"][0]
+ assert tool["input_schema"]["properties"]["user"]["$ref"] == "#/$defs/User"
+ assert (
+ tool["input_schema"]["$defs"]["User"]["properties"]["name"]["type"] == "string"
+ )
+
+
from litellm.constants import RESPONSE_FORMAT_TOOL_NAME