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

🐛 Fixed missing models field not skipping serialization on GuardrailsConfig #99

Merged
merged 6 commits into from
Jul 16, 2024

Conversation

pmcjr
Copy link
Collaborator

@pmcjr pmcjr commented Jun 25, 2024

This PR addresses #91, solved by (edit) adding validation to models field.

Added a test case regarding the issue as well.

@pmcjr pmcjr requested review from declark1 and removed request for declark1 June 25, 2024 18:38
Copy link
Collaborator

@evaline-ju evaline-ju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! I don’t think this is quite the behavior we want to resolve the JSON deserialization error, since we ultimately still want to give the user an error showing that models is a required field.

I think the update I would expect is for an update of deserialization, since I would expect the same error “Failed to deserialize…” to still surface if any other required inputs are left off, but we want those errors to give a clear error in code/details like the other errors.

@pmcjr pmcjr marked this pull request as draft June 25, 2024 18:55
@pmcjr pmcjr requested a review from evaline-ju July 1, 2024 16:49
@pmcjr pmcjr marked this pull request as ready for review July 1, 2024 20:47
@declark1
Copy link
Collaborator

declark1 commented Jul 3, 2024

It sounds like the objective is to:

  1. Simplify the error message, i.e. remove the excess "Failed to deserialize the JSON body into the target type:" bloat and just tell the user what is missing, e.g. "guardrail_config.input: missing field models at line 6 column 3"
  2. Return our custom server::Error type which should be returned as JSON in the following form:
    {
        "code": 422,
        "details": "some error message"
    }

Axum features a Json extractor that handles deserializing/serializing the requests/responses via serde behind the scenes. When deserialization fails, it returns a JsonRejection error. In the case of a missing field, the JsonRejection::JsonDataError variant is returned, and if you check the Display impl for it here, you can see this is where the "Failed to deserialize..." message is set.

What we need to do is map this to server::Error and customize the message returned for JsonRejection::JsonDataError to only include the lower-level serde_json error message that tells us what is missing. There are a few ways to approach this (examples here), the simplest being to leverage axum_extra::extract::WithRejection to transform JsonRejection to our custom error type server::Error, add a JsonExtractorRejection variant to server::Error, and then update IntoResponse impl for the variant.

I went ahead and gave this a try here. The error message returned for a missing field is now:

{
    "code": 422,
    "details": "guardrail_config.input: missing field `models` at line 6 column 3"
}

If this is sufficient, feel free to steal this. If the message needs to be customized further, it will be a more complicated task. You may be able to downcast &dyn std::error::Error (returned from JsonDataError.source()) to the actual serde_json error to customize it more, but I haven't tried this. I would recommend this approach versus converting existing fields to Option and adding in additional validation logic.

Btw, here is a thread with some useful information for reference.

@pmcjr pmcjr marked this pull request as draft July 3, 2024 19:50
@pmcjr pmcjr marked this pull request as ready for review July 5, 2024 19:23
Copy link
Collaborator

@declark1 declark1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of requested changes:

  • Rebase latest changes from main
  • You can drop ValidationError::Missing variant as it isn't being used

@pmcjr pmcjr requested a review from declark1 July 11, 2024 12:57
Copy link
Collaborator

@declark1 declark1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Since Evaline is out and these changes should also address her comments, will approve and merge.

@declark1 declark1 dismissed evaline-ju’s stale review July 16, 2024 17:02

Addressed by changes

@declark1 declark1 merged commit 90a4a39 into foundation-model-stack:main Jul 16, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failed to deserialize the JSON body into the target type: missing field models
3 participants