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

Seems like passing a discriminated union crashes #823

Closed
remiconnesson opened this issue Apr 16, 2024 · 1 comment · Fixed by #897
Closed

Seems like passing a discriminated union crashes #823

remiconnesson opened this issue Apr 16, 2024 · 1 comment · Fixed by #897
Labels

Comments

@remiconnesson
Copy link

Describe the issue as clearly as possible:

Seems like discriminated unions crashes outlines

Steps/code to reproduce the bug:

class Cat(BaseModel):
    pet_type: Literal['cat']
    meows: int


class Dog(BaseModel):
    pet_type: Literal['dog']
    barks: float


class Model(BaseModel):
    pet: Union[Cat, Dog] = Field(..., discriminator='pet_type')
    n: int

json_schema = Model.schema_json()

# and then use the schema with a vLLM endpoint


### Expected result:

```shell
To be able to pass a discriminated union

Error message:

InternalServerError: Internal Server Error

Outlines/Python version information:

Version information

``` (command output here) ```

Context for the issue:

This would allow for multiple tool calling with reliability, because JSON mode is not enough and Json Schema only allwo

lapp0 added a commit to lapp0/outlines that referenced this issue May 17, 2024
@lapp0
Copy link
Contributor

lapp0 commented May 17, 2024

Thanks for the good reproduction script and issue!

Schema:

{"$defs": {"Cat": {"properties": {"pet_type": {"const": "cat", "enum": ["cat"], "title": "Pet Type", "type": "string"}, "meows": {"title": "Meows", "type": "integer"}}, "required": ["pet_type", "meows"], "title": "Cat", "type": "object"}, "Dog": {"properties": {"pet_type": {"const": "dog", "enum": ["dog"], "title": "Pet Type", "type": "string"}, "barks": {"title": "Barks", "type": "number"}}, "required": ["pet_type", "barks"], "title": "Dog", "type": "object"}}, "properties": {"pet": {"discriminator": {"mapping": {"cat": "#/$defs/Cat", "dog": "#/$defs/Dog"}, "propertyName": "pet_type"}, "oneOf": [{"$ref": "#/$defs/Cat"}, {"$ref": "#/$defs/Dog"}], "title": "Pet"}, "n": {"title": "N", "type": "integer"}}, "required": ["pet", "n"], "title": "Model", "type": "object"}

pattern:

\{[\n ]*"pet"[\n ]*:[\n ]*((\{[\n ]*"pet_type"[\n ]*:[\n ]*("cat")[\n ]*,[\n ]*"meows"[\n ]*:[\n ]*(-)?(0|[1-9][0-9]*)[\n ]*\})(?!.*(\{[\n ]*"pet_type"[\n ]*:[\n ]*("dog")[\n ]*,[\n ]*"barks"[\n ]*:[\n ]*((-)?(0|[1-9][0-9]*))(\.[0-9]+)?([eE][+-][0-9]+)?[\n ]*\}))|(\{[\n ]*"pet_type"[\n ]*:[\n ]*("dog")[\n ]*,[\n ]*"barks"[\n ]*:[\n ]*((-)?(0|[1-9][0-9]*))(\.[0-9]+)?([eE][+-][0-9]+)?[\n ]*\})(?!.*(\{[\n ]*"pet_type"[\n ]*:[\n ]*("cat")[\n ]*,[\n ]*"meows"[\n ]*:[\n ]*(-)?(0|[1-9][0-9]*)[\n ]*\})))[\n ]*,[\n ]*"n"[\n ]*:[\n ]*(-)?(0|[1-9][0-9]*)[\n ]*\}

Error:

interegular.patterns.Unsupported: Group can not have lookbacks/lookaheads that go beyond the group bounds.

This PR should resolve your issue, please let me know if it works! #897

rlouf pushed a commit that referenced this issue May 17, 2024
Fixes #823

This comment details the issues error:
#823 (comment)

The reproduction code provided results in a json schema with
`OneOf[pets]`:

```
class Model(BaseModel):
    pet: Union[Cat, Dog] = Field(..., discriminator='pet_type')
```

Before this PR: `OneOf` uses negative lookaheads to assert that only one
schema member is included. This is illegal in `interegular`, more
details available here:
#456

After `OneOf` uses or-joined non-capturing groups which don't have the
same issues with `interegular`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants