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

[Bug]:don't use dict(self.pydantic_class(**response.parsed)) #466

Closed
wujiren opened this issue Oct 20, 2024 · 2 comments · Fixed by #471
Closed

[Bug]:don't use dict(self.pydantic_class(**response.parsed)) #466

wujiren opened this issue Oct 20, 2024 · 2 comments · Fixed by #471
Labels
bug Something isn't working

Comments

@wujiren
Copy link
Contributor

wujiren commented Oct 20, 2024

Describe the bug
in MarkdownJsonDictParser, the parse function use response.parsed = dict(self.pydantic_class(**response.parsed)) to transfer pydantic model to dict, it's not good for a nested data.
you should use response.parsed = self.pydantic_class(**response.parsed).model_dump() instead
this is the source code

        # Requirement checking by Pydantic
        if self.pydantic_class is not None:
            try:
                response.parsed = dict(self.pydantic_class(**response.parsed))
            except Exception as e:
                raise JsonParsingError(
                    message=str(e),
                    raw_response=response.text,
                ) from None

I will show you an example

To Reproduce
this is an example why don't do it.

from pydantic import BaseModel,Field
import json

class InsideSchema(BaseModel):
    key:str  = Field("value", description="key")

class NestedStructure(BaseModel):
    object: InsideSchema = Field(default_factory=InsideSchema, description="object")


if __name__ == "__main__":
    data = NestedStructure()
    print(data.model_dump()) # {'object': {'key': 'value'}}

    print(dict(data)) # {'object': InsideSchema(key='value')}

    try:
        json.dumps(dict(data))
    except Exception as e:
        print(e) # Object of type InsideSchema is not JSON serializable

the result will show that dict(*) won't parse complicated data, inside the 'object' is a pydantic data type InsideSchema(key='value'), not a dict {'key': 'value'}.
it will raise error when you dump the it.

@wujiren wujiren added the bug Something isn't working label Oct 20, 2024
@DavdGao
Copy link
Collaborator

DavdGao commented Oct 22, 2024

@wujiren Thanks for pointing out this issue. I agree with your change. Could you please create a pull request to support nested Pydantic models?

@wujiren
Copy link
Contributor Author

wujiren commented Oct 23, 2024

@wujiren Thanks for pointing out this issue. I agree with your change. Could you please create a pull request to support nested Pydantic models?

sure thing, coming soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants