Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pydantic schema parser now supports nested objects #911

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

inchoate
Copy link

@inchoate inchoate commented Jul 10, 2024

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
    }
}
```

@e4stwood
Copy link

e4stwood commented Aug 4, 2024

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

@theCyberTech theCyberTech added documentation Improvements or additions to documentation improvement labels Aug 10, 2024
@joaomdmoura
Copy link
Collaborator

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

@pythonbyte
Copy link
Collaborator

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

@inchoate inchoate force-pushed the pydantic-parser-fix-and-docs branch 2 times, most recently from 0fbe0c4 to 0ab0fcd Compare December 9, 2024 23:00
@inchoate
Copy link
Author

inchoate commented Dec 9, 2024

Updated.

@pythonbyte @joaomdmoura

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

This fails before the PR and succeeds afterward:
```python
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
structurees. 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 inchoate force-pushed the pydantic-parser-fix-and-docs branch from 0ab0fcd to af0ed9a Compare December 10, 2024 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants