Skip to content

The agent engineering platform.

fix(core): improve error message for missing title in JSON schema functions (#34683)

Bhavesh Sharmae26192403030
diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py
index fcb199914d..6850a5648e 100644
--- a/libs/core/langchain_core/utils/function_calling.py
+++ b/libs/core/langchain_core/utils/function_calling.py
@@ -428,11 +428,20 @@ def convert_to_openai_function(
"dict", _convert_python_function_to_openai_function(function)
)
else:
+ if isinstance(function, dict) and (
+ "type" in function or "properties" in function
+ ):
+ msg = (
+ f"Unsupported function\n\n{function}\n\nTo use a JSON schema as a "
+ "function, it must have a top-level 'title' key to be used as the "
+ "function name."
+ )
+ raise ValueError(msg)
msg = (
f"Unsupported function\n\n{function}\n\nFunctions must be passed in"
" as Dict, pydantic.BaseModel, or Callable. If they're a dict they must"
" either be in OpenAI function format or valid JSON schema with top-level"
- " 'title' and 'description' keys."
+ " 'title' key."
)
raise ValueError(msg)
diff --git a/libs/core/tests/unit_tests/utils/test_function_calling.py b/libs/core/tests/unit_tests/utils/test_function_calling.py
index 953f488542..29749b34a6 100644
--- a/libs/core/tests/unit_tests/utils/test_function_calling.py
+++ b/libs/core/tests/unit_tests/utils/test_function_calling.py
@@ -1180,3 +1180,32 @@ def test_convert_to_openai_function_strict_defaults() -> None:
func = convert_to_openai_function(MyModel, strict=True)
assert func["parameters"]["additionalProperties"] is False
+
+
+def test_convert_to_openai_function_json_schema_missing_title_with_type() -> None:
+ """Test error for JSON schema with 'type' but no 'title'."""
+ schema_without_title = {
+ "type": "object",
+ "properties": {"arg1": {"type": "string"}},
+ }
+ with pytest.raises(ValueError, match="must have a top-level 'title' key"):
+ convert_to_openai_function(schema_without_title)
+
+
+def test_convert_to_openai_function_json_schema_missing_title_properties() -> None:
+ """Test error for JSON schema with 'properties' but no 'title'."""
+ schema_without_title = {
+ "properties": {"arg1": {"type": "string"}},
+ }
+ with pytest.raises(ValueError, match="must have a top-level 'title' key"):
+ convert_to_openai_function(schema_without_title)
+
+
+def test_convert_to_openai_function_json_schema_missing_title_includes_schema() -> None:
+ """Test that the error message includes the schema for debugging."""
+ schema_without_title = {
+ "type": "object",
+ "properties": {"my_field": {"type": "integer"}},
+ }
+ with pytest.raises(ValueError, match="my_field"):
+ convert_to_openai_function(schema_without_title)