Skip to content

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

fix(tool): omit injected state from inferred function schemas (#3090)

quifoxf0177cf7d2a4
diff --git a/src/agentscope/tool/_utils.py b/src/agentscope/tool/_utils.py
index bb364c031..672700d88 100644
--- a/src/agentscope/tool/_utils.py
+++ b/src/agentscope/tool/_utils.py
@@ -111,8 +111,8 @@ def _extract_input_schema(
# Create a dynamic model with the function signature
fields = {}
for name, param in inspect.signature(tool_func).parameters.items():
- # Skip the `self` and `cls` parameters
- if name in ["self", "cls"]:
+ # Skip `self`, `cls` and the toolkit-injected `_agent_state`
+ if name in ["self", "cls", "_agent_state"]:
continue
annotation = type_hints.get(name, param.annotation)
diff --git a/tests/toolkit_test.py b/tests/toolkit_test.py
index 0865310e1..32a4b0a8b 100644
--- a/tests/toolkit_test.py
+++ b/tests/toolkit_test.py
@@ -1217,6 +1217,65 @@ class RegisterFunctionTest(IsolatedAsyncioTestCase):
f"started{expected_dict_text}",
)
+ async def test_state_injected_function(self) -> None:
+ """Infer business parameters and inject state at invocation."""
+
+ def echo(value: str, _agent_state: AgentState) -> str:
+ """Return a value with the current session.
+
+ Args:
+ value: The value to return.
+ _agent_state: The agent state injected by the toolkit.
+ """
+ return f"{value}:{_agent_state.session_id}"
+
+ tool = FunctionTool(echo, is_state_injected=True)
+ self.assertDictEqual(
+ tool.input_schema,
+ {
+ "type": "object",
+ "properties": {
+ "value": {
+ "type": "string",
+ "description": "The value to return.",
+ },
+ },
+ "required": ["value"],
+ },
+ )
+
+ state = AgentState(session_id="test-session")
+ toolkit = Toolkit(tools=[tool])
+ response = None
+ async for result in toolkit.call_tool(
+ ToolCallBlock(
+ id="test_state_injection",
+ name="echo",
+ input=json.dumps({"value": "hello"}),
+ ),
+ state,
+ ):
+ if isinstance(result, ToolResponse):
+ response = result
+
+ self.assertDictEqual(
+ response.model_dump(),
+ {
+ "content": [
+ {
+ "type": "text",
+ "created_at": AnyString(),
+ "finished_at": None,
+ "id": AnyString(),
+ "text": "hello:test-session",
+ },
+ ],
+ "state": "success",
+ "metadata": {},
+ "id": "test_state_injection",
+ },
+ )
+
async def test_custom_input_schema(self) -> None:
"""Test overriding the auto-extracted schema with a custom one."""