Enhanced Tool Definitions with Complex Schemas

Complex Schema Support for Tool Arguments

You can now use complex Pydantic schemas to define arguments for tools, enabling better type safety and validation for your tool inputs.

1from pydantic import BaseModel
2from typing import List, Optional
3
4class ItemData(BaseModel):
5 name: str
6 sku: str
7 price: float
8 description: Optional[str] = None
9
10class InventoryEntry(BaseModel):
11 item: ItemData
12 location: str
13 current_stock: int
14 minimum_stock: int = 5
15
16class InventoryEntryData(BaseModel):
17 data: InventoryEntry
18 quantity_change: int

Tool Creation from Function with Complex Schema

Use the args_schema parameter to specify a Pydantic model for tool arguments when creating tools from functions.

1from letta_client import Letta
2
3client = Letta(
4 token="YOUR_API_KEY",
5)
6
7def manage_inventory_mock(data: InventoryEntry, quantity_change: int) -> bool:
8 """
9 Implementation of the manage_inventory tool
10 """
11 print(f"Updated inventory for {data.item.name} with a quantity change of {quantity_change}")
12 return True
13
14tool_from_func = client.tools.upsert_from_function(
15 func=manage_inventory_mock,
16 args_schema=InventoryEntryData,
17)

BaseTool Class Extension

For more complex tool implementations, you can also extend the BaseTool class to create custom tools with full control over the implementation.

1from letta_client import BaseTool
2from typing import Type, List
3from pydantic import BaseModel
4
5class ManageInventoryTool(BaseTool):
6 name: str = "manage_inventory"
7 args_schema: Type[BaseModel] = InventoryEntryData
8 description: str = "Update inventory catalogue with a new data entry"
9 tags: List[str] = ["inventory", "shop"]
10
11 def run(self, data: InventoryEntry, quantity_change: int) -> bool:
12 """
13 Implementation of the manage_inventory tool
14 """
15 # implementation
16 print(f"Updated inventory for {data.item.name} with a quantity change of {quantity_change}")
17 return True
18
19custom_tool = client.tools.add(
20 tool=ManageInventoryTool(),
21)