core[patch]: fix empty OpenAI tools when strict=True (#26287)

Fix #26232

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
Bagatur 2024-09-11 16:06:03 -07:00 committed by GitHub
parent d87feb1b04
commit feb351737c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 2 deletions

View File

@ -605,9 +605,13 @@ def _recursive_set_additional_properties_false(
schema: Dict[str, Any],
) -> Dict[str, Any]:
if isinstance(schema, dict):
# Check if 'required' is a key at the current level
if "required" in schema:
# Check if 'required' is a key at the current level or if the schema is empty,
# in which case additionalProperties still needs to be specified.
if "required" in schema or (
"properties" in schema and not schema["properties"]
):
schema["additionalProperties"] = False
# Recursively check 'properties' and 'items' if they exist
if "properties" in schema:
for value in schema["properties"].values():

View File

@ -793,3 +793,22 @@ def test_convert_union_type_py_39() -> None:
assert result["parameters"]["properties"]["input"] == {
"anyOf": [{"type": "integer"}, {"type": "number"}]
}
def test_convert_to_openai_function_no_args() -> None:
@tool
def empty_tool() -> str:
"""No args"""
return "foo"
actual = convert_to_openai_function(empty_tool, strict=True)
assert actual == {
"name": "empty_tool",
"description": "No args",
"parameters": {
"properties": {},
"additionalProperties": False,
"type": "object",
},
"strict": True,
}