Skip to content

Commit

Permalink
core[patch]: fix empty OpenAI tools when strict=True (#26287)
Browse files Browse the repository at this point in the history
Fix #26232

---------

Co-authored-by: Eugene Yurtsev <[email protected]>
  • Loading branch information
baskaryan and eyurtsev authored Sep 11, 2024
1 parent d87feb1 commit feb3517
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions libs/core/langchain_core/utils/function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
19 changes: 19 additions & 0 deletions libs/core/tests/unit_tests/utils/test_function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,3 +793,22 @@ def magic_function(input: int | float) -> str:
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,
}

0 comments on commit feb3517

Please sign in to comment.