forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_process_model_response.py
More file actions
868 lines (763 loc) · 28.8 KB
/
Copy pathtest_process_model_response.py
File metadata and controls
868 lines (763 loc) · 28.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
from typing import Any, cast
import pytest
from mcp import Tool as MCPTool
from openai._models import construct_type
from openai.types.responses import (
ResponseApplyPatchToolCall,
ResponseCompactionItem,
ResponseCustomToolCall,
ResponseFunctionShellToolCall,
ResponseFunctionShellToolCallOutput,
ResponseFunctionToolCall,
ResponseOutputItem,
ResponseToolSearchCall,
ResponseToolSearchOutputItem,
)
from openai.types.responses.response_output_item import McpCall, McpListTools, McpListToolsTool
from agents import (
Agent,
ApplyPatchTool,
CompactionItem,
CustomTool,
Handoff,
HostedMCPTool,
ShellTool,
Tool,
function_tool,
handoff,
tool_namespace,
)
from agents.exceptions import ModelBehaviorError, UserError
from agents.items import (
HandoffCallItem,
MCPListToolsItem,
ModelResponse,
ToolCallItem,
ToolCallOutputItem,
ToolSearchCallItem,
ToolSearchOutputItem,
)
from agents.mcp.util import MCPUtil
from agents.run_internal import run_loop
from agents.usage import Usage
from tests.fake_model import FakeModel
from tests.mcp.helpers import FakeMCPServer
from tests.test_responses import get_function_tool_call
from tests.utils.hitl import (
RecordingEditor,
make_apply_patch_dict,
make_shell_call,
)
def _response(output: list[object]) -> ModelResponse:
response = ModelResponse(output=[], usage=Usage(), response_id="resp")
response.output = output # type: ignore[assignment]
return response
def _make_hosted_mcp_list_tools(server_label: str, tool_name: str) -> McpListTools:
return McpListTools(
id=f"list_{server_label}",
server_label=server_label,
tools=[
McpListToolsTool(
name=tool_name,
input_schema={},
description="Search the docs.",
annotations={"title": "Search Docs"},
)
],
type="mcp_list_tools",
)
def test_process_model_response_shell_call_without_tool_raises() -> None:
agent = Agent(name="no-shell", model=FakeModel())
shell_call = make_shell_call("shell-1")
with pytest.raises(ModelBehaviorError, match="shell tool"):
run_loop.process_model_response(
agent=agent,
all_tools=[],
response=_response([shell_call]),
output_schema=None,
handoffs=[],
)
def test_process_model_response_sets_title_for_local_mcp_function_tool() -> None:
agent = Agent(name="local-mcp", model=FakeModel())
mcp_tool = MCPTool(name="search_docs", inputSchema={}, description=None, title="Search Docs")
function_tool = MCPUtil.to_function_tool(
mcp_tool,
FakeMCPServer(),
convert_schemas_to_strict=False,
)
tool_call = ResponseFunctionToolCall(
type="function_call",
name="search_docs",
call_id="call_search_docs",
status="completed",
arguments="{}",
)
processed = run_loop.process_model_response(
agent=agent,
all_tools=[function_tool],
response=_response([tool_call]),
output_schema=None,
handoffs=[],
)
assert len(processed.new_items) == 1
item = processed.new_items[0]
assert isinstance(item, ToolCallItem)
assert item.description == "Search Docs"
assert item.title == "Search Docs"
def test_process_model_response_uses_mcp_list_tools_metadata_for_hosted_mcp_calls() -> None:
agent = Agent(name="hosted-mcp", model=FakeModel())
hosted_tool = HostedMCPTool(
tool_config=cast(
Any,
{
"type": "mcp",
"server_label": "docs_server",
"server_url": "https://example.com/mcp",
},
)
)
existing_items = [
MCPListToolsItem(
agent=agent,
raw_item=_make_hosted_mcp_list_tools("docs_server", "search_docs"),
)
]
mcp_call = McpCall(
id="mcp_call_1",
arguments="{}",
name="search_docs",
server_label="docs_server",
type="mcp_call",
status="completed",
)
processed = run_loop.process_model_response(
agent=agent,
all_tools=[hosted_tool],
response=_response([mcp_call]),
output_schema=None,
handoffs=[],
existing_items=existing_items,
)
assert len(processed.new_items) == 1
item = processed.new_items[0]
assert isinstance(item, ToolCallItem)
assert item.description == "Search the docs."
assert item.title == "Search Docs"
def test_process_model_response_skips_local_shell_execution_for_hosted_environment() -> None:
shell_tool = ShellTool(environment={"type": "container_auto"})
agent = Agent(name="hosted-shell", model=FakeModel(), tools=[shell_tool])
shell_call = make_shell_call("shell-hosted-1")
processed = run_loop.process_model_response(
agent=agent,
all_tools=[shell_tool],
response=_response([shell_call]),
output_schema=None,
handoffs=[],
)
assert len(processed.new_items) == 1
assert isinstance(processed.new_items[0], ToolCallItem)
assert processed.shell_calls == []
assert processed.tools_used == ["shell"]
def test_process_model_response_sanitizes_shell_call_model_object() -> None:
shell_call = ResponseFunctionShellToolCall(
type="shell_call",
id="sh_call_2",
call_id="call_shell_2",
status="completed",
created_by="server",
action=cast(Any, {"commands": ["echo hi"], "timeout_ms": 1000}),
)
shell_tool = ShellTool(environment={"type": "container_auto"})
agent = Agent(name="hosted-shell-model", model=FakeModel(), tools=[shell_tool])
processed = run_loop.process_model_response(
agent=agent,
all_tools=[shell_tool],
response=_response([shell_call]),
output_schema=None,
handoffs=[],
)
assert len(processed.new_items) == 1
item = processed.new_items[0]
assert isinstance(item, ToolCallItem)
assert isinstance(item.raw_item, dict)
assert item.raw_item["type"] == "shell_call"
assert "created_by" not in item.raw_item
next_input = item.to_input_item()
assert isinstance(next_input, dict)
assert next_input["type"] == "shell_call"
assert "created_by" not in next_input
assert processed.shell_calls == []
assert processed.tools_used == ["shell"]
def test_process_model_response_preserves_shell_call_output() -> None:
shell_output = {
"type": "shell_call_output",
"id": "sh_out_1",
"call_id": "call_shell_1",
"status": "completed",
"max_output_length": 1000,
"output": [
{
"stdout": "ok\n",
"stderr": "",
"outcome": {"type": "exit", "exit_code": 0},
}
],
}
agent = Agent(name="shell-output", model=FakeModel())
processed = run_loop.process_model_response(
agent=agent,
all_tools=[],
response=_response([shell_output]),
output_schema=None,
handoffs=[],
)
assert len(processed.new_items) == 1
assert isinstance(processed.new_items[0], ToolCallOutputItem)
assert processed.new_items[0].raw_item == shell_output
assert processed.tools_used == ["shell"]
assert processed.shell_calls == []
def test_process_model_response_sanitizes_shell_call_output_model_object() -> None:
shell_output = ResponseFunctionShellToolCallOutput(
type="shell_call_output",
id="sh_out_2",
call_id="call_shell_2",
status="completed",
created_by="server",
output=cast(
Any,
[
{
"stdout": "ok\n",
"stderr": "",
"outcome": {"type": "exit", "exit_code": 0},
"created_by": "server",
}
],
),
)
agent = Agent(name="shell-output-model", model=FakeModel())
processed = run_loop.process_model_response(
agent=agent,
all_tools=[],
response=_response([shell_output]),
output_schema=None,
handoffs=[],
)
assert len(processed.new_items) == 1
item = processed.new_items[0]
assert isinstance(item, ToolCallOutputItem)
assert isinstance(item.raw_item, dict)
assert item.raw_item["type"] == "shell_call_output"
assert "created_by" not in item.raw_item
shell_outputs = item.raw_item.get("output")
assert isinstance(shell_outputs, list)
assert isinstance(shell_outputs[0], dict)
assert "created_by" not in shell_outputs[0]
next_input = item.to_input_item()
assert isinstance(next_input, dict)
assert next_input["type"] == "shell_call_output"
assert "status" not in next_input
assert "created_by" not in next_input
next_outputs = next_input.get("output")
assert isinstance(next_outputs, list)
assert isinstance(next_outputs[0], dict)
assert "created_by" not in next_outputs[0]
assert processed.tools_used == ["shell"]
def test_process_model_response_apply_patch_call_without_tool_raises() -> None:
agent = Agent(name="no-apply", model=FakeModel())
apply_patch_call = make_apply_patch_dict("apply-1", diff="-old\n+new\n")
with pytest.raises(ModelBehaviorError, match="apply_patch tool"):
run_loop.process_model_response(
agent=agent,
all_tools=[],
response=_response([apply_patch_call]),
output_schema=None,
handoffs=[],
)
def test_process_model_response_sanitizes_apply_patch_call_model_object() -> None:
editor = RecordingEditor()
apply_patch_tool = ApplyPatchTool(editor=editor)
agent = Agent(name="apply-agent-model", model=FakeModel(), tools=[apply_patch_tool])
apply_patch_call = ResponseApplyPatchToolCall(
type="apply_patch_call",
id="ap_call_1",
call_id="call_apply_1",
status="completed",
created_by="server",
operation=cast(
Any,
{"type": "update_file", "path": "test.md", "diff": "-old\n+new\n"},
),
)
processed = run_loop.process_model_response(
agent=agent,
all_tools=[apply_patch_tool],
response=_response([apply_patch_call]),
output_schema=None,
handoffs=[],
)
assert len(processed.new_items) == 1
item = processed.new_items[0]
assert isinstance(item, ToolCallItem)
assert isinstance(item.raw_item, dict)
assert item.raw_item["type"] == "apply_patch_call"
assert "created_by" not in item.raw_item
next_input = item.to_input_item()
assert isinstance(next_input, dict)
assert next_input["type"] == "apply_patch_call"
assert "created_by" not in next_input
assert len(processed.apply_patch_calls) == 1
queued_call = processed.apply_patch_calls[0].tool_call
assert isinstance(queued_call, dict)
assert queued_call["type"] == "apply_patch_call"
assert "created_by" not in queued_call
assert processed.tools_used == [apply_patch_tool.name]
def test_process_model_response_queues_apply_patch_call() -> None:
editor = RecordingEditor()
apply_patch_tool = ApplyPatchTool(editor=editor)
agent = Agent(name="apply-agent", model=FakeModel(), tools=[apply_patch_tool])
apply_patch_call = make_apply_patch_dict("apply-1")
processed = run_loop.process_model_response(
agent=agent,
all_tools=[apply_patch_tool],
response=_response([apply_patch_call]),
output_schema=None,
handoffs=[],
)
assert processed.apply_patch_calls, "apply_patch call should be queued"
converted_call = processed.apply_patch_calls[0].tool_call
assert isinstance(converted_call, dict)
assert converted_call.get("type") == "apply_patch_call"
def test_process_model_response_queues_hosted_apply_patch_from_custom_tool_call() -> None:
editor = RecordingEditor()
apply_patch_tool = ApplyPatchTool(editor=editor)
agent = Agent(name="apply-agent-custom", model=FakeModel(), tools=[apply_patch_tool])
custom_call = ResponseCustomToolCall(
type="custom_tool_call",
name="apply_patch",
call_id="custom-apply-1",
input='{"type":"update_file","path":"test.md","diff":"-old\\n+new\\n"}',
)
processed = run_loop.process_model_response(
agent=agent,
all_tools=[apply_patch_tool],
response=_response([custom_call]),
output_schema=None,
handoffs=[],
)
assert len(processed.new_items) == 1
item = processed.new_items[0]
assert isinstance(item, ToolCallItem)
assert isinstance(item.raw_item, dict)
assert item.raw_item["type"] == "apply_patch_call"
assert processed.apply_patch_calls, "apply_patch call should be queued"
converted_call = processed.apply_patch_calls[0].tool_call
assert isinstance(converted_call, dict)
assert converted_call["type"] == "apply_patch_call"
assert converted_call["operation"]["type"] == "update_file"
assert processed.tools_used == [apply_patch_tool.name]
def test_process_model_response_queues_custom_tool_call_for_custom_tool() -> None:
custom_tool = CustomTool(
name="raw_editor",
description="Edit raw text.",
on_invoke_tool=lambda _ctx, raw_input: raw_input,
format={"type": "text"},
)
agent = Agent(name="custom-agent", model=FakeModel(), tools=[custom_tool])
custom_call = ResponseCustomToolCall(
type="custom_tool_call",
name="raw_editor",
call_id="custom-apply-1",
input="-old\n+new\n",
)
processed = run_loop.process_model_response(
agent=agent,
all_tools=[custom_tool],
response=_response([custom_call]),
output_schema=None,
handoffs=[],
)
item = processed.new_items[0]
assert isinstance(item, ToolCallItem)
assert cast(object, item.raw_item) is custom_call
assert processed.apply_patch_calls == []
assert processed.custom_tool_calls[0].tool_call is custom_call
assert processed.custom_tool_calls[0].custom_tool is custom_tool
def test_process_model_response_prefers_namespaced_function_over_apply_patch_fallback() -> None:
namespaced_tool = tool_namespace(
name="billing",
description="Billing tools",
tools=[function_tool(lambda payload: payload, name_override="apply_patch_lookup")],
)[0]
all_tools: list[Tool] = [namespaced_tool]
agent = Agent(name="billing-agent", model=FakeModel(), tools=all_tools)
processed = run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[
get_function_tool_call(
"apply_patch_lookup",
'{"payload":"value"}',
namespace="billing",
)
]
),
output_schema=None,
handoffs=[],
)
assert len(processed.functions) == 1
assert processed.functions[0].function_tool is namespaced_tool
assert processed.apply_patch_calls == []
def test_process_model_response_handles_compaction_item() -> None:
agent = Agent(name="compaction-agent", model=FakeModel())
compaction_item = ResponseCompactionItem(
id="comp-1",
encrypted_content="enc",
type="compaction",
created_by="server",
)
processed = run_loop.process_model_response(
agent=agent,
all_tools=[],
response=_response([compaction_item]),
output_schema=None,
handoffs=[],
)
assert len(processed.new_items) == 1
item = processed.new_items[0]
assert isinstance(item, CompactionItem)
assert isinstance(item.raw_item, dict)
assert item.raw_item["type"] == "compaction"
assert item.raw_item["encrypted_content"] == "enc"
assert "created_by" not in item.raw_item
def test_process_model_response_classifies_tool_search_items() -> None:
agent = Agent(name="tool-search-agent", model=FakeModel())
tool_search_call = construct_type(
type_=ResponseOutputItem,
value={
"id": "tsc_123",
"type": "tool_search_call",
"arguments": {"paths": ["crm"], "query": "profile"},
"execution": "server",
"status": "completed",
},
)
tool_search_output = construct_type(
type_=ResponseOutputItem,
value={
"id": "tso_123",
"type": "tool_search_output",
"execution": "server",
"status": "completed",
"tools": [
{
"type": "function",
"name": "get_customer_profile",
"description": "Fetch a CRM customer profile.",
"parameters": {
"type": "object",
"properties": {
"customer_id": {
"type": "string",
}
},
"required": ["customer_id"],
},
"defer_loading": True,
}
],
},
)
processed = run_loop.process_model_response(
agent=agent,
all_tools=[],
response=_response([tool_search_call, tool_search_output]),
output_schema=None,
handoffs=[],
)
assert isinstance(processed.new_items[0], ToolSearchCallItem)
assert isinstance(processed.new_items[0].raw_item, ResponseToolSearchCall)
assert isinstance(processed.new_items[1], ToolSearchOutputItem)
assert isinstance(processed.new_items[1].raw_item, ResponseToolSearchOutputItem)
assert processed.tools_used == ["tool_search", "tool_search"]
def test_process_model_response_uses_namespace_for_duplicate_function_names() -> None:
crm_tool = function_tool(lambda customer_id: customer_id, name_override="lookup_account")
billing_tool = function_tool(lambda customer_id: customer_id, name_override="lookup_account")
crm_namespace = tool_namespace(
name="crm",
description="CRM tools",
tools=[crm_tool],
)
billing_namespace = tool_namespace(
name="billing",
description="Billing tools",
tools=[billing_tool],
)
all_tools: list[Tool] = [*crm_namespace, *billing_namespace]
agent = Agent(name="billing-agent", model=FakeModel(), tools=all_tools)
processed = run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[
get_function_tool_call(
"lookup_account",
'{"customer_id":"customer_42"}',
namespace="billing",
)
]
),
output_schema=None,
handoffs=[],
)
assert len(processed.functions) == 1
assert processed.functions[0].function_tool is billing_namespace[0]
assert processed.tools_used == ["billing.lookup_account"]
def test_process_model_response_collapses_synthetic_deferred_namespace_in_tools_used() -> None:
deferred_tool = function_tool(
lambda city: city,
name_override="get_weather",
defer_loading=True,
)
agent = Agent(name="weather-agent", model=FakeModel(), tools=[deferred_tool])
processed = run_loop.process_model_response(
agent=agent,
all_tools=[deferred_tool],
response=_response(
[
get_function_tool_call(
"get_weather",
'{"city":"Tokyo"}',
namespace="get_weather",
)
]
),
output_schema=None,
handoffs=[],
)
assert len(processed.functions) == 1
assert processed.functions[0].function_tool is deferred_tool
assert processed.tools_used == ["get_weather"]
def test_process_model_response_rejects_bare_name_for_duplicate_namespaced_functions() -> None:
crm_tool = function_tool(lambda customer_id: customer_id, name_override="lookup_account")
billing_tool = function_tool(lambda customer_id: customer_id, name_override="lookup_account")
crm_namespace = tool_namespace(
name="crm",
description="CRM tools",
tools=[crm_tool],
)
billing_namespace = tool_namespace(
name="billing",
description="Billing tools",
tools=[billing_tool],
)
all_tools: list[Tool] = [*crm_namespace, *billing_namespace]
agent = Agent(name="billing-agent", model=FakeModel(), tools=all_tools)
with pytest.raises(ModelBehaviorError, match="Tool lookup_account not found"):
run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[get_function_tool_call("lookup_account", '{"customer_id":"customer_42"}')]
),
output_schema=None,
handoffs=[],
)
def test_process_model_response_uses_last_duplicate_top_level_function() -> None:
first_tool = function_tool(lambda customer_id: f"first:{customer_id}", name_override="lookup")
second_tool = function_tool(lambda customer_id: f"second:{customer_id}", name_override="lookup")
all_tools: list[Tool] = [first_tool, second_tool]
agent = Agent(name="lookup-agent", model=FakeModel(), tools=all_tools)
processed = run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response([get_function_tool_call("lookup", '{"customer_id":"customer_42"}')]),
output_schema=None,
handoffs=[],
)
assert len(processed.functions) == 1
assert processed.functions[0].function_tool is second_tool
def test_process_model_response_rejects_reserved_same_name_namespace_shape() -> None:
invalid_tool = function_tool(lambda customer_id: customer_id, name_override="lookup_account")
invalid_tool._tool_namespace = "lookup_account"
invalid_tool._tool_namespace_description = "Same-name namespace"
all_tools: list[Tool] = [invalid_tool]
agent = Agent(name="lookup-agent", model=FakeModel(), tools=all_tools)
with pytest.raises(UserError, match="synthetic namespace `lookup_account.lookup_account`"):
run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[
get_function_tool_call(
"lookup_account",
'{"customer_id":"customer_42"}',
namespace="lookup_account",
)
]
),
output_schema=None,
handoffs=[],
)
def test_process_model_response_rejects_qualified_name_collision_with_dotted_top_level_tool() -> (
None
):
dotted_top_level_tool = function_tool(
lambda customer_id: customer_id,
name_override="crm.lookup_account",
)
namespaced_tool = tool_namespace(
name="crm",
description="CRM tools",
tools=[function_tool(lambda customer_id: customer_id, name_override="lookup_account")],
)[0]
all_tools: list[Tool] = [dotted_top_level_tool, namespaced_tool]
agent = Agent(name="lookup-agent", model=FakeModel(), tools=all_tools)
with pytest.raises(UserError, match="qualified name `crm.lookup_account`"):
run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[
get_function_tool_call(
"lookup_account",
'{"customer_id":"customer_42"}',
namespace="crm",
)
]
),
output_schema=None,
handoffs=[],
)
def test_process_model_response_prefers_visible_top_level_function_over_deferred_same_name_tool():
visible_tool = function_tool(
lambda customer_id: f"visible:{customer_id}",
name_override="lookup_account",
)
deferred_tool = function_tool(
lambda customer_id: f"deferred:{customer_id}",
name_override="lookup_account",
defer_loading=True,
)
all_tools: list[Tool] = [visible_tool, deferred_tool]
agent = Agent(name="lookup-agent", model=FakeModel(), tools=all_tools)
processed = run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[get_function_tool_call("lookup_account", '{"customer_id":"customer_42"}')]
),
output_schema=None,
handoffs=[],
)
assert len(processed.functions) == 1
assert processed.functions[0].function_tool is visible_tool
assert getattr(processed.functions[0].tool_call, "namespace", None) is None
assert isinstance(processed.new_items[0], ToolCallItem)
assert getattr(processed.new_items[0].raw_item, "namespace", None) is None
def test_process_model_response_uses_internal_lookup_key_for_deferred_top_level_calls() -> None:
visible_tool = function_tool(
lambda customer_id: f"visible:{customer_id}",
name_override="lookup_account.lookup_account",
)
deferred_tool = function_tool(
lambda customer_id: f"deferred:{customer_id}",
name_override="lookup_account",
defer_loading=True,
)
all_tools: list[Tool] = [visible_tool, deferred_tool]
agent = Agent(name="lookup-agent", model=FakeModel(), tools=all_tools)
processed = run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[
get_function_tool_call(
"lookup_account",
'{"customer_id":"customer_42"}',
namespace="lookup_account",
)
]
),
output_schema=None,
handoffs=[],
)
assert len(processed.functions) == 1
assert processed.functions[0].function_tool is deferred_tool
def test_process_model_response_preserves_synthetic_namespace_for_deferred_top_level_tools() -> (
None
):
deferred_tool = function_tool(
lambda city: city,
name_override="get_weather",
defer_loading=True,
)
all_tools: list[Tool] = [deferred_tool]
agent = Agent(name="weather-agent", model=FakeModel(), tools=all_tools)
processed = run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[get_function_tool_call("get_weather", '{"city":"Tokyo"}', namespace="get_weather")]
),
output_schema=None,
handoffs=[],
)
assert len(processed.functions) == 1
assert processed.functions[0].function_tool is deferred_tool
assert getattr(processed.functions[0].tool_call, "namespace", None) == "get_weather"
assert isinstance(processed.new_items[0], ToolCallItem)
assert getattr(processed.new_items[0].raw_item, "namespace", None) == "get_weather"
def test_process_model_response_prefers_namespaced_function_over_handoff_name_collision() -> None:
billing_tool = function_tool(lambda customer_id: customer_id, name_override="lookup_account")
billing_namespace = tool_namespace(
name="billing",
description="Billing tools",
tools=[billing_tool],
)
handoff_target = Agent(name="lookup-agent", model=FakeModel())
lookup_handoff: Handoff = handoff(handoff_target, tool_name_override="lookup_account")
all_tools: list[Tool] = [*billing_namespace]
agent = Agent(name="billing-agent", model=FakeModel(), tools=all_tools)
processed = run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[
get_function_tool_call(
"lookup_account",
'{"customer_id":"customer_42"}',
namespace="billing",
)
]
),
output_schema=None,
handoffs=[lookup_handoff],
)
assert len(processed.functions) == 1
assert processed.functions[0].function_tool is billing_namespace[0]
assert processed.handoffs == []
assert len(processed.new_items) == 1
assert isinstance(processed.new_items[0], ToolCallItem)
assert not isinstance(processed.new_items[0], HandoffCallItem)
def test_process_model_response_rejects_mismatched_function_namespace() -> None:
bare_tool = function_tool(lambda customer_id: customer_id, name_override="lookup_account")
all_tools: list[Tool] = [bare_tool]
agent = Agent(name="bare-agent", model=FakeModel(), tools=all_tools)
with pytest.raises(ModelBehaviorError, match="crm.lookup_account"):
run_loop.process_model_response(
agent=agent,
all_tools=all_tools,
response=_response(
[
get_function_tool_call(
"lookup_account",
'{"customer_id":"customer_42"}',
namespace="crm",
)
]
),
output_schema=None,
handoffs=[],
)