mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
fix(tool_choice): keep allowed_tools selector extension fields and leave conflicting selectors untranslated
A nested function or custom selector now keeps its extra keys when flattened for the Responses bridge, and a selector that has both a flat name and a nested object, or keys that collide when lifted, makes the bridge send the original restriction untouched instead of guessing which name wins
This commit is contained in:
parent
2b42ecc128
commit
48d4b994a4
2 changed files with 27 additions and 17 deletions
|
|
@ -30,13 +30,7 @@ class ChatAllowedToolsToolChoice(BaseModel):
|
|||
_CHAT_ALLOWED_TOOLS_TOOL_CHOICE: Final = TypeAdapter(ChatAllowedToolsToolChoice)
|
||||
|
||||
|
||||
class _NamedTool(BaseModel):
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
name: str = Field(min_length=1)
|
||||
|
||||
|
||||
_NAMED_TOOL: Final = TypeAdapter(_NamedTool)
|
||||
_SELECTOR: Final = TypeAdapter(dict[str, object])
|
||||
|
||||
|
||||
class _ToolChoiceType(BaseModel):
|
||||
|
|
@ -83,22 +77,30 @@ def parse_chat_allowed_tools_tool_choice(tool_choice: object) -> ChatAllowedTool
|
|||
return None
|
||||
|
||||
|
||||
def _has_name(selector: Mapping[str, object]) -> bool:
|
||||
name: Final = selector.get("name")
|
||||
return isinstance(name, str) and name != ""
|
||||
|
||||
|
||||
def _responses_allowed_tool(tool: Mapping[str, object]) -> Mapping[str, object] | None:
|
||||
tool_type: Final = tool.get("type")
|
||||
if tool_type not in ("function", "custom"):
|
||||
return tool
|
||||
if isinstance(tool.get("name"), str):
|
||||
return tool
|
||||
if tool_type not in tool:
|
||||
return tool if _has_name(tool) else None
|
||||
try:
|
||||
nested: Final = _NAMED_TOOL.validate_python(tool.get(tool_type))
|
||||
nested: Final = _SELECTOR.validate_python(tool[tool_type])
|
||||
except ValidationError:
|
||||
return None
|
||||
return {"type": tool_type, "name": nested.name}
|
||||
outer: Final = {key: value for key, value in tool.items() if key != tool_type}
|
||||
if not _has_name(nested) or outer.keys() & nested.keys():
|
||||
return None
|
||||
return {**outer, **nested}
|
||||
|
||||
|
||||
def responses_allowed_tools_tool_choice(choice: ChatAllowedToolsToolChoice) -> ToolChoiceAllowedParam | None:
|
||||
"""The flat Responses API layout of a chat ``allowed_tools`` choice, or ``None`` when an entry is not one the
|
||||
chat layout defines, so the caller sends the original rather than a rewrite that could mean something looser."""
|
||||
"""The flat Responses API layout of a chat ``allowed_tools`` choice, or ``None`` when an entry is malformed or
|
||||
carries conflicting keys, so the caller sends the original rather than a rewrite that could mean something looser."""
|
||||
flattened: Final = tuple(_responses_allowed_tool(tool) for tool in choice.allowed_tools.tools)
|
||||
if any(tool is None for tool in flattened):
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ _CHAT_LAYOUT: Final[dict[str, object]] = {
|
|||
"mode": "auto",
|
||||
"tools": [
|
||||
{"type": "function", "function": {"name": "get_time"}},
|
||||
{"type": "custom", "custom": {"name": "apply_patch"}},
|
||||
{"type": "custom", "custom": {"name": "apply_patch", "x-vendor": 1}, "x-trace": "a"},
|
||||
{"type": "mcp", "server_label": "docs", "server_url": "https://docs.example"},
|
||||
{"type": "function", "name": "already_flat"},
|
||||
],
|
||||
|
|
@ -37,7 +37,7 @@ def test_responses_layout_lifts_mode_and_tools_and_flattens_names_in_order():
|
|||
"mode": "auto",
|
||||
"tools": [
|
||||
{"type": "function", "name": "get_time"},
|
||||
{"type": "custom", "name": "apply_patch"},
|
||||
{"type": "custom", "name": "apply_patch", "x-vendor": 1, "x-trace": "a"},
|
||||
{"type": "mcp", "server_label": "docs", "server_url": "https://docs.example"},
|
||||
{"type": "function", "name": "already_flat"},
|
||||
],
|
||||
|
|
@ -54,9 +54,17 @@ def test_responses_layout_leaves_the_caller_dict_untouched():
|
|||
|
||||
@pytest.mark.parametrize(
|
||||
"tool",
|
||||
[{"type": "function"}, {"type": "function", "function": {}}, {"type": "custom", "custom": {"name": ""}}],
|
||||
[
|
||||
{"type": "function"},
|
||||
{"type": "function", "name": ""},
|
||||
{"type": "function", "function": {}},
|
||||
{"type": "function", "function": "get_time"},
|
||||
{"type": "custom", "custom": {"name": ""}},
|
||||
{"type": "function", "name": "get_time", "function": {"name": "get_weather"}},
|
||||
{"type": "function", "function": {"name": "get_time", "type": "custom"}},
|
||||
],
|
||||
)
|
||||
def test_responses_layout_refuses_to_rewrite_an_entry_without_a_name(tool: dict[str, object]):
|
||||
def test_responses_layout_refuses_to_rewrite_a_malformed_or_conflicting_entry(tool: dict[str, object]):
|
||||
tool_choice: Final[dict[str, object]] = {
|
||||
"type": "allowed_tools",
|
||||
"allowed_tools": {"mode": "required", "tools": [tool]},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue