Skip to content

Build and run agents you can see, understand and trust.

fix(model): refine _format_tools for openai response model (#1635)

qbccda90db87c72
diff --git a/src/agentscope/model/_openai_response/_model.py b/src/agentscope/model/_openai_response/_model.py
index fc5d2e3ad..6c004b28d 100644
--- a/src/agentscope/model/_openai_response/_model.py
+++ b/src/agentscope/model/_openai_response/_model.py
@@ -443,13 +443,12 @@ class OpenAIResponseModel(ChatModelBase):
tools: list[dict] | None,
tool_choice: ToolChoice | None,
) -> tuple[list[dict] | None, str | dict | None]:
- """Validate, filter, and format tools and tool_choice for the
- Responses API.
+ """Validate and format tools and tool_choice for the Responses API.
- When ``tool_choice.tools`` is specified the schemas list is filtered
- to only those tools. When ``tool_choice.mode`` is a specific tool name
- (str) the model is forced to call exactly that tool without needing to
- filter the list, preserving prompt-cache efficiency.
+ The full ``tools`` list is always sent unchanged to maximise prompt
+ cache hits. When ``tool_choice.tools`` restricts the callable
+ subset, the ``allowed_tools`` tool_choice format is used instead of
+ filtering the schemas list.
Args:
tools (`list[dict] | None`, optional):
@@ -464,9 +463,6 @@ class OpenAIResponseModel(ChatModelBase):
"""
if tool_choice and tools:
self._validate_tool_choice(tool_choice, tools)
- if tool_choice.tools:
- allowed = set(tool_choice.tools)
- tools = [t for t in tools if t["function"]["name"] in allowed]
fmt_tools = None
if tools:
@@ -482,4 +478,14 @@ class OpenAIResponseModel(ChatModelBase):
if mode not in _TOOL_CHOICE_LITERAL_MODES:
return fmt_tools, {"type": "function", "name": mode}
+ if tool_choice.tools:
+ return fmt_tools, {
+ "type": "allowed_tools",
+ "mode": mode,
+ "tools": [
+ {"type": "function", "name": name}
+ for name in tool_choice.tools
+ ],
+ }
+
return fmt_tools, mode
diff --git a/tests/model_openai_response_test.py b/tests/model_openai_response_test.py
index 4563a5c08..5d7c86708 100644
--- a/tests/model_openai_response_test.py
+++ b/tests/model_openai_response_test.py
@@ -539,14 +539,22 @@ class TestOpenAIResponseFormatTools(unittest.TestCase):
)
def test_tools_filtered(self) -> None:
- """ToolChoice with tools list filters to matching function names."""
+ """ToolChoice with tools list keeps the full tools schema and
+ narrows the callable subset via ``allowed_tools`` to preserve
+ prompt cache hits."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="auto", tools=["get_weather"]),
)
- self.assertEqual(len(fmt_tools), 1)
- self.assertEqual(fmt_tools[0]["name"], "get_weather")
- self.assertEqual(fmt_choice, "auto")
+ self.assertListEqual(fmt_tools, _FT_TOOLS_RESPONSE)
+ self.assertEqual(
+ fmt_choice,
+ {
+ "type": "allowed_tools",
+ "mode": "auto",
+ "tools": [{"type": "function", "name": "get_weather"}],
+ },
+ )
def test_no_tool_choice(self) -> None:
"""Tools are converted when tool_choice is None."""