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

Allow model_validate_json to return valid JSON string upon raising error #9371

Open
3 of 13 tasks
timothylimyl opened this issue May 2, 2024 · 3 comments
Open
3 of 13 tasks

Comments

@timothylimyl
Copy link

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

Currently, I am using model_validate_json to validate the JSON string output.

By default, I am hoping to get the JSON output without the fields that Pydantic has raised error on.

For example, if my class is as such:

class Human(BaseModel):
    name: str
    age: float
    hobby: list[str]

but the JSON output is:
{ "name" : "Tim", "age": 20.0, "hobby" : "coding"}

It will raised the validation error, indicating "hobby" is type str but it is suppose to be type list[str]. Given that the validate function knows which field is errorneous, can it remove that field away from the JSON and return me the valid JSON instead?

so the code typically look something like this:

try:
  human_json_string = model_output_json(....)
  Human.model_validate_json(json_data = human_json_string)
  # now we know the JSON is all good, we can load it 
  human_data = json.loads(human_json_string)
except ValidationError as e:
  print(f"Error message from Pydantic: {e}\n")

within this code, I want to get the valid json that I can load to still display valid fields instead of failing the entire JSON output from the model

Docs: https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_validate_json

Affected Components

@timothylimyl
Copy link
Author

I reckon this is a common feature that people will want for use cases where JSON data output is non-deterministic and partial information is still valid for the application to "move on".

@timothylimyl
Copy link
Author

As an example, I believe Jason Liu himself also brought up this use case @ 15.22: https://www.youtube.com/watch?v=yj-wSRJwrrc

If we set up a validator to check whether the JSON output of the field has answer that is containing in the text that we have provided it, I believe in an application flow, we will want to continue with the answers that we have and deal with the fields that has been validated as false (no reference in text).

@sydney-runkle
Copy link
Member

Hi @timothylimyl,

This is a solid feature request. I think our partial JSON parsing offers some support for this, but doesn't entirely cover the cases you've outlined above.

Leaving this open, PRs + API design ideas are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants