Skip to content

Commit cc5d516

Browse files
sushant-mecopybara-github
authored andcommitted
fix: refuse the reserved tool name set_model_response
Merge #7145 Fixes #7144 PiperOrigin-RevId: 992443492
1 parent 490cfd7 commit cc5d516

7 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎src/google/adk/cli/agent_test_runner.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from google.adk.models.llm_response import LlmResponse
3636
from google.adk.runners import Runner
3737
from google.adk.sessions.in_memory_session_service import InMemorySessionService
38+
from google.adk.tools.set_model_response_tool import SetModelResponseTool
3839
from google.genai import types
3940
from pydantic import alias_generators
4041
import pytest
@@ -532,7 +533,7 @@ def test_agent_replay(agent_dir, test_file, monkeypatch):
532533
for part in parts:
533534
if "functionResponse" in part:
534535
func_resp = part["functionResponse"]
535-
if func_resp.get("name") == "set_model_response":
536+
if func_resp.get("name") == SetModelResponseTool.NAME:
536537
last_was_set_model_response = True
537538

538539
elif role == "model":

‎src/google/adk/flows/llm_flows/prompt/_schema.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def get_structured_model_response(function_response_event: Event) -> str | None:
112112
return None
113113

114114
for func_response in function_response_event.get_function_responses():
115-
if func_response.name == 'set_model_response':
115+
if func_response.name == SetModelResponseTool.NAME:
116116
response = function_response_event.actions.set_model_response
117117
if response is None:
118118
return None

‎src/google/adk/tools/mcp_tool/mcp_tool.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
# part of the ADK public API; consumers flip the env var, not the symbol.
6161
from .._gemini_schema_util import _to_gemini_schema
6262
from ..base_authenticated_tool import BaseAuthenticatedTool
63+
from ..set_model_response_tool import SetModelResponseTool
6364
from ..tool_context import ToolContext
6465
from ..transfer_to_agent_tool import transfer_to_agent
6566
from .mcp_session_manager import _http_debug_var
@@ -81,6 +82,7 @@
8182
REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
8283
REQUEST_INPUT_FUNCTION_CALL_NAME,
8384
transfer_to_agent.__name__,
85+
SetModelResponseTool.NAME,
8486
})
8587

8688
_UNSET = object()

‎src/google/adk/tools/set_model_response_tool.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import types as typing_types
2121
from typing import Any
2222
from typing import cast
23+
from typing import Final
2324
from typing import get_args
2425
from typing import get_origin
2526
from typing import Optional
@@ -112,6 +113,9 @@ def _apply_nested_descriptions(prop: types.Schema, annotation: Any) -> None:
112113
_apply_nested_descriptions(prop, arg)
113114

114115

116+
_SET_MODEL_RESPONSE_TOOL_NAME: Final[str] = 'set_model_response'
117+
118+
115119
class SetModelResponseTool(BaseTool):
116120
"""Internal tool used for output schema workaround.
117121
@@ -120,6 +124,8 @@ class SetModelResponseTool(BaseTool):
120124
provide its final structured response instead of outputting text directly.
121125
"""
122126

127+
NAME: Final[str] = _SET_MODEL_RESPONSE_TOOL_NAME
128+
123129
def __init__(self, output_schema: SchemaType):
124130
"""Initialize the tool with the expected output schema.
125131
@@ -213,9 +219,10 @@ def set_model_response() -> str:
213219
setattr(set_model_response, '__signature__', new_sig)
214220

215221
self.func = set_model_response
222+
self.func.__name__ = self.NAME
216223

217224
super().__init__(
218-
name=self.func.__name__,
225+
name=self.NAME,
219226
description=self.func.__doc__.strip() if self.func.__doc__ else '',
220227
)
221228

‎tests/unittests/tools/mcp_tool/test_mcp_tool.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from google.adk.tools.mcp_tool.mcp_tool import MCPTool
4747
from google.adk.tools.mcp_tool.mcp_tool import ProgressCallbackFactory
4848
from google.adk.tools.mcp_tool.mcp_tool import ProgressFnT
49+
from google.adk.tools.set_model_response_tool import SetModelResponseTool
4950
from google.adk.tools.tool_context import ToolContext
5051
from google.genai.types import FunctionDeclaration
5152
from google.genai.types import GroundingMetadata
@@ -734,6 +735,7 @@ def test_init_with_empty_description(self):
734735
"adk_request_confirmation",
735736
"adk_request_input",
736737
"transfer_to_agent",
738+
SetModelResponseTool.NAME,
737739
],
738740
)
739741
def test_init_reserved_name(self, reserved_name):

‎tests/unittests/tools/mcp_tool/test_mcp_toolset.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from google.adk.tools.mcp_tool.mcp_tool import MCPTool
4848
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
4949
from google.adk.tools.mcp_tool.mcp_toolset import McpToolsetConfig
50+
from google.adk.tools.set_model_response_tool import SetModelResponseTool
5051
from google.adk.tools.tool_configs import ToolArgsConfig
5152
from mcp import StdioServerParameters
5253
from mcp.types import BlobResourceContents
@@ -409,6 +410,7 @@ async def test_get_tools_skips_reserved_names(self):
409410
MockMCPTool("adk_request_credential"),
410411
MockMCPTool("adk_request_confirmation"),
411412
MockMCPTool("adk_request_input"),
413+
MockMCPTool(SetModelResponseTool.NAME),
412414
]
413415
self.mock_session.list_tools = AsyncMock(
414416
return_value=MockListToolsResult(mock_tools)

‎tests/unittests/tools/test_set_model_response_tool.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def test_tool_initialization_simple_schema():
7070

7171
assert tool.output_schema == PersonSchema
7272
assert tool.name == 'set_model_response'
73+
assert tool.name == SetModelResponseTool.NAME
74+
assert tool.func.__name__ == SetModelResponseTool.NAME
7375
assert 'Set your final response' in tool.description
7476
assert tool.func is not None
7577

0 commit comments

Comments
 (0)