Closed YogeshSaini85 closed 1 month ago
Hello,
According to the langhchain documentation on tool use, you can use the args_schema
argument to the @tool
decorator. This expects a pydantic BaseModel
, which can have your string as its field. For example
class PydanticArgs(BaseModel):
args: str
@tool(args_schema=PydanticArgs)
def get_schema_tool(args: str):
"""Can be used to get connections and objects associated with the applications and input is a python list
of applications e.g. ["app1","app2"]"""
app_list = ast.literal_eval(args)
return app_list
input = '["app1", "app2"]'
get_schema_tool(input)
Thank you @enzo-inc , it worked.
Checked other resources
Example Code
@tool def get_schema_tool(args: str): """Can be used to get connections and objects associated with the applications and input is a python list of applications e.g. ["app1","app2"]""" app_list = ast.literal_eval(args) return app_list
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor.from_agent_and_tools( agent=agent, tools=tools, verbose=True, handle_parsing_errors=True, max_iterations=4000, max_execution_time=60000, return_intermediate_steps=True ) agent_executor.agent.stream_runnable = False
agent_runnable = RunnableWithMessageHistory( agent_executor, lambda session_id: memory, input_messages_key="input", history_messages_key="chat_history", )
agentrunnable.invoke({"input": user_input})
Error Message and Stack Trace (if applicable)
File "C:\Users\ysaini\OneDrive - Informatica\Desktop\ApiGenie\ConversationMemory\cai_copilot.py", line 103, in
output = process_input(u_input, conv_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\OneDrive - Informatica\Desktop\ApiGenie\ConversationMemory\cai_copilot.py", line 87, in process_input
out = copilot_agent.invoke({"input": user_input},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\runnables\base.py", line 5343, in invoke
return self.bound.invoke(
^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\runnables\base.py", line 5343, in invoke
return self.bound.invoke(
^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\runnables\base.py", line 3022, in invoke
input = context.run(step.invoke, input, config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\runnables\base.py", line 5343, in invoke
return self.bound.invoke(
^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\runnables\base.py", line 4706, in invoke
return self._call_with_config(
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\runnables\base.py", line 1923, in _call_with_config
context.run(
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\runnables\config.py", line 396, in call_func_with_variable_args
return func(input, **kwargs) # type: ignore[call-arg]
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\runnables\base.py", line 4572, in _invoke
output = output.invoke(
^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\runnables\base.py", line 5343, in invoke
return self.bound.invoke(
^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain\chains\base.py", line 170, in invoke
raise e
File "C:\Users\ysaini\venv\Lib\site-packages\langchain\chains\base.py", line 160, in invoke
self._call(inputs, run_manager=run_manager)
File "C:\Users\ysaini\venv\Lib\site-packages\langchain\agents\agent.py", line 1629, in _call
next_step_output = self._take_next_step(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain\agents\agent.py", line 1337, in _take_next_step
for a in self._iter_next_step(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain\agents\agent.py", line 1420, in _iter_next_step
yield self._perform_agent_action(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain\agents\agent.py", line 1442, in _perform_agent_action
observation = tool.run(
^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\tools\base.py", line 688, in run
raise error_to_raise
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\tools\base.py", line 651, in run
tool_args, tool_kwargs = self._to_args_and_kwargs(tool_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\tools\base.py", line 574, in _to_args_and_kwargs
tool_input = self._parse_input(tool_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ysaini\venv\Lib\site-packages\langchain_core\tools\base.py", line 509, in _parse_input
input_args.modelvalidate({key: tool_input})
File "C:\Users\ysaini\venv\Lib\site-packages\pydantic\main.py", line 596, in model_validate
return cls.__pydantic_validator.validate_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for get_schema_tool
vargs
Input should be a valid list [type=list_type, input_value='["Salesforce", "ServiceNow"]', input_type=str]
For further information visit https://errors.pydantic.dev/2.9/v/list_type
Description
I have a tool which expects a string. This string is a string representation of a python list of strings. In the tool I do literal_eval to convert it into a python list.
But I get this error: Input should be a valid list [type=list_type, input_value='["Salesforce", "ServiceNow"]', input_type=str]
Why is it expecting list when I have clearly defined the args as string.
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies