Skip to content

Commit a405f65

Browse files
authored
Add strict parameter (#30)
1 parent d122ebf commit a405f65

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

openai_function_calling/function.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class FunctionDict(TypedDict):
2626
name: str
2727
description: str
2828
parameters: ParametersDict
29+
strict: NotRequired[bool]
2930

3031

3132
class Function:
@@ -37,6 +38,7 @@ def __init__(
3738
description: str,
3839
parameters: list[Parameter] | None = None,
3940
required_parameters: list[str] | None = None,
41+
strict: bool | None = None,
4042
) -> None:
4143
"""Create a new function instance.
4244
@@ -46,12 +48,14 @@ def __init__(
4648
parameters: A list of parameters.
4749
required_parameters: A list of parameter names that are required to run the\
4850
function.
51+
strict: If the function should enforce strict parameters.
4952
5053
"""
5154
self.name: str = name
5255
self.description: str = description
5356
self.parameters: list[Parameter] = parameters or []
5457
self.required_parameters: list[str] = required_parameters or []
58+
self.strict: bool | None = strict
5559

5660
self.validate()
5761

@@ -110,6 +114,9 @@ def to_json_schema(self) -> FunctionDict:
110114
},
111115
}
112116

117+
if self.strict is not None:
118+
output_dict["strict"] = self.strict
119+
113120
if self.required_parameters is None or len(self.required_parameters) == 0:
114121
return output_dict
115122

tests/test_function.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,35 @@ def test_merge_with_no_parameters_does_not_add_any() -> None:
343343
get_current_weather_function.merge(get_tomorrows_weather_function)
344344

345345
assert len(get_current_weather_function.parameters) == 0
346+
347+
348+
def test_function_with_strict_true_includes_strict_in_output() -> None:
349+
func = Function(
350+
name="example_function",
351+
description="An example function",
352+
strict=True,
353+
)
354+
func_dict: FunctionDict = func.to_json_schema()
355+
356+
assert func_dict.get("strict") is True
357+
358+
359+
def test_function_with_strict_false_includes_strict_in_output() -> None:
360+
func = Function(
361+
name="example_function",
362+
description="An example function",
363+
strict=False,
364+
)
365+
func_dict: FunctionDict = func.to_json_schema()
366+
367+
assert func_dict.get("strict") is False
368+
369+
370+
def test_function_without_strict_excludes_strict_in_output() -> None:
371+
func = Function(
372+
name="example_function",
373+
description="An example function",
374+
)
375+
func_dict: FunctionDict = func.to_json_schema()
376+
377+
assert "strict" not in func_dict

0 commit comments

Comments
 (0)