Update tests and docs for strict mode decorator

This commit is contained in:
Rohan Mehta 2025-03-17 15:06:57 -04:00
parent b09a5bfc43
commit 47aed7d362
2 changed files with 17 additions and 6 deletions

View file

@ -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:

View file

@ -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"
@ -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,
@ -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))