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

* fix: preserve $defs for Anthropic tools input schema

* fix: preserve $defs for Anthropic tools input schema

* fix: preserve $defs for Anthropic tools input schema
This commit is contained in:
Luka Pečnik
2025-11-15 04:35:27 +01:00
committed by GitHub
parent 473886d35a
commit d84e97f211
2 changed files with 57 additions and 5 deletions

View File

@@ -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):

View File

@@ -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