langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.91k stars 15.37k forks source link

Getting error while calling a tool with two arguments #26849

Closed YogeshSaini85 closed 1 month ago

YogeshSaini85 commented 1 month ago

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

OS: Windows OS Version: 10.0.19045 Python Version: 3.12.6 (tags/v3.12.6:a4a2d2b, Sep 6 2024, 20:11:23) [MSC v.1940 64 bit (AMD64)]

Package Information

langchain_core: 0.3.5 langchain: 0.3.0 langsmith: 0.1.127 langchain_text_splitters: 0.3.0

Optional packages not installed

langgraph langserve

Other Dependencies

aiohttp: 3.10.6 async-timeout: Installed. No version info available. httpx: 0.27.2 jsonpatch: 1.33 numpy: 1.26.4 orjson: 3.10.7 packaging: 24.1 pydantic: 2.9.2 PyYAML: 6.0.2 requests: 2.32.3 SQLAlchemy: 2.0.35 tenacity: 8.5.0 typing-extensions: 4.12.2

enzo-inc commented 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)
YogeshSaini85 commented 1 month ago

Thank you @enzo-inc , it worked.