The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(core): preserve flat tool args schema for `RootModel` runnables (#39307)
Nishitha M0424e03dc71c
diff --git a/libs/core/langchain_core/tools/convert.py b/libs/core/langchain_core/tools/convert.pyindex 6b4a8c37c2..d353fc5250 100644--- a/libs/core/langchain_core/tools/convert.py+++ b/libs/core/langchain_core/tools/convert.py@@ -4,7 +4,7 @@ import inspectfrom collections.abc import Callablefrom typing import Any, Literal, cast, get_type_hints, overload-from pydantic import BaseModel, Field, create_model+from pydantic import BaseModel, Field, RootModel, create_modelfrom langchain_core.callbacks import Callbacksfrom langchain_core.runnables import Runnable@@ -469,12 +469,19 @@ def convert_runnable_to_tool(def invoke_wrapper(callbacks: Callbacks | None = None, **kwargs: Any) -> Any:return runnable.invoke(kwargs, config={"callbacks": callbacks})+ input_schema_cls = runnable.input_schema+ # Detect `RootModel` input schemas so we don't use them directly as the tool+ # args schema.+ is_root_model = isinstance(input_schema_cls, type) and issubclass(+ input_schema_cls, RootModel+ )if (arg_types is Noneand schema.get("type") == "object"and schema.get("properties")+ and not is_root_model):- args_schema = runnable.input_schema+ args_schema = input_schema_clselse:args_schema = _get_schema_from_runnable_and_arg_types(runnable, name, arg_types=arg_typesdiff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.pyindex 522500815f..f23fd9052a 100644--- a/libs/core/tests/unit_tests/test_tools.py+++ b/libs/core/tests/unit_tests/test_tools.py@@ -23,7 +23,7 @@ from typing import ()import pytest-from pydantic import BaseModel, ConfigDict, Field, ValidationError+from pydantic import BaseModel, ConfigDict, Field, RootModel, ValidationErrorfrom pydantic.v1 import BaseModel as BaseModelV1from pydantic.v1 import ValidationError as ValidationErrorV1from typing_extensions import TypedDict, override@@ -1759,6 +1759,57 @@ def test_convert_from_runnable_dict() -> None:assert result == "6"+def test_convert_from_runnable_root_model_input_schema() -> None:+ """`as_tool` should not advertise a `TypedDict` input nested under `root`.++ Some `Runnable`s (e.g. a compiled `langgraph` `StateGraph`) expose a+ `pydantic.RootModel` as `input_schema` even though `get_input_jsonschema`+ reports a flat object schema. See:+ """++ class Args(TypedDict):+ foo: str+ bar: str++ class _RootModelInputRunnable(RunnableLambda[Args, str]):+ @override+ def get_input_schema(+ self, config: RunnableConfig | None = None+ ) -> TypeBaseModel:+ return RootModel[Args]++ @override+ def get_input_jsonschema(+ self, config: RunnableConfig | None = None+ ) -> dict[str, Any]:+ return {+ "type": "object",+ "properties": {+ "foo": {"type": "string"},+ "bar": {"type": "string"},+ },+ "required": ["foo", "bar"],+ }++ def f(x: Args) -> str:+ return f"{x['foo']} {x['bar']}"++ runnable = _RootModelInputRunnable(f)+ as_tool = runnable.as_tool(name="my_tool", description="Example tool.")++ assert as_tool.args_schema is not None+ assert isinstance(as_tool.args_schema, type)+ assert not issubclass(as_tool.args_schema, RootModel)++ oai_schema = convert_to_openai_tool(as_tool)+ parameters = oai_schema["function"]["parameters"]+ assert parameters["properties"].keys() == {"foo", "bar"}+ assert "root" not in parameters["properties"]++ result = as_tool.invoke({"foo": "hello", "bar": "world"})+ assert result == "hello world"++def test_convert_from_runnable_other() -> None:# String inputdef f(x: str) -> str: