Skip to content

Commit 04ae744

Browse files
gaoanze888gaoanzeccurme
authored
fix(core): make convert_to_openai_function handle callables and non-dict mappings (#39750)
Co-authored-by: gaoanze <[email protected]> Co-authored-by: Chester Curme <[email protected]>
1 parent 37f2662 commit 04ae744

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎libs/core/langchain_core/utils/function_calling.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from pydantic.v1 import create_model as create_model_v1
3030
from typing_extensions import TypedDict, is_typeddict
3131

32-
import langchain_core
3332
from langchain_core._api import beta
3433
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage
3534
from langchain_core.utils.json_schema import dereference_refs
@@ -225,8 +224,11 @@ def _convert_python_function_to_openai_function(
225224
Returns:
226225
The OpenAI function description.
227226
"""
227+
# Import locally to prevent circular import
228+
from langchain_core.tools.base import create_schema_from_function # noqa: PLC0415
229+
228230
func_name = _get_python_function_name(function)
229-
model = langchain_core.tools.base.create_schema_from_function(
231+
model = create_schema_from_function(
230232
func_name,
231233
function,
232234
filter_args=(),
@@ -336,9 +338,10 @@ def _format_tool_to_openai_function(tool: BaseTool) -> FunctionDescription:
336338
Returns:
337339
The function description.
338340
"""
339-
is_simple_oai_tool = (
340-
isinstance(tool, langchain_core.tools.simple.Tool) and not tool.args_schema
341-
)
341+
# Import locally to prevent circular import
342+
from langchain_core.tools import Tool # noqa: PLC0415
343+
344+
is_simple_oai_tool = isinstance(tool, Tool) and not tool.args_schema
342345
if tool.tool_call_schema and not is_simple_oai_tool:
343346
if isinstance(tool.tool_call_schema, dict):
344347
return _convert_json_schema_to_openai_function(
@@ -402,6 +405,9 @@ def convert_to_openai_function(
402405
`description` and `parameters` keys are now optional. Only `name` is
403406
required and guaranteed to be part of the output.
404407
"""
408+
# Import locally to prevent circular import
409+
from langchain_core.tools import BaseTool # noqa: PLC0415
410+
405411
if strict and isinstance(function, dict):
406412
function = copy.deepcopy(function)
407413

@@ -447,7 +453,7 @@ def convert_to_openai_function(
447453
"dict[str, Any]",
448454
_convert_typed_dict_to_openai_function(cast("type", function)),
449455
)
450-
elif isinstance(function, langchain_core.tools.base.BaseTool):
456+
elif isinstance(function, BaseTool):
451457
oai_function = cast("dict[str, Any]", _format_tool_to_openai_function(function))
452458
elif callable(function):
453459
oai_function = cast(

‎libs/core/tests/unit_tests/utils/test_function_calling.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pydantic import BaseModel, ConfigDict, Field
2626
from pydantic.errors import PydanticInvalidForJsonSchema
2727

28+
import langchain_core
2829
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
2930
from langchain_core.runnables import RunnableLambda
3031
from langchain_core.tools import BaseTool, StructuredTool, Tool, tool
@@ -1475,3 +1476,29 @@ def test_convert_to_openai_tool_computer_passthrough() -> None:
14751476
}
14761477
result = convert_to_openai_tool(computer_tool)
14771478
assert result == computer_tool
1479+
1480+
1481+
def test_convert_to_openai_function_without_tools_module_imported(
1482+
monkeypatch: pytest.MonkeyPatch,
1483+
) -> None:
1484+
"""Test conversion works when `langchain_core.tools` is not bound on the package.
1485+
1486+
Regression test: the `BaseTool` check reached the class through
1487+
`langchain_core.tools.base`, which is only an attribute of `langchain_core` once
1488+
something imports that submodule. Called from a process that had not, conversion
1489+
raised `AttributeError: module 'langchain_core' has no attribute 'tools'` instead of
1490+
converting the callable.
1491+
"""
1492+
monkeypatch.delattr(langchain_core, "tools", raising=False)
1493+
1494+
def my_func(x: int) -> int:
1495+
"""Return the input.
1496+
1497+
Args:
1498+
x: A number.
1499+
"""
1500+
return x
1501+
1502+
result = convert_to_openai_function(my_func)
1503+
1504+
assert result["name"] == "my_func"

0 commit comments

Comments
 (0)