The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(core): require all nested properties for strict tool schemas (#39306)
Nishitha M65e5e3cfa36c
diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.pyindex 301b5241e1..94ca4038cb 100644--- a/libs/core/langchain_core/utils/function_calling.py+++ b/libs/core/langchain_core/utils/function_calling.py@@ -3,6 +3,7 @@from __future__ import annotationsimport collections+import copyimport inspectimport loggingimport types@@ -401,6 +402,9 @@ def convert_to_openai_function(`description` and `parameters` keys are now optional. Only `name` isrequired and guaranteed to be part of the output."""+ if strict and isinstance(function, dict):+ function = copy.deepcopy(function)+# an Anthropic format toolif isinstance(function, dict) and all(k in function for k in ("name", "input_schema")@@ -477,18 +481,6 @@ def convert_to_openai_function(raise ValueError(msg)oai_function["strict"] = strictif strict:- # All fields must be `required`- parameters = oai_function.get("parameters")- if isinstance(parameters, dict):- fields = parameters.get("properties")- if isinstance(fields, dict) and fields:- parameters = dict(parameters)- parameters["required"] = list(fields.keys())- oai_function["parameters"] = parameters-- # As of 08/06/24, OpenAI requires that additionalProperties be supplied and- # set to False if strict is True.- # All properties layer needs 'additionalProperties=False'oai_function["parameters"] = _recursive_set_additional_properties_false(oai_function["parameters"])@@ -828,6 +820,12 @@ def _recursive_set_additional_properties_false(schema: dict[str, Any],) -> dict[str, Any]:if isinstance(schema, dict):+ # OpenAI strict mode requires every property to appear in `required` at every+ # level of nesting. Without this, nested object schemas are rejected.+ properties = schema.get("properties")+ if isinstance(properties, dict) and properties:+ schema["required"] = list(properties.keys())+# 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 (@@ -845,10 +843,21 @@ def _recursive_set_additional_properties_false(if "anyOf" in schema:for sub_schema in schema["anyOf"]:_recursive_set_additional_properties_false(sub_schema)+ # Pydantic <2.9 wraps a referenced model field in 'allOf' when it has+ # sibling keys (e.g. 'description'), instead of merging them directly.+ if "allOf" in schema:+ for sub_schema in schema["allOf"]:+ _recursive_set_additional_properties_false(sub_schema)if "properties" in schema:for sub_schema in schema["properties"].values():_recursive_set_additional_properties_false(sub_schema)if "items" in schema:_recursive_set_additional_properties_false(schema["items"])+ # Raw JSON schemas may keep nested objects in `$defs` and reference them via+ # `$ref`; walk those definitions too so they're made strict.+ for defs_key in ("$defs", "definitions"):+ if isinstance(schema.get(defs_key), dict):+ for sub_schema in schema[defs_key].values():+ _recursive_set_additional_properties_false(sub_schema)return schemadiff --git a/libs/core/tests/unit_tests/utils/test_function_calling.py b/libs/core/tests/unit_tests/utils/test_function_calling.pyindex e53c034406..1e5a2d3e0f 100644--- a/libs/core/tests/unit_tests/utils/test_function_calling.py+++ b/libs/core/tests/unit_tests/utils/test_function_calling.py@@ -1197,6 +1197,73 @@ def test_convert_to_openai_function_strict_required() -> None:assert actual == expected+def test_convert_to_openai_function_strict_nested_required() -> None:+ """Nested objects must also list every property as `required` under strict mode.++ See: https://github.com/langchain-ai/langchain/issues/33869+ """++ class Inner(BaseModel):+ """Inner schema."""++ required_field: str = Field(..., description="req")+ optional_field: str = Field(default="x", description="opt")++ class Outer(BaseModel):+ """Outer schema."""++ inner: Inner = Field(..., description="nested")++ func = convert_to_openai_function(Outer, strict=True)+ inner_schema = func["parameters"]["properties"]["inner"]+ # Pydantic <2.9 wraps a referenced model field with sibling keys (e.g. the+ # `description` set above) in `allOf` instead of merging them directly.+ if "allOf" in inner_schema:+ inner_schema = inner_schema["allOf"][0]+ assert set(inner_schema["required"]) == {"required_field", "optional_field"}+ assert inner_schema["additionalProperties"] is False+++def test_convert_to_openai_function_strict_nested_required_via_defs_ref() -> None:+ """Object definitions referenced via `$ref`/`$defs` must also be made strict.++ Raw JSON-schema-style tool input can represent nested objects as `$ref`+ pointers into a top-level `$defs`/`definitions` map instead of inlining+ them under `properties`. See: https://github.com/langchain-ai/langchain/pull/39306+ """+ raw_schema = {+ "name": "f",+ "parameters": {+ "type": "object",+ "properties": {"inner": {"$ref": "#/$defs/Inner"}},+ "$defs": {+ "Inner": {+ "type": "object",+ "properties": {+ "required_field": {"type": "string"},+ "optional_field": {"type": "string"},+ },+ }+ },+ },+ }++ # Snapshot the caller's schema before conversion; the strict-mode walk+ # mutates `required`/`additionalProperties` in place, so `deepcopy` must+ # shield the original input (see "don't mutate" commit on #39306).+ original_inner: dict[str, Any] = raw_schema["parameters"]["$defs"]["Inner"] # type: ignore[index]+ assert "required" not in original_inner+ assert "additionalProperties" not in original_inner++ func = convert_to_openai_function(raw_schema, strict=True)+ inner_def: dict[str, Any] = func["parameters"]["$defs"]["Inner"]+ assert set(inner_def["required"]) == {"required_field", "optional_field"}+ assert inner_def["additionalProperties"] is False+ # The caller's original schema must be left untouched.+ assert "required" not in original_inner+ assert "additionalProperties" not in original_inner++def test_convert_to_openai_function_arbitrary_type_error() -> None:"""Test that a helpful error is raised for non-JSON-serializable types.