Struggling to pass multiple arguments to Openai function agent in Langchain

Hey guys, I’m stuck trying to create an agent with Langchain that can place orders for me. I’ve set up a function/Tool to handle the order placement, but it needs several inputs.

When I run my code, I keep getting this error: TypeError: __init__() takes 1 positional argument but 7 were given

Here’s a simplified version of what I’m working with:

class OrderPlacement(BaseModel):
    customer_name: str
    contact_number: str
    item_name: str
    birth_date: str
    item_count: str

class PlaceOrderTool(BaseTool):
    name = "submit_order"
    description = "Use this to place an order. Provide name, contact, item, birthdate, and quantity."

    def _run(self, customer_name: str, contact_number: str, item_name: str, birth_date: str, item_count: str):
        order_result = submit_order(customer_name, contact_number, birth_date, item_name, item_count)
        return order_result

    args_schema: Optional[Type[BaseModel]] = OrderPlacement

tools = [PlaceOrderTool()]
ai_agent = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)

a_agent.run("Place an order for Jane Doe, phone 1234567890, born 01/01/1990, for 3 blue widgets")

Can anyone spot what I’m doing wrong here? Thanks!

I’ve been using Langchain for a while now, and I’ve run into this exact problem before. The issue is with how the _run method is defined in your PlaceOrderTool class. Instead of specifying each argument separately, you need to use **kwargs to capture all the arguments at once.

Here’s what worked for me:

def _run(self, **kwargs):
    return submit_order(**kwargs)

This change allows the tool to accept any number of arguments defined in your OrderPlacement schema. It should resolve that TypeError you’re seeing.

Also, make sure your submit_order function can handle these keyword arguments correctly. You might need to unpack them within the function if it’s expecting individual parameters.

One more thing - double-check that your OrderPlacement schema matches exactly with what your submit_order function expects. Any mismatch there could cause issues down the line.

hey john, looks like ur issue might be with the _run method. try changing it to:

def _run(self, **kwargs):
    return submit_order(**kwargs)

this should handle multiple args better. lmk if that works!

yo john, i had the same problem last week. try this:

def _run(self, **kwargs):
    return submit_order(**kwargs)

it fixed it for me. also, make sure ur submit_order function can handle those kwargs. good luck man!

I encountered a similar issue when working with Langchain’s function agents. The problem lies in how the tool’s _run method is defined. Instead of listing all parameters individually, you should use **kwargs to capture all arguments passed to the function. Here’s how you can modify your PlaceOrderTool class:

class PlaceOrderTool(BaseTool):
    name = "submit_order"
    description = "Use this to place an order. Provide name, contact, item, birthdate, and quantity."

    def _run(self, **kwargs):
        return submit_order(**kwargs)

    args_schema: Optional[Type[BaseModel]] = OrderPlacement

This modification allows the tool to accept any number of arguments defined in your OrderPlacement schema. It should resolve the TypeError you’re encountering. Remember to ensure that your submit_order function can handle these keyword arguments correctly.