42
42
RunnableConfig = Any
43
43
RunnableSerializable = Any
44
44
45
+ try :
46
+ from langchain_google_vertexai .functions_utils import _ToolsType
47
+
48
+ _ToolLike = _ToolsType
49
+ except ImportError :
50
+ _ToolLike = Any
51
+
45
52
46
53
def _default_runnable_kwargs (has_history : bool ) -> Mapping [str , Any ]:
47
54
# https://github.com/langchain-ai/langchain/blob/5784dfed001730530637793bea1795d9d5a7c244/libs/core/langchain_core/runnables/history.py#L237-L241
@@ -62,7 +69,13 @@ def _default_runnable_kwargs(has_history: bool) -> Mapping[str, Any]:
62
69
63
70
64
71
def _default_output_parser ():
65
- from langchain .agents .output_parsers .tools import ToolsAgentOutputParser
72
+ try :
73
+ from langchain .agents .output_parsers .tools import ToolsAgentOutputParser
74
+ except (ModuleNotFoundError , ImportError ):
75
+ # Fallback to an older version if needed.
76
+ from langchain .agents .output_parsers .openai_tools import (
77
+ OpenAIToolsAgentOutputParser as ToolsAgentOutputParser ,
78
+ )
66
79
67
80
return ToolsAgentOutputParser ()
68
81
@@ -90,7 +103,7 @@ def _default_model_builder(
90
103
def _default_runnable_builder (
91
104
model : "BaseLanguageModel" ,
92
105
* ,
93
- tools : Optional [Sequence [Union [ Callable , "BaseTool" ] ]] = None ,
106
+ tools : Optional [Sequence ["_ToolLike" ]] = None ,
94
107
prompt : Optional ["RunnableSerializable" ] = None ,
95
108
output_parser : Optional ["RunnableSerializable" ] = None ,
96
109
chat_history : Optional ["GetSessionHistoryCallable" ] = None ,
@@ -123,6 +136,7 @@ def _default_runnable_builder(
123
136
if isinstance (tool , lc_tools .BaseTool )
124
137
else StructuredTool .from_function (tool )
125
138
for tool in tools
139
+ if isinstance (tool , (Callable , lc_tools .BaseTool ))
126
140
],
127
141
** agent_executor_kwargs ,
128
142
)
@@ -139,7 +153,14 @@ def _default_runnable_builder(
139
153
140
154
def _default_prompt (has_history : bool ) -> "RunnableSerializable" :
141
155
from langchain_core import prompts
142
- from langchain .agents .format_scratchpad .tools import format_to_tool_messages
156
+
157
+ try :
158
+ from langchain .agents .format_scratchpad .tools import format_to_tool_messages
159
+ except (ModuleNotFoundError , ImportError ):
160
+ # Fallback to an older version if needed.
161
+ from langchain .agents .format_scratchpad .openai_tools import (
162
+ format_to_openai_tool_messages as format_to_tool_messages ,
163
+ )
143
164
144
165
if has_history :
145
166
return {
@@ -186,12 +207,10 @@ def _validate_callable_parameters_are_annotated(callable: Callable):
186
207
)
187
208
188
209
189
- def _validate_tools (tools : Sequence [Union [ Callable , "BaseTool" ] ]):
210
+ def _validate_tools (tools : Sequence ["_ToolLike" ]):
190
211
"""Validates that the tools are usable for tool calling."""
191
- from langchain_core import tools as lc_tools
192
-
193
212
for tool in tools :
194
- if not isinstance (tool , lc_tools . BaseTool ):
213
+ if isinstance (tool , Callable ):
195
214
_validate_callable_parameters_are_annotated (tool )
196
215
197
216
@@ -208,7 +227,7 @@ def __init__(
208
227
model : str ,
209
228
* ,
210
229
prompt : Optional ["RunnableSerializable" ] = None ,
211
- tools : Optional [Sequence [Union [ Callable , "BaseTool" ] ]] = None ,
230
+ tools : Optional [Sequence ["_ToolLike" ]] = None ,
212
231
output_parser : Optional ["RunnableSerializable" ] = None ,
213
232
chat_history : Optional ["GetSessionHistoryCallable" ] = None ,
214
233
model_kwargs : Optional [Mapping [str , Any ]] = None ,
0 commit comments