forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_output_tool.py
113 lines (79 loc) · 4.13 KB
/
test_output_tool.py
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
import json
import pytest
from pydantic import BaseModel
from typing_extensions import TypedDict
from agents import Agent, AgentOutputSchema, ModelBehaviorError, Runner, UserError, _utils
from agents.agent_output import _WRAPPER_DICT_KEY
def test_plain_text_output():
agent = Agent(name="test")
output_schema = Runner._get_output_schema(agent)
assert not output_schema, "Shouldn't have an output tool config without an output type"
agent = Agent(name="test", output_type=str)
assert not output_schema, "Shouldn't have an output tool config with str output type"
class Foo(BaseModel):
bar: str
def test_structured_output_pydantic():
agent = Agent(name="test", output_type=Foo)
output_schema = Runner._get_output_schema(agent)
assert output_schema, "Should have an output tool config with a structured output type"
assert output_schema.output_type == Foo, "Should have the correct output type"
assert not output_schema._is_wrapped, "Pydantic objects should not be wrapped"
for key, value in Foo.model_json_schema().items():
assert output_schema.json_schema()[key] == value
json_str = Foo(bar="baz").model_dump_json()
validated = output_schema.validate_json(json_str)
assert validated == Foo(bar="baz")
class Bar(TypedDict):
bar: str
def test_structured_output_typed_dict():
agent = Agent(name="test", output_type=Bar)
output_schema = Runner._get_output_schema(agent)
assert output_schema, "Should have an output tool config with a structured output type"
assert output_schema.output_type == Bar, "Should have the correct output type"
assert not output_schema._is_wrapped, "TypedDicts should not be wrapped"
json_str = json.dumps(Bar(bar="baz"))
validated = output_schema.validate_json(json_str)
assert validated == Bar(bar="baz")
def test_structured_output_list():
agent = Agent(name="test", output_type=list[str])
output_schema = Runner._get_output_schema(agent)
assert output_schema, "Should have an output tool config with a structured output type"
assert output_schema.output_type == list[str], "Should have the correct output type"
assert output_schema._is_wrapped, "Lists should be wrapped"
# This is testing implementation details, but it's useful to make sure this doesn't break
json_str = json.dumps({_WRAPPER_DICT_KEY: ["foo", "bar"]})
validated = output_schema.validate_json(json_str)
assert validated == ["foo", "bar"]
def test_bad_json_raises_error(mocker):
agent = Agent(name="test", output_type=Foo)
output_schema = Runner._get_output_schema(agent)
assert output_schema, "Should have an output tool config with a structured output type"
with pytest.raises(ModelBehaviorError):
output_schema.validate_json("not valid json")
agent = Agent(name="test", output_type=list[str])
output_schema = Runner._get_output_schema(agent)
assert output_schema, "Should have an output tool config with a structured output type"
mock_validate_json = mocker.patch.object(_utils, "validate_json")
mock_validate_json.return_value = ["foo"]
with pytest.raises(ModelBehaviorError):
output_schema.validate_json(json.dumps(["foo"]))
mock_validate_json.return_value = {"value": "foo"}
with pytest.raises(ModelBehaviorError):
output_schema.validate_json(json.dumps(["foo"]))
def test_plain_text_obj_doesnt_produce_schema():
output_wrapper = AgentOutputSchema(output_type=str)
with pytest.raises(UserError):
output_wrapper.json_schema()
def test_structured_output_is_strict():
output_wrapper = AgentOutputSchema(output_type=Foo)
assert output_wrapper.strict_json_schema
for key, value in Foo.model_json_schema().items():
assert output_wrapper.json_schema()[key] == value
assert (
"additionalProperties" in output_wrapper.json_schema()
and not output_wrapper.json_schema()["additionalProperties"]
)
def test_setting_strict_false_works():
output_wrapper = AgentOutputSchema(output_type=Foo, strict_json_schema=False)
assert not output_wrapper.strict_json_schema
assert output_wrapper.json_schema() == Foo.model_json_schema()