crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

fix: pydantic schema parser now supports nested objects

Open inchoate opened this issue 1 year ago • 4 comments

Updated.

fix: fixes pydantic parser to support nested objects.

This fails before the PR and succeeds afterward:

from pydantic import BaseModel
from typing import List, Optional

from crewai.utilities.pydantic_schema_parser import PydanticSchemaParser

class InnerModel(BaseModel):
    inner_field: int

class TestModel(BaseModel):
    simple_field: str
    list_field: List[int]
    optional_field: Optional[str]
    nested_model: InnerModel

print(PydanticSchemaParser(model=InnerModel).get_schema())   # works
print(PydanticSchemaParser(model=TestModel).get_schema())    # fails

Note. Because the main branch currently doesn't support nested schemas and the original code against which I made this PR months ago drifted, I made a judgement call on how to format the nested structures. I chose the following, inferred from the current code:

>>> print(PydanticSchemaParser(model=InnerModel).get_schema())
{
    inner_field: int
}

>>> print(PydanticSchemaParser(model=TestModel).get_schema())
{
    simple_field: str,
    list_field: List[int],
    optional_field: Optional[str],
    nested_model: InnerModel
    {
        inner_field: int
    }
}
```

inchoate avatar Jul 10 '24 16:07 inchoate

Thanks for this. Struggling with this error atm. Hopefully this is merged.

e4stwood avatar Aug 04 '24 13:08 e4stwood

Nice! I see there are some conlicts, I'll try to fix it

joaomdmoura avatar Aug 10 '24 19:08 joaomdmoura

Hey @inchoate! Thanks for your pull request!

We've made quite a few changes since July, so could you take a moment to check if everything is still relevant and working as it should? If it is, just give us a shout— we’d love to help you get it merged!

Thanks

pythonbyte avatar Dec 09 '24 14:12 pythonbyte

Updated.

@pythonbyte @joaomdmoura

Let me know if you want a new PR b/c of the force push.

inchoate avatar Dec 09 '24 23:12 inchoate