diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.py index 1f20d4b0c7..903d429790 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 42a3cbfca3..27841140e5 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