You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are currently using classes to define the output types on the v1.0 branch. For instance, to define a Json output type we use the outlines.types.Json class:
And the behavior of the model is determined by dispatching based on the class passed to Generator. However, this introduces complexity and ambiguity as we will show in the following.
Passing custom types to initialize the Generator
We recently introduced custom types to abstract common regular expression, for instance the regular expression that represents US phone numbers. It is defined as:
We use the Annotated python type so this custom type can be used in Pydantic models. We would naturally want to be able to build a Generator object by passing USPhoneNumber directly as follows:
But then the type of the objects that we pass to Generator are different. Sometimes a class instance, sometimes a type annotation. This is a design disaster waiting to happen, and will introduce a lot of complexity to the code down the line.
Use Python type annotations such as List and Union
Defining the output type as List[Json(...)] or Union[Json(...)] will raise an exception, since these parametric types can only be parametrized by types, and Json(...) is an instance of the Json class. This is very confusing from a user perspective; it is not clear why these types would not be supported.
Use Annotated types everywhere
I thus suggest that we use type constructors that generate an Annotated type. For instance:
This way we can also support Tuple, Union, and other composite types. As a bonus, we can configure them so that they can be serialized by Pydantic into the correct Json Schema.
The text was updated successfully, but these errors were encountered:
We are currently using classes to define the output types on the
v1.0
branch. For instance, to define a Json output type we use theoutlines.types.Json
class:And the behavior of the model is determined by dispatching based on the class passed to
Generator
. However, this introduces complexity and ambiguity as we will show in the following.Passing custom types to initialize the
Generator
We recently introduced custom types to abstract common regular expression, for instance the regular expression that represents US phone numbers. It is defined as:
We use the
Annotated
python type so this custom type can be used in Pydantic models. We would naturally want to be able to build aGenerator
object by passingUSPhoneNumber
directly as follows:But then the type of the objects that we pass to
Generator
are different. Sometimes a class instance, sometimes a type annotation. This is a design disaster waiting to happen, and will introduce a lot of complexity to the code down the line.Use Python type annotations such as
List
andUnion
Defining the output type as
List[Json(...)]
orUnion[Json(...)]
will raise an exception, since these parametric types can only be parametrized by types, andJson(...)
is an instance of theJson
class. This is very confusing from a user perspective; it is not clear why these types would not be supported.Use
Annotated
types everywhereI thus suggest that we use type constructors that generate an
Annotated
type. For instance:This way we can also support
Tuple
,Union
, and other composite types. As a bonus, we can configure them so that they can be serialized by Pydantic into the correct Json Schema.The text was updated successfully, but these errors were encountered: