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

Improved typing when using Pydantic models #1107

Open
robinvandernoord opened this issue Aug 21, 2024 · 1 comment
Open

Improved typing when using Pydantic models #1107

robinvandernoord opened this issue Aug 21, 2024 · 1 comment

Comments

@robinvandernoord
Copy link

What behavior of the library made you think about the improvement?

generator = outlines.generate.json(model, MyModel)
answer = generator(prompt)

Answer is now type hinted as str | int | ... but we know it should be MyModel.
When I explicitly define answer: MyModel = generator(prompt) it also says the types do not match.

How would you like it to behave?

SequenceGenerator could be generic over the provided pydantic model when using outlines.generate.json, so the return value of __call__ would be the right data structure.
This would probably be harder to do for other outlines.generate methods, but for pydantic it would be very nice.

@robinvandernoord
Copy link
Author

robinvandernoord commented Aug 21, 2024

I created a hacky example that roughly does what I mean:

T = typing.TypeVar("T")


class GenericSequenceGenerator(SequenceGenerator, typing.Generic[T]):
    def __call__(self, *args, **kwargs) -> T:
        return typing.cast(
            T,
            super().__call__(*args, **kwargs)
        )


def outlines_generate_json(model, klass: typing.Type[T]) -> GenericSequenceGenerator[T]:
    return typing.cast(
        T,
        outlines.generate.json(model, klass)
    )

If this conflicts with other behavior of generate.json, perhaps it would be wise to split the pydantic generation into something like generate.pydantic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant