77
88from agents import Agent , FunctionTool , ModelBehaviorError , RunContextWrapper , function_tool
99from agents .tool import default_tool_error_function
10+ from agents .tool_context import ToolContext
1011
1112
1213def argless_function () -> str :
@@ -18,11 +19,11 @@ async def test_argless_function():
1819 tool = function_tool (argless_function )
1920 assert tool .name == "argless_function"
2021
21- result = await tool .on_invoke_tool (RunContextWrapper ( None ), "" )
22+ result = await tool .on_invoke_tool (ToolContext ( context = None , tool_call_id = "1" ), "" )
2223 assert result == "ok"
2324
2425
25- def argless_with_context (ctx : RunContextWrapper [str ]) -> str :
26+ def argless_with_context (ctx : ToolContext [str ]) -> str :
2627 return "ok"
2728
2829
@@ -31,11 +32,11 @@ async def test_argless_with_context():
3132 tool = function_tool (argless_with_context )
3233 assert tool .name == "argless_with_context"
3334
34- result = await tool .on_invoke_tool (RunContextWrapper (None ), "" )
35+ result = await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), "" )
3536 assert result == "ok"
3637
3738 # Extra JSON should not raise an error
38- result = await tool .on_invoke_tool (RunContextWrapper (None ), '{"a": 1}' )
39+ result = await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), '{"a": 1}' )
3940 assert result == "ok"
4041
4142
@@ -48,15 +49,15 @@ async def test_simple_function():
4849 tool = function_tool (simple_function , failure_error_function = None )
4950 assert tool .name == "simple_function"
5051
51- result = await tool .on_invoke_tool (RunContextWrapper (None ), '{"a": 1}' )
52+ result = await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), '{"a": 1}' )
5253 assert result == 6
5354
54- result = await tool .on_invoke_tool (RunContextWrapper (None ), '{"a": 1, "b": 2}' )
55+ result = await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), '{"a": 1, "b": 2}' )
5556 assert result == 3
5657
5758 # Missing required argument should raise an error
5859 with pytest .raises (ModelBehaviorError ):
59- await tool .on_invoke_tool (RunContextWrapper (None ), "" )
60+ await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), "" )
6061
6162
6263class Foo (BaseModel ):
@@ -84,7 +85,7 @@ async def test_complex_args_function():
8485 "bar" : Bar (x = "hello" , y = 10 ),
8586 }
8687 )
87- result = await tool .on_invoke_tool (RunContextWrapper (None ), valid_json )
88+ result = await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), valid_json )
8889 assert result == "6 hello10 hello"
8990
9091 valid_json = json .dumps (
@@ -93,7 +94,7 @@ async def test_complex_args_function():
9394 "bar" : Bar (x = "hello" , y = 10 ),
9495 }
9596 )
96- result = await tool .on_invoke_tool (RunContextWrapper (None ), valid_json )
97+ result = await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), valid_json )
9798 assert result == "3 hello10 hello"
9899
99100 valid_json = json .dumps (
@@ -103,12 +104,12 @@ async def test_complex_args_function():
103104 "baz" : "world" ,
104105 }
105106 )
106- result = await tool .on_invoke_tool (RunContextWrapper (None ), valid_json )
107+ result = await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), valid_json )
107108 assert result == "3 hello10 world"
108109
109110 # Missing required argument should raise an error
110111 with pytest .raises (ModelBehaviorError ):
111- await tool .on_invoke_tool (RunContextWrapper (None ), '{"foo": {"a": 1}}' )
112+ await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), '{"foo": {"a": 1}}' )
112113
113114
114115def test_function_config_overrides ():
@@ -168,7 +169,7 @@ async def run_function(ctx: RunContextWrapper[Any], args: str) -> str:
168169 assert tool .params_json_schema [key ] == value
169170 assert tool .strict_json_schema
170171
171- result = await tool .on_invoke_tool (RunContextWrapper (None ), '{"data": "hello"}' )
172+ result = await tool .on_invoke_tool (ToolContext (None , tool_call_id = "1" ), '{"data": "hello"}' )
172173 assert result == "hello_done"
173174
174175 tool_not_strict = FunctionTool (
@@ -183,7 +184,7 @@ async def run_function(ctx: RunContextWrapper[Any], args: str) -> str:
183184 assert "additionalProperties" not in tool_not_strict .params_json_schema
184185
185186 result = await tool_not_strict .on_invoke_tool (
186- RunContextWrapper (None ), '{"data": "hello", "bar": "baz"}'
187+ ToolContext (None , tool_call_id = "1" ), '{"data": "hello", "bar": "baz"}'
187188 )
188189 assert result == "hello_done"
189190
@@ -194,7 +195,7 @@ def my_func(a: int, b: int = 5):
194195 raise ValueError ("test" )
195196
196197 tool = function_tool (my_func )
197- ctx = RunContextWrapper (None )
198+ ctx = ToolContext (None , tool_call_id = "1" )
198199
199200 result = await tool .on_invoke_tool (ctx , "" )
200201 assert "Invalid JSON" in str (result )
@@ -218,7 +219,7 @@ def custom_sync_error_function(ctx: RunContextWrapper[Any], error: Exception) ->
218219 return f"error_{ error .__class__ .__name__ } "
219220
220221 tool = function_tool (my_func , failure_error_function = custom_sync_error_function )
221- ctx = RunContextWrapper (None )
222+ ctx = ToolContext (None , tool_call_id = "1" )
222223
223224 result = await tool .on_invoke_tool (ctx , "" )
224225 assert result == "error_ModelBehaviorError"
@@ -242,7 +243,7 @@ def custom_sync_error_function(ctx: RunContextWrapper[Any], error: Exception) ->
242243 return f"error_{ error .__class__ .__name__ } "
243244
244245 tool = function_tool (my_func , failure_error_function = custom_sync_error_function )
245- ctx = RunContextWrapper (None )
246+ ctx = ToolContext (None , tool_call_id = "1" )
246247
247248 result = await tool .on_invoke_tool (ctx , "" )
248249 assert result == "error_ModelBehaviorError"
0 commit comments