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

Support recursive arrays in JSON when an item is an array #316

Merged
merged 1 commit into from
Oct 19, 2023

Conversation

AL-377
Copy link
Contributor

@AL-377 AL-377 commented Oct 10, 2023

I have noticed that the origin outlines doesn't support recursive array type. To be clear, here is a example:

from outlines.text.json_schema import build_regex_from_schema
from pydantic import BaseModel
from typing import List
import json 

class ItemModel(BaseModel):
    name:str

class ArrayModel(BaseModel):
    items:List[List[ItemModel]]

test_json = \
{
    "items": [[{"name":"item1"},{"name":"item2"}]], 
}


json_schema = ArrayModel.model_json_schema()
print(json.dumps(json_schema,indent=2))

regex = build_regex_from_schema(json.dumps(json_schema))
print(regex)

parsed = ArrayModel.model_validate_json(json.dumps(test_json))
print(parsed)

Run the code above, then we can get the json schema of ArrayModel , but we will get a error from build_regex_from_schema when we meet the List[List[]] type property.

And now, I have modified the code in outlines and it works. You can check the test_json_schema.py

.DS_Store Outdated Show resolved Hide resolved
test.py Outdated Show resolved Hide resolved
@AL-377
Copy link
Contributor Author

AL-377 commented Oct 13, 2023

It is strange that the pytest here seems to fail intest_continuation.py, I have passed the test_continuation.py in my local machine.

image

Copy link
Contributor Author

@AL-377 AL-377 left a comment

Choose a reason for hiding this comment

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

The changes have done

@rlouf
Copy link
Member

rlouf commented Oct 19, 2023

Thank you for the contribution and sorry for the delay in reviewing. Will get to it soon.

@brandonwillard brandonwillard changed the title support the recursive array when the item is still a array Support recursive arrays in JSON when an item is an array Oct 19, 2023
@brandonwillard
Copy link
Contributor

@AL-377, this was a great fix/enhancement; much appreciated! I've added some minor refactoring and it should be ready to merge once everything finishes in CI.

@brandonwillard brandonwillard merged commit 07c47db into outlines-dev:main Oct 19, 2023
5 checks passed
@AL-377
Copy link
Contributor Author

AL-377 commented Oct 20, 2023

@AL-377, this was a great fix/enhancement; much appreciated! I've added some minor refactoring and it should be ready to merge once everything finishes in CI.

👍👍👍

@AL-377
Copy link
Contributor Author

AL-377 commented Oct 20, 2023

Thank you for the contribution and sorry for the delay in reviewing. Will get to it soon.

Never mind!

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.

None yet

3 participants