Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests and docs for strict mode decorator #205

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/agents/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,11 @@ def function_tool(
failure_error_function: If provided, use this function to generate an error message when
the tool call fails. The error message is sent to the LLM. If you pass None, then no
error message will be sent and instead an Exception will be raised.
strict_mode: If False, parameters with default values become optional in the
function schema.
strict_mode: Whether to enable strict mode for the tool's JSON schema. We *strongly*
recommend setting this to True, as it increases the likelihood of correct JSON input.
If False, it allows non-strict JSON schemas. For example, if a parameter has a default
value, it will be optional, additional properties are allowed, etc. See here for more:
https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses#supported-schemas
"""

def _create_function_tool(the_func: ToolFunction[...]) -> FunctionTool:
Expand Down
16 changes: 12 additions & 4 deletions tests/test_function_tool_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,13 @@ def optional_param_function(a: int, b: Optional[int] = None) -> str:


@pytest.mark.asyncio
async def test_optional_param_function():
async def test_non_strict_mode_function():
tool = optional_param_function

assert tool.strict_json_schema is False, "strict_json_schema should be False"

assert tool.params_json_schema.get("required") == ["a"], "required should only be a"

input_data = {"a": 5}
output = await tool.on_invoke_tool(ctx_wrapper(), json.dumps(input_data))
assert output == "5_no_b"
Expand All @@ -165,7 +169,7 @@ async def test_optional_param_function():


@function_tool(strict_mode=False)
def multiple_optional_params_function(
def all_optional_params_function(
x: int = 42,
y: str = "hello",
z: Optional[int] = None,
Expand All @@ -176,8 +180,12 @@ def multiple_optional_params_function(


@pytest.mark.asyncio
async def test_multiple_optional_params_function():
tool = multiple_optional_params_function
async def test_all_optional_params_function():
tool = all_optional_params_function

assert tool.strict_json_schema is False, "strict_json_schema should be False"

assert tool.params_json_schema.get("required") is None, "required should be empty"

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