Skip to content

Optional keywords on Objects/List or any other Datatypes that wraps other BaseModel #489

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

Closed
victai opened this issue Dec 5, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@victai
Copy link

victai commented Dec 5, 2023

For example:

class Dog(BaseModel):
  name: str
  breed: Optional[str]

class Cat(BaseModel):
  name: str
  breed: Optional[str]

class People(BaseModel):
  dogs: Optional[list[Dog]]
  cats: list[Cat]

When I ask the model to output People, currently Guard accepts this

{
  'dogs': [
    {'name': 'dog_name1'},
    {'name': 'dog_name2', 'breed': 'Chihuahua'}
  ]
  'cats': [
    {'name': 'cat_name1'}
  ]
}

But not this

{
  'cats': [
    {'name': 'cat_name1'}
  ]
}

The issue I'm facing is sometimes the model does not even output 'dogs': [] when the model thinks that there are no dogs in the prompt.

Quick Fix

I added

if not value:
    return validation

In the List and Object classes in guardrails/datatypes.py to workaround this. However, I'm not sure if there are potential risks by doing so.

class List(NonScalarType):
    """Element tag: `<list>`"""
    tag = "list"
    def collect_validation(
        self,
        key: str,
        value: Any,
        schema: Dict,
    ) -> FieldValidation:
        # Validators in the main list data type are applied to the list overall.
        validation = self._constructor_validation(key, value)

        if len(self._children) == 0:
            return validation
        if not value:
            return validation
@victai victai added the enhancement New feature or request label Dec 5, 2023
@irgolic
Copy link
Contributor

irgolic commented Dec 6, 2023

Hey, thanks for this issue, we'll look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants