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

Add TypeAdapter support for JSON #1308

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hoesler
Copy link

@hoesler hoesler commented Dec 2, 2024

Add support for pydantic TypeAdapter to allow schema objects to be generated from primitive types or lists for example (e.g. TypeAdapter(List[Transaction])).

@rlouf
Copy link
Member

rlouf commented Dec 2, 2024

Thank you! Could you add some tests?

@hoesler
Copy link
Author

hoesler commented Dec 2, 2024

I actually looked into adding a test, but found that the relevant test is skipped. So what would you suggest here?

@hoesler
Copy link
Author

hoesler commented Dec 2, 2024

BTW, I guess this could also replace the recently added enum support from #1277, as you can just do something like:

class Color(Enum):
    RED = 1
    BLUE = 2

generator = outlines.generate.json(model, TypeAdapter(Color))

@rlouf
Copy link
Member

rlouf commented Dec 4, 2024

Could you give me an example of how you would use this?

@hoesler
Copy link
Author

hoesler commented Dec 4, 2024

My motivation was to generate a list of objects:

class Transaction(BaseModel):
   ...

generator = outlines.generate.json(model, TypeAdapter(List[Transaction]))

But one could use it for primitives as well. See for example the comment above for Enums.

@rlouf
Copy link
Member

rlouf commented Dec 8, 2024

I wonder if there is a way to let users pass List[Object] and hide the use of TypeAdapter from them?

@hoesler
Copy link
Author

hoesler commented Dec 8, 2024 via email

@rlouf
Copy link
Member

rlouf commented Dec 8, 2024

You're probably right, we'll leave it as is for now. We just need to get to full coverage before merging!

@hoesler
Copy link
Author

hoesler commented Dec 9, 2024

Happy to add tests, but please give me some guidance here.
Intuitively I would add a schema_object parameter to test_generate_json in test_generate.py. But this test is skipped, because it fails for some models? Shouldn't we just skip those models instead of all? And by executing the test with all the different concrete models, doesn't that make it an integration test? If I'm right, how do you decide if a test goes to this file or to any of the test_integration_*.py files?

And should I remove the specific handling of the Enum type in favor of the TypeAdapter?

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.

2 participants