-
Hello everyone! I'm trying to create an Agent with access to some tools where the Agent and the tools themselves are part of a class. This is pretty standard stuff outside of a class, but I'm running into issues when I try to use it as part of a custom class. Specifically, I'm trying to use the
The problem I'm running into is that the Agent will supply the
which causes an error: So my question - has anyone encountered this issue and figured out a way to work around it? I tried giving it an arg_schema that was an empty class along with
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 16 replies
-
🤖 Hello, The issue you're encountering is due to the fact that the One way to work around this is to use the class MyClass:
@staticmethod
@tool("my_tool", return_direct=True)
def my_method(input: str) -> str:
# Your code here
return processed_input In this example, Please note that static methods cannot access or modify the instance or class state, so this approach is only suitable for methods that don't need to interact with other parts of the class. If you need to access or modify the instance or class state, you might need to rethink your design. One possibility is to move the method outside of the class and pass any necessary data as arguments. I hope this helps! Let me know if you have any other questions. SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
I was also able to solve the issue, I passed the self argument in the metadata, then utilized that field from metadata |
Beta Was this translation helpful? Give feedback.
-
These solutions to me sound more like workarounds. The proper solution is to have the support for using instance methods, which require a self parameter, for Agents where the tools themselves are part of a class. Maybe something that the people behind Langchain can fix? |
Beta Was this translation helpful? Give feedback.
-
I was reviewing the tools in the LangChain repository tools file There are two updates needed:
I managed to fix the problem by applying these updates, and it is now working fine! |
Beta Was this translation helpful? Give feedback.
-
I was able to solve my use case with the from langchain.tools.base import StructuredTool
from langgraph.prebuilt import create_react_agent
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o-mini")
class Example:
def __init__(self):
self.agent = create_react_agent(model, tools=[StructuredTool.from_function(self.magic_function)])
def magic_function(self, value: str) -> str:
"""Determine the magic value of the given value"""
return value + "... magic!"
example = Example()
result = example.agent.invoke(
{"messages": [HumanMessage("What is the magic value of 'Watermelon'?")]}
)
print(result["messages"][-1].content) In my case, I had to figure out to not use the |
Beta Was this translation helpful? Give feedback.
🤖
Hello,
The issue you're encountering is due to the fact that the
@tool
decorator is not designed to be used with instance methods, which require aself
parameter. When you use the@tool
decorator on an instance method, the AgentExecutor tries to supply theself
parameter, leading to theTypeError
you're seeing.One way to work around this is to use the
@staticmethod
decorator in conjunction with the@tool
decorator. This tells Python that the method does not require access to any instance or class-specific data, allowing it to be called without an instance of the class. Here's an example: