Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use nested Pydantic models for function parameters in Claudetools


Can I use nested Pydantic models for function parameters in Claudetools


Yes, you can use nested Pydantic models for defining function parameters in Claudetools. Here's how you can do it:

1. Define your nested Pydantic models:

python
from pydantic import BaseModel, Field

class Album(BaseModel):
    title: str
    release_date: str

class Band(BaseModel):
    name: str 
    genre: str
    albums: list[Album]

2. Implement your function that takes the nested Pydantic model as a parameter:

python
def get_band_info(params: Band):
    return f"Band: {params.name}, Genre: {params.genre}, Albums: {', '.join([album.title for album in params.albums])}"

3. Create a list of function definitions with the nested model schema:

python
functions = [
    {
        "name": "get_band_info",
        "description": "Get information about a band and its albums",
        "parameters": Band.schema()
    }
]

4. Call the Claudetools Tool with the list of functions and other required parameters:

python
from claudetools.tools.tool import Tool

tool = Tool(ANTHROPIC_API_KEY)
output = tool(model="claude-3-sonnet-20240229", messages=user_messages, tools=functions, ...)

When you call the tool with the nested Pydantic model, Claudetools will automatically handle parsing and validating the input data according to the nested model schema you defined[1][2][5].

This allows you to define complex data structures as function parameters and ensures type safety and consistency in your function calls. The nested models will be automatically included in the JSON schema generated for the function definitions[1].

Citations:
[1] https://www.youtube.com/watch?v=yD_oDTeObJY
[2] https://github.com/vatsalsaglani/claudetools
[3] https://github.com/pydantic/pydantic/discussions/4741
[4] https://pub.towardsai.net/you-wont-believe-how-this-python-library-unlocks-gpt-4-level-features-with-claude-3-293e675f2bd8?gi=cbb8dee0031f
[5] https://www.youtube.com/watch?v=ID9b4diFZN8