Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Langchain agent with tools generates "StructuredTool is not JSON serializable" #24766

Open
5 tasks done
smolavipour opened this issue Jul 29, 2024 · 2 comments
Open
5 tasks done
Labels
Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@smolavipour
Copy link

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

The code is picked up from LangChain documentations
https://python.langchain.com/v0.2/docs/how_to/tools_chain/

from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.tools import tool


model = # A mistral model

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant"),
        ("placeholder", "{chat_history}"),
        ("human", "{input}"),
        ("placeholder", "{agent_scratchpad}"),
    ]
)

@tool
def multiply(first_int: int, second_int: int) -> int:
    """Multiply two integers together."""
    return first_int * second_int

@tool
def add(first_int: int, second_int: int) -> int:
    "Add two integers."
    return first_int + second_int


@tool
def exponentiate(base: int, exponent: int) -> int:
    "Exponentiate the base to the exponent power."
    return base**exponent


tools = [multiply, add, exponentiate]

# Construct the tool calling agent
agent = create_tool_calling_agent(model,tools, prompt)

# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

result = agent_executor.invoke(
    {
        "input": "Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result"
    }
)

Error Message and Stack Trace (if applicable)

TypeError: Object of type StructuredTool is not JSON serializable

Description

I am trying to run the sample code in https://python.langchain.com/v0.2/docs/how_to/tools_chain/ to call an agent equipped with tools. I see two problems:

  • If I run the code as it is, it generates the error that "Object of type StructuredTool is not JSON serializable".
  • If I create the agent with empty tools list (i.e., tools=[]) it generates the response. However, it is not supposed to be the right way of creating agents, as far as I understand. Besides the answer with mistral7b model is very inaccurate. Even in the example provided in the link above, the answer seems to be different and wrong when checking the langSmith run.

System Info

langchain-core==0.1.52
langchain==0.1.16

@dosubot dosubot bot added Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Jul 29, 2024
@smolavipour
Copy link
Author

The problem seems to be resolved if we use dumps function in LangChain:

from langchain.load.dump import dumps

tools = [multiply, add, exponentiate]
tools_ = [dumps(multiply), dumps(add), dumps(exponentiate)]

agent = create_tool_calling_agent(model,tools_, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

@baskaryan
Copy link
Collaborator

hm im not able to recreate. could you post the full stack trace and share how you're instantiating the model? could you also try bumping to langchain and langchain-core >=0.2?

Screenshot 2024-07-29 at 2 29 24 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants