forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_programmatic_tool_calling.py
More file actions
2763 lines (2377 loc) · 90.6 KB
/
Copy pathtest_programmatic_tool_calling.py
File metadata and controls
2763 lines (2377 loc) · 90.6 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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import asyncio
import json
import sys
from collections.abc import Awaitable, Coroutine
from dataclasses import dataclass
from typing import Annotated, Any, Literal, cast
import pytest
from openai.types.responses import (
ResponseApplyPatchToolCall,
ResponseCustomToolCall,
ResponseFileSearchToolCall,
ResponseFunctionShellToolCall,
ResponseFunctionShellToolCallOutput,
ResponseFunctionToolCall,
ResponseFunctionWebSearch,
ResponseToolSearchCall,
ResponseToolSearchOutputItem,
)
from openai.types.responses.response_apply_patch_tool_call import OperationCreateFile
from openai.types.responses.response_code_interpreter_tool_call import (
ResponseCodeInterpreterToolCall,
)
from openai.types.responses.response_function_shell_tool_call import Action
from openai.types.responses.response_function_tool_call import CallerProgram
from openai.types.responses.response_function_web_search import ActionSearch
from openai.types.responses.response_output_item import (
ImageGenerationCall,
McpApprovalRequest,
McpCall,
McpListTools,
Program,
ProgramOutput,
)
from pydantic import BaseModel, ConfigDict, Field
from typing_extensions import TypedDict
from agents import (
Agent,
ApplyPatchTool,
CodeInterpreterTool,
CustomTool,
HostedMCPTool,
ModelResponse,
ModelSettings,
ProgrammaticToolCallingTool,
RunConfig,
RunItem,
Runner,
RunState,
ShellTool,
ToolCallItem,
ToolCallOutputItem,
ToolExecutionConfig,
ToolGuardrailFunctionOutput,
ToolInputGuardrailData,
ToolOutputGuardrailData,
ToolSearchTool,
Usage,
UserError,
function_tool,
tool_input_guardrail,
tool_output_guardrail,
)
from agents.exceptions import ModelBehaviorError
from agents.items import ItemHelpers
from agents.memory import SQLiteSession
from agents.models.chatcmpl_converter import Converter as ChatCompletionsConverter
from agents.models.openai_responses import Converter as ResponsesConverter
from agents.run_internal.turn_resolution import process_model_response
from agents.tool_context import ToolContext
from .fake_model import FakeModel
from .test_responses import get_handoff_tool_call, get_text_message
PROGRAM_CALL_ID = "call_program"
FUNCTION_CALL_ID = "call_lookup"
PROGRAM_CALLER = {"type": "program", "caller_id": PROGRAM_CALL_ID}
class InventoryOutput(BaseModel):
sku: str
available_units: int
class InventoryAwaitable(Awaitable[InventoryOutput]):
def __await__(self) -> Any:
raise NotImplementedError
class InventoryDict(TypedDict):
sku: str
available_units: int
@dataclass
class InventoryData:
sku: str
available_units: int
class AliasedInventoryOutput(BaseModel):
model_config = ConfigDict(populate_by_name=True)
sku: str
available_units: int = Field(
validation_alias="inputUnits",
serialization_alias="availableUnits",
)
def _program() -> Program:
return Program(
id="program_item",
call_id=PROGRAM_CALL_ID,
code='lookup_inventory(sku="A-1")',
fingerprint="fingerprint",
type="program",
)
def _function_call() -> ResponseFunctionToolCall:
return ResponseFunctionToolCall(
id="function_item",
call_id=FUNCTION_CALL_ID,
name="lookup_inventory",
arguments='{"sku":"A-1"}',
caller=CallerProgram(type="program", caller_id=PROGRAM_CALL_ID),
type="function_call",
)
def _program_output(
status: Literal["completed", "incomplete"] = "completed",
) -> ProgramOutput:
return ProgramOutput(
id="program_output_item",
call_id=PROGRAM_CALL_ID,
result='{"sku":"A-1","available_units":42}',
status=status,
type="program_output",
)
def _hosted_program_call_and_tool(
output_type: str,
allowed_callers: list[Any] | None,
) -> tuple[Any, Any]:
if output_type in ("mcp_approval_request", "mcp_call", "mcp_list_tools"):
mcp_config: dict[str, Any] = {
"type": "mcp",
"server_label": "docs_server",
"server_url": "https://example.com/mcp",
}
if allowed_callers is not None:
mcp_config["allowed_callers"] = allowed_callers
hosted_tool = HostedMCPTool(tool_config=cast(Any, mcp_config))
if output_type == "mcp_list_tools":
return (
McpListTools.model_construct(
id="mcp_list_tools_1",
server_label="docs_server",
tools=[],
type="mcp_list_tools",
caller=PROGRAM_CALLER,
),
hosted_tool,
)
call_type: Any = McpApprovalRequest if output_type == "mcp_approval_request" else McpCall
output = call_type.model_construct(
id=f"{output_type}_1",
arguments="{}",
name="search_docs",
server_label="docs_server",
type=output_type,
caller=PROGRAM_CALLER,
)
return output, hosted_tool
code_interpreter_config: dict[str, Any] = {
"type": "code_interpreter",
"container": "auto",
}
if allowed_callers is not None:
code_interpreter_config["allowed_callers"] = allowed_callers
return (
ResponseCodeInterpreterToolCall.model_construct(
id="code_interpreter_1",
container_id="container_1",
status="completed",
type="code_interpreter_call",
caller=PROGRAM_CALLER,
),
CodeInterpreterTool(tool_config=cast(Any, code_interpreter_config)),
)
def _caller_dict(value: Any) -> dict[str, str]:
if isinstance(value, dict):
return cast(dict[str, str], value)
return cast(dict[str, str], value.model_dump(exclude_none=True))
def _raw_item_type(value: Any) -> str | None:
if isinstance(value, dict):
item_type = value.get("type")
return item_type if isinstance(item_type, str) else None
item_type = getattr(value, "type", None)
return item_type if isinstance(item_type, str) else None
def _function_output_raw_items(result: Any) -> list[dict[str, Any]]:
return [
cast(dict[str, Any], item.raw_item)
for item in result.new_items
if isinstance(item, ToolCallOutputItem)
and isinstance(item.raw_item, dict)
and item.raw_item.get("type") == "function_call_output"
]
def test_responses_converter_serializes_programmatic_tool_configuration() -> None:
@function_tool(allowed_callers=["programmatic"])
def lookup_inventory(sku: str) -> InventoryOutput:
return InventoryOutput(sku=sku, available_units=42)
converted = ResponsesConverter.convert_tools(
tools=[ProgrammaticToolCallingTool(), lookup_inventory],
handoffs=[],
)
assert converted.tools[0] == {"type": "programmatic_tool_calling"}
function_payload = cast(dict[str, Any], converted.tools[1])
assert function_payload["allowed_callers"] == ["programmatic"]
assert function_payload["output_schema"] == lookup_inventory.output_json_schema
assert function_payload["output_schema"] == {
"additionalProperties": False,
"properties": {
"sku": {"title": "Sku", "type": "string"},
"available_units": {"title": "Available Units", "type": "integer"},
},
"required": ["sku", "available_units"],
"title": "InventoryOutput",
"type": "object",
}
assert ResponsesConverter.convert_tool_choice("programmatic_tool_calling") == {
"type": "programmatic_tool_calling"
}
def test_function_tool_infers_typed_dict_and_dataclass_output_schemas() -> None:
@function_tool(allowed_callers=["programmatic"])
def typed_dict_tool() -> InventoryDict:
return {"sku": "A-1", "available_units": 42}
@function_tool(allowed_callers=["programmatic"])
def dataclass_tool() -> InventoryData:
return InventoryData(sku="A-1", available_units=42)
assert typed_dict_tool.output_json_schema is not None
assert typed_dict_tool.output_json_schema["type"] == "object"
assert typed_dict_tool.output_json_schema["additionalProperties"] is False
assert dataclass_tool.output_json_schema is not None
assert dataclass_tool.output_json_schema["type"] == "object"
assert dataclass_tool.output_json_schema["additionalProperties"] is False
@pytest.mark.parametrize(
"return_annotation",
[
Awaitable[InventoryOutput],
Coroutine[Any, Any, InventoryOutput],
InventoryAwaitable,
Awaitable[InventoryOutput] | InventoryOutput,
Awaitable,
Coroutine,
],
)
def test_sync_callable_does_not_infer_through_awaitable_output(
return_annotation: Any,
) -> None:
class LookupInventory:
def __call__(self) -> Any:
raise AssertionError("The handler must not run during tool construction.")
cast(Any, LookupInventory.__call__).__annotations__["return"] = return_annotation
with pytest.raises(UserError, match="programmatic function tool return annotation"):
function_tool(LookupInventory(), allowed_callers=["programmatic"])
@pytest.mark.skipif(sys.version_info < (3, 12), reason="PEP 695 requires Python 3.12")
def test_sync_callable_does_not_infer_through_pep695_awaitable_alias() -> None:
namespace = {
"Any": Any,
"Awaitable": Awaitable,
"InventoryOutput": InventoryOutput,
}
exec(
"type OutputAwaitable = Awaitable[InventoryOutput]\n"
"class LookupInventory:\n"
" def __call__(self) -> OutputAwaitable:\n"
" raise AssertionError\n",
namespace,
)
with pytest.raises(UserError, match="explicit wrapper function"):
function_tool(namespace["LookupInventory"](), allowed_callers=["programmatic"])
def test_function_tool_treats_annotated_plain_returns_as_untyped() -> None:
@function_tool(allowed_callers=["programmatic"])
def string_tool() -> Annotated[str, "plain string"]:
return "ok"
@function_tool(allowed_callers=["programmatic"])
def any_tool() -> Annotated[Any, "untyped value"]:
return {"status": "ok"}
@function_tool(allowed_callers=["programmatic"])
def none_tool() -> Annotated[None, "no value"]:
return None
assert string_tool.output_json_schema is None
assert string_tool._output_type_adapter is None
assert any_tool.output_json_schema is None
assert any_tool._output_type_adapter is None
assert none_tool.output_json_schema is None
assert none_tool._output_type_adapter is None
def test_function_tool_preserves_annotated_structured_return_metadata() -> None:
@function_tool(allowed_callers=["programmatic"])
def lookup_inventory() -> Annotated[
InventoryOutput,
Field(description="Inventory result"),
]:
return InventoryOutput(sku="A-1", available_units=42)
assert lookup_inventory.output_json_schema is not None
assert lookup_inventory.output_json_schema["description"] == "Inventory result"
assert lookup_inventory._output_type_adapter is not None
def test_function_tool_output_type_override_and_raw_schema_are_mutually_exclusive() -> None:
def unannotated_tool() -> Any:
return {"sku": "A-1", "available_units": 42}
tool = function_tool(
unannotated_tool,
allowed_callers=["programmatic"],
output_type=InventoryOutput,
)
assert tool.output_json_schema is not None
assert tool.output_json_schema["title"] == "InventoryOutput"
with pytest.raises(UserError, match="cannot both be provided"):
function_tool(
unannotated_tool,
allowed_callers=["programmatic"],
output_type=InventoryOutput,
output_json_schema={"type": "object"},
)
with pytest.raises(UserError, match="output_type must define a strict JSON object"):
function_tool(
unannotated_tool,
allowed_callers=["programmatic"],
output_type=str,
)
def test_function_tool_rejects_loose_programmatic_output_annotation() -> None:
def loose_dict_tool() -> dict[str, Any]:
return {"sku": "A-1", "available_units": 42}
with pytest.raises(UserError, match="return annotation must define a strict JSON object"):
function_tool(loose_dict_tool, allowed_callers=["programmatic"])
def test_function_tool_does_not_infer_non_programmatic_output() -> None:
@function_tool
def direct_tool() -> InventoryOutput:
return InventoryOutput(sku="A-1", available_units=42)
assert direct_tool.output_json_schema is None
@pytest.mark.parametrize(
"output_json_schema",
[
{"type": "string"},
{"type": "object", "additionalProperties": True},
],
)
def test_function_tool_rejects_non_object_or_non_strict_raw_output_schema(
output_json_schema: dict[str, Any],
) -> None:
with pytest.raises(UserError, match="output_json_schema must define a.*object schema"):
function_tool(
lambda: "ok",
allowed_callers=["programmatic"],
output_json_schema=output_json_schema,
)
@pytest.mark.asyncio
async def test_function_tool_validates_inferred_output_type() -> None:
@function_tool(allowed_callers=["programmatic"])
def invalid_tool() -> InventoryOutput:
return {"sku": "A-1", "available_units": "many"} # type: ignore[return-value]
context = ToolContext(
None,
tool_name=invalid_tool.name,
tool_call_id="invalid",
tool_arguments="{}",
tool_call=_function_call(),
)
with pytest.raises(UserError, match="does not match its declared output type"):
await invalid_tool.on_invoke_tool(context, "{}")
@pytest.mark.asyncio
async def test_schema_backed_programmatic_tool_bypasses_default_failure_formatter() -> None:
@function_tool(allowed_callers=["programmatic"])
def failing_tool() -> InventoryOutput:
raise RuntimeError("inventory unavailable")
context = ToolContext(
None,
tool_name=failing_tool.name,
tool_call_id="failing",
tool_arguments="{}",
tool_call=_function_call(),
)
with pytest.raises(RuntimeError, match="inventory unavailable"):
await failing_tool.on_invoke_tool(context, "{}")
@function_tool(
allowed_callers=["programmatic"],
output_json_schema={
"type": "object",
"properties": {"error": {"type": "string"}},
"required": ["error"],
"additionalProperties": False,
},
)
def failing_declared_schema_tool() -> str:
raise RuntimeError("declared schema unavailable")
declared_context = ToolContext(
None,
tool_name=failing_declared_schema_tool.name,
tool_call_id="failing-declared",
tool_arguments="{}",
tool_call=_function_call(),
)
with pytest.raises(RuntimeError, match="declared schema unavailable"):
await failing_declared_schema_tool.on_invoke_tool(declared_context, "{}")
@pytest.mark.asyncio
async def test_schema_backed_direct_tool_preserves_argument_error_formatter() -> None:
@function_tool(allowed_callers=["direct", "programmatic"])
def failing_tool(sku: str) -> InventoryOutput:
return InventoryOutput(sku=sku, available_units=42)
direct_call = ResponseFunctionToolCall(
id="function_item",
call_id=FUNCTION_CALL_ID,
name=failing_tool.name,
arguments="{}",
type="function_call",
)
context = ToolContext(
None,
tool_name=failing_tool.name,
tool_call_id=FUNCTION_CALL_ID,
tool_arguments="{}",
tool_call=direct_call,
)
result = await failing_tool.on_invoke_tool(context, "{}")
assert result.startswith("An error occurred while running the tool. Please try again. Error:")
assert "sku" in result
@pytest.mark.asyncio
async def test_runner_preserves_direct_error_for_schema_backed_tool() -> None:
model = FakeModel()
direct_call = ResponseFunctionToolCall(
id="function_item",
call_id=FUNCTION_CALL_ID,
name="lookup_inventory",
arguments='{"sku":"A-1"}',
type="function_call",
)
model.add_multiple_turn_outputs([[direct_call], [get_text_message("inventory lookup failed")]])
@function_tool(allowed_callers=["direct", "programmatic"])
def lookup_inventory(sku: str) -> InventoryOutput:
raise RuntimeError(f"inventory unavailable for {sku}")
result = await Runner.run(
Agent(name="inventory", model=model, tools=[lookup_inventory]),
"Check inventory",
)
function_output = next(
item for item in result.new_items if isinstance(item, ToolCallOutputItem)
)
expected_error = (
"An error occurred while running the tool. Please try again. "
"Error: inventory unavailable for A-1"
)
assert result.final_output == "inventory lookup failed"
assert function_output.output == expected_error
assert cast(dict[str, Any], function_output.raw_item)["output"] == expected_error
@pytest.mark.asyncio
async def test_runner_preserves_direct_default_timeout_for_schema_backed_tool() -> None:
model = FakeModel()
direct_call = ResponseFunctionToolCall(
id="function_item",
call_id=FUNCTION_CALL_ID,
name="lookup_inventory",
arguments='{"sku":"A-1"}',
type="function_call",
)
model.add_multiple_turn_outputs([[direct_call], [get_text_message("timed out")]])
@function_tool(allowed_callers=["direct", "programmatic"], timeout=0.01)
async def lookup_inventory(sku: str) -> InventoryOutput:
await asyncio.sleep(0.2)
return InventoryOutput(sku=sku, available_units=42)
result = await Runner.run(
Agent(name="inventory", model=model, tools=[lookup_inventory]),
"Check inventory",
)
function_output = next(
item for item in result.new_items if isinstance(item, ToolCallOutputItem)
)
assert result.final_output == "timed out"
assert isinstance(function_output.output, str)
assert "timed out" in function_output.output.lower()
assert cast(dict[str, Any], function_output.raw_item)["output"] == function_output.output
@pytest.mark.asyncio
async def test_schema_backed_function_tool_accepts_conforming_custom_error_output() -> None:
@function_tool(
allowed_callers=["programmatic"],
failure_error_function=lambda _context, _error: json.dumps(
{"sku": "ERROR", "available_units": 0}
),
)
def failing_tool() -> InventoryOutput:
raise RuntimeError("inventory unavailable")
context = ToolContext(
None,
tool_name=failing_tool.name,
tool_call_id="failing",
tool_arguments="{}",
)
result = await failing_tool.on_invoke_tool(context, "{}")
output = ItemHelpers.tool_call_output_item(
_function_call(),
result,
output_json_schema=failing_tool.output_json_schema,
output_type_adapter=failing_tool._output_type_adapter,
)
assert json.loads(cast(str, output["output"])) == {
"sku": "ERROR",
"available_units": 0,
}
@pytest.mark.asyncio
async def test_schema_backed_programmatic_tool_accepts_conforming_custom_timeout_output() -> None:
model = FakeModel()
model.add_multiple_turn_outputs(
[
[_program(), _function_call()],
[_program_output(), get_text_message("timeout handled")],
]
)
@function_tool(
allowed_callers=["programmatic"],
timeout=0.01,
timeout_error_function=lambda _context, _error: json.dumps(
{"sku": "TIMEOUT", "available_units": 0}
),
)
async def lookup_inventory(sku: str) -> InventoryOutput:
await asyncio.sleep(0.2)
return InventoryOutput(sku=sku, available_units=42)
result = await Runner.run(
Agent(
name="inventory",
model=model,
tools=[ProgrammaticToolCallingTool(), lookup_inventory],
),
"Check inventory",
)
function_output = next(
item for item in result.new_items if isinstance(item, ToolCallOutputItem)
)
assert result.final_output == "timeout handled"
assert json.loads(cast(str, cast(dict[str, Any], function_output.raw_item)["output"])) == {
"sku": "TIMEOUT",
"available_units": 0,
}
def test_schema_backed_function_output_rejects_plain_error_text() -> None:
@function_tool(allowed_callers=["programmatic"])
def lookup_inventory() -> InventoryOutput:
return InventoryOutput(sku="A-1", available_units=42)
with pytest.raises(UserError, match="does not match its declared output schema"):
ItemHelpers.tool_call_output_item(
_function_call(),
"inventory unavailable",
output_json_schema=lookup_inventory.output_json_schema,
output_type_adapter=lookup_inventory._output_type_adapter,
)
with pytest.raises(UserError, match="requires a JSON object"):
ItemHelpers.tool_call_output_item(
_function_call(),
"inventory unavailable",
output_json_schema={"type": "object"},
)
@pytest.mark.asyncio
async def test_function_tool_serializes_typed_output_with_schema_aliases() -> None:
@function_tool(allowed_callers=["programmatic"])
def aliased_tool() -> AliasedInventoryOutput:
return AliasedInventoryOutput(sku="A-1", available_units=42)
context = ToolContext(
None,
tool_name=aliased_tool.name,
tool_call_id="aliased",
tool_arguments="{}",
)
result = await aliased_tool.on_invoke_tool(context, "{}")
output = ItemHelpers.tool_call_output_item(
_function_call(),
result,
output_json_schema=aliased_tool.output_json_schema,
output_type_adapter=aliased_tool._output_type_adapter,
)
assert aliased_tool.output_json_schema is not None
assert "availableUnits" in aliased_tool.output_json_schema["properties"]
assert json.loads(cast(str, output["output"])) == {
"sku": "A-1",
"availableUnits": 42,
}
def test_responses_converter_serializes_allowed_callers_for_other_eligible_tools() -> None:
async def shell_executor(_request: Any) -> str:
return "ok"
def custom_executor(_context: Any, _input: str) -> str:
return "ok"
class Editor:
def create_file(self, _operation: Any) -> str:
return "ok"
def update_file(self, _operation: Any) -> str:
return "ok"
def delete_file(self, _operation: Any) -> str:
return "ok"
converted = ResponsesConverter.convert_tools(
tools=[
ProgrammaticToolCallingTool(),
ShellTool(executor=shell_executor, allowed_callers=["programmatic"]),
ApplyPatchTool(editor=Editor(), allowed_callers=["direct", "programmatic"]),
CustomTool(
name="custom",
description="Custom tool",
on_invoke_tool=custom_executor,
allowed_callers=["programmatic"],
),
],
handoffs=[],
)
tool_payloads = [cast(dict[str, Any], tool) for tool in converted.tools]
assert tool_payloads[1]["allowed_callers"] == ["programmatic"]
assert tool_payloads[2]["allowed_callers"] == ["direct", "programmatic"]
assert tool_payloads[3]["allowed_callers"] == ["programmatic"]
@pytest.mark.parametrize(
"allowed_callers",
[
[],
["direct", "direct"],
["unsupported"],
],
)
def test_tool_construction_rejects_invalid_allowed_callers(
allowed_callers: list[Any],
) -> None:
with pytest.raises(UserError, match="allowed_callers"):
function_tool(lambda: "ok", allowed_callers=allowed_callers)
with pytest.raises(UserError, match="allowed_callers"):
ShellTool(executor=lambda _request: "ok", allowed_callers=allowed_callers)
with pytest.raises(UserError, match="allowed_callers"):
HostedMCPTool(
tool_config=cast(
Any,
{
"type": "mcp",
"server_label": "inventory",
"server_url": "https://example.com/mcp",
"allowed_callers": allowed_callers,
},
)
)
with pytest.raises(UserError, match="allowed_callers"):
CodeInterpreterTool(
tool_config=cast(
Any,
{
"type": "code_interpreter",
"container": "auto",
"allowed_callers": allowed_callers,
},
)
)
def test_responses_converter_rejects_incomplete_programmatic_configuration() -> None:
@function_tool(allowed_callers=["programmatic"])
def programmatic_only() -> str:
return "ok"
with pytest.raises(UserError, match="requires ProgrammaticToolCallingTool"):
ResponsesConverter.convert_tools(tools=[programmatic_only], handoffs=[])
with pytest.raises(UserError, match="requires ProgrammaticToolCallingTool"):
ResponsesConverter.convert_tools(
tools=[],
handoffs=[],
tool_choice="programmatic_tool_calling",
)
with pytest.raises(UserError, match="requires at least one tool"):
ResponsesConverter.convert_tools(
tools=[ProgrammaticToolCallingTool()],
handoffs=[],
)
with pytest.raises(UserError, match="Only one ProgrammaticToolCallingTool"):
ResponsesConverter.convert_tools(
tools=[ProgrammaticToolCallingTool(), ProgrammaticToolCallingTool()],
handoffs=[],
)
def test_responses_converter_accepts_mixed_or_tool_search_managed_configuration() -> None:
@function_tool(allowed_callers=["direct", "programmatic"])
def mixed_callers() -> str:
return "ok"
converted_mixed = ResponsesConverter.convert_tools(tools=[mixed_callers], handoffs=[])
assert cast(dict[str, Any], converted_mixed.tools[0])["allowed_callers"] == [
"direct",
"programmatic",
]
converted_search = ResponsesConverter.convert_tools(
tools=[ProgrammaticToolCallingTool(), ToolSearchTool()],
handoffs=[],
allow_opaque_tool_search_surface=True,
)
assert converted_search.tools == [
{"type": "programmatic_tool_calling"},
{"type": "tool_search"},
]
def test_chat_completions_rejects_programmatic_tool_configuration() -> None:
@function_tool(allowed_callers=["programmatic"])
def lookup_inventory() -> InventoryOutput:
return InventoryOutput(sku="A-1", available_units=42)
with pytest.raises(UserError, match="only supported with OpenAI Responses models"):
ChatCompletionsConverter.tool_to_openai(lookup_inventory)
with pytest.raises(UserError, match="programmatic_tool_calling"):
ChatCompletionsConverter.convert_tool_choice("programmatic_tool_calling")
with pytest.raises(UserError, match="Hosted tools are not supported"):
ChatCompletionsConverter.tool_to_openai(ProgrammaticToolCallingTool())
def test_function_output_preserves_caller_and_uses_declared_json_schema() -> None:
output = ItemHelpers.tool_call_output_item(
_function_call(),
{"sku": "A-1", "available_units": 42},
output_json_schema={"type": "object"},
)
assert json.loads(cast(str, output["output"])) == {
"sku": "A-1",
"available_units": 42,
}
assert _caller_dict(output["caller"]) == PROGRAM_CALLER
programmatic_output = ItemHelpers.tool_call_output_item(
_function_call(),
{"sku": "A-1", "units": [1, 2]},
)
assert json.loads(cast(str, programmatic_output["output"])) == {
"sku": "A-1",
"units": [1, 2],
}
assert _caller_dict(programmatic_output["caller"]) == PROGRAM_CALLER
direct_call = ResponseFunctionToolCall(
id="direct_function_item",
call_id="direct_call",
name="lookup_inventory",
arguments="{}",
type="function_call",
)
legacy_output = ItemHelpers.tool_call_output_item(direct_call, {"sku": "A-1"})
assert legacy_output["output"] == "{'sku': 'A-1'}"
def test_process_model_response_keeps_program_items_in_order() -> None:
@function_tool(allowed_callers=["programmatic"])
def lookup_inventory(sku: str) -> str:
return sku
agent = Agent(
name="inventory",
tools=[ProgrammaticToolCallingTool(), lookup_inventory],
)
response = ModelResponse(
output=[_program(), _function_call(), _program_output("incomplete")],
usage=Usage(),
response_id="response_1",
)
processed = process_model_response(
agent=agent,
all_tools=agent.tools,
response=response,
output_schema=None,
handoffs=[],
)
assert [type(item) for item in processed.new_items] == [
ToolCallItem,
ToolCallItem,
ToolCallOutputItem,
]
assert [_raw_item_type(item.raw_item) for item in processed.new_items] == [
"program",
"function_call",
"program_output",
]
assert processed.tools_used == [
"programmatic_tool_calling",
"lookup_inventory",
"programmatic_tool_calling",
]
@pytest.mark.parametrize("call_id", [None, ""])
def test_process_model_response_rejects_program_without_valid_call_id(
call_id: str | None,
) -> None:
program: dict[str, Any] = {
"type": "program",
"id": "program_item",
"code": "return 42",
"fingerprint": "fingerprint",
}
if call_id is not None:
program["call_id"] = call_id
response = ModelResponse(output=[], usage=Usage(), response_id="response_1")
response.output = cast(Any, [program])
agent = Agent(name="inventory", tools=[ProgrammaticToolCallingTool()])
with pytest.raises(ModelBehaviorError, match="without a valid call_id"):
process_model_response(
agent=agent,
all_tools=agent.tools,
response=response,
output_schema=None,
handoffs=[],
)
@pytest.mark.parametrize(
"program_output",
[_program_output(), _program_output().model_dump(exclude_none=True)],
)
def test_process_model_response_rejects_orphan_program_output(program_output: Any) -> None:
agent = Agent(name="inventory", tools=[ProgrammaticToolCallingTool()])
with pytest.raises(ModelBehaviorError, match="does not match a parent program item"):
process_model_response(
agent=agent,
all_tools=agent.tools,
response=ModelResponse(
output=[program_output],
usage=Usage(),
response_id="response_1",
),
output_schema=None,
handoffs=[],
)
def test_process_model_response_accepts_program_output_for_retained_program() -> None:
agent = Agent(name="inventory", tools=[ProgrammaticToolCallingTool()])
existing_program = ToolCallItem(raw_item=_program(), agent=agent)
processed = process_model_response(
agent=agent,
all_tools=agent.tools,
response=ModelResponse(
output=[_program_output()],
usage=Usage(),
response_id="response_1",
),
output_schema=None,
handoffs=[],
existing_items=[existing_program],
)
assert len(processed.new_items) == 1
assert isinstance(processed.new_items[0], ToolCallOutputItem)
@pytest.mark.parametrize(
("field", "value", "remove_field", "error_match"),
[
("status", None, True, "without a valid status"),
("status", "running", False, "without a valid status"),
("result", None, True, "without a string result"),
("result", 42, False, "without a string result"),
],
)
def test_process_model_response_rejects_malformed_program_output(
field: str,
value: Any,
remove_field: bool,
error_match: str,
) -> None:
agent = Agent(name="inventory", tools=[ProgrammaticToolCallingTool()])
program_output = _program_output().model_dump(exclude_none=True)
if remove_field:
program_output.pop(field)
else:
program_output[field] = value
response = ModelResponse(output=[], usage=Usage(), response_id="response_1")
response.output = cast(Any, [_program(), program_output])
with pytest.raises(ModelBehaviorError, match=error_match):
process_model_response(
agent=agent,
all_tools=agent.tools,
response=response,
output_schema=None,
handoffs=[],
)
@pytest.mark.parametrize("parent_location", ["existing_items", "current_response"])