Skip to content

feat: support creating tools from functions with model parameters #913

Description

@puzzle-solver

Feature description

I would want to be able to use tools with defined by functions with parameters typed as pydantic models.

Motivation

Example use case: creating a cart with a list of items

class Cart(BaseModel):
    products: list[Product]
    
class Product(BaseModel):
    product_id: str
    quantity: int = Field(default=1, gt=0)

Then, I would want to define my tool like this:

def create_cart(cart: Cart) -> bool:
    """Creates a cart with items that a user can order"""
    if validate(cart):
        return "success"
    else:
        return "cart creation failed"

The agent definition would be just Agent(llm=llm, prompt=prompt, tools=[create_cart]).


Right now, specifying the tools this way does not capture the schema of a Cart and is not reflected in JSON schema that is created and send to the API.

The workaround for now is to use a custom tool:

def on_create_cart_tool_call(products: list):
    cart = Cart(products=products)
    return create_cart(cart)

Tool(
        name="create_cart",
        description="Creates a cart with items that a user can order",
        parameters=Cart.model_json_schema(),
        on_tool_call=on_create_cart_tool_call,
    )

However, this is much less clean.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions