Skip to content

Commit 6f7e801

Browse files
authored
Update tests and docs for strict mode decorator (#205)
As titled. Test plan: unit tests/docs.
2 parents ff65fb4 + 47aed7d commit 6f7e801

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/agents/tool.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ def function_tool(
190190
failure_error_function: If provided, use this function to generate an error message when
191191
the tool call fails. The error message is sent to the LLM. If you pass None, then no
192192
error message will be sent and instead an Exception will be raised.
193-
strict_mode: If False, parameters with default values become optional in the
194-
function schema.
193+
strict_mode: Whether to enable strict mode for the tool's JSON schema. We *strongly*
194+
recommend setting this to True, as it increases the likelihood of correct JSON input.
195+
If False, it allows non-strict JSON schemas. For example, if a parameter has a default
196+
value, it will be optional, additional properties are allowed, etc. See here for more:
197+
https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses#supported-schemas
195198
"""
196199

197200
def _create_function_tool(the_func: ToolFunction[...]) -> FunctionTool:

tests/test_function_tool_decorator.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,13 @@ def optional_param_function(a: int, b: Optional[int] = None) -> str:
152152

153153

154154
@pytest.mark.asyncio
155-
async def test_optional_param_function():
155+
async def test_non_strict_mode_function():
156156
tool = optional_param_function
157157

158+
assert tool.strict_json_schema is False, "strict_json_schema should be False"
159+
160+
assert tool.params_json_schema.get("required") == ["a"], "required should only be a"
161+
158162
input_data = {"a": 5}
159163
output = await tool.on_invoke_tool(ctx_wrapper(), json.dumps(input_data))
160164
assert output == "5_no_b"
@@ -165,7 +169,7 @@ async def test_optional_param_function():
165169

166170

167171
@function_tool(strict_mode=False)
168-
def multiple_optional_params_function(
172+
def all_optional_params_function(
169173
x: int = 42,
170174
y: str = "hello",
171175
z: Optional[int] = None,
@@ -176,8 +180,12 @@ def multiple_optional_params_function(
176180

177181

178182
@pytest.mark.asyncio
179-
async def test_multiple_optional_params_function():
180-
tool = multiple_optional_params_function
183+
async def test_all_optional_params_function():
184+
tool = all_optional_params_function
185+
186+
assert tool.strict_json_schema is False, "strict_json_schema should be False"
187+
188+
assert tool.params_json_schema.get("required") is None, "required should be empty"
181189

182190
input_data: dict[str, Any] = {}
183191
output = await tool.on_invoke_tool(ctx_wrapper(), json.dumps(input_data))

0 commit comments

Comments
 (0)