mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(responses): check allowed_tools entries without recursion and drop a nested allowed_tools
The repo's recursion check refuses recursive helpers, so allowed_tools entries now go through a separate per-entry check. OpenAI rejects an allowed_tools entry nested inside allowed_tools with a 400, so that choice now drops under drop_params.
This commit is contained in:
parent
f9c805e77f
commit
eb1d8919b9
2 changed files with 28 additions and 3 deletions
|
|
@ -337,11 +337,19 @@ def _names_a_sent_tool(tool_choice: object, tools: tuple[Mapping[str, object], .
|
|||
choice: Final = as_str_mapping(tool_choice)
|
||||
if choice is None:
|
||||
return False
|
||||
if choice.get("type") != "allowed_tools":
|
||||
return _sent_tools_include(choice, tools)
|
||||
allowed: Final = choice.get("tools")
|
||||
entries: Final = tuple(map(as_str_mapping, _json_array_or_empty(allowed)))
|
||||
return not isinstance(allowed, list | tuple) or all(
|
||||
entry is not None and _sent_tools_include(entry, tools) for entry in entries
|
||||
)
|
||||
|
||||
|
||||
def _sent_tools_include(choice: Mapping[str, object], tools: tuple[Mapping[str, object], ...]) -> bool:
|
||||
match choice.get("type"):
|
||||
case "allowed_tools":
|
||||
allowed: Final = choice.get("tools")
|
||||
entries: Final = _json_array_or_empty(allowed)
|
||||
return not isinstance(allowed, list | tuple) or all(_names_a_sent_tool(entry, tools) for entry in entries)
|
||||
return False
|
||||
case "function":
|
||||
return any(tool.get("type") == "function" and tool.get("name") == choice.get("name") for tool in tools)
|
||||
case "custom":
|
||||
|
|
|
|||
|
|
@ -3346,6 +3346,23 @@ class TestMalformedParamsFollowDropParams:
|
|||
},
|
||||
id="allowed-tools-naming-a-tool-not-sent",
|
||||
),
|
||||
pytest.param(
|
||||
{
|
||||
"tool_choice": {
|
||||
"type": "allowed_tools",
|
||||
"mode": "auto",
|
||||
"tools": [
|
||||
{
|
||||
"type": "allowed_tools",
|
||||
"mode": "auto",
|
||||
"tools": [{"type": "function", "name": "get_weather"}],
|
||||
}
|
||||
],
|
||||
},
|
||||
"tools": [_WEATHER_TOOL, {"type": "web_search"}],
|
||||
},
|
||||
id="allowed-tools-nested-in-allowed-tools",
|
||||
),
|
||||
pytest.param({"parallel_tool_calls": "true"}, id="parallel-tool-calls-string"),
|
||||
pytest.param({"previous_response_id": 12}, id="previous-response-id-int"),
|
||||
pytest.param({"previous_response_id": ""}, id="previous-response-id-empty"),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue