diff --git a/src/agents/tool.py b/src/agents/tool.py index 968928b792..974115f1aa 100644 --- a/src/agents/tool.py +++ b/src/agents/tool.py @@ -401,7 +401,9 @@ def __post_init__(self): if callable(bind_to_function_tool): self.on_invoke_tool = bind_to_function_tool(self) if self.strict_json_schema: - self.params_json_schema = ensure_strict_json_schema(self.params_json_schema) + self.params_json_schema = ensure_strict_json_schema( + copy.deepcopy(self.params_json_schema) + ) _validate_function_tool_timeout_config(self) def __copy__(self) -> FunctionTool: diff --git a/tests/test_function_tool.py b/tests/test_function_tool.py index 300d1ab3b9..bf410971e3 100644 --- a/tests/test_function_tool.py +++ b/tests/test_function_tool.py @@ -732,6 +732,27 @@ def echo(value: str) -> str: ) +def test_function_tool_does_not_mutate_params_json_schema() -> None: + async def noop(ctx: ToolContext[Any], input: str) -> str: + return "" + + schema = {"type": "object", "properties": {"x": {"type": "string"}}} + schema_snapshot = copy.deepcopy(schema) + + tool = FunctionTool( + name="t", + description="d", + params_json_schema=schema, + on_invoke_tool=noop, + strict_json_schema=True, + ) + + assert schema == schema_snapshot + assert tool.params_json_schema is not schema + assert tool.params_json_schema["additionalProperties"] is False + assert tool.params_json_schema["required"] == ["x"] + + @pytest.mark.asyncio async def test_default_failure_error_function_survives_deepcopy() -> None: def boom() -> None: