jsonSchemaConstraint field in GenerationOptions will enable constrained generation. While it is possible to
directly set the constraint with raw JSON Schema strings, we recommend to create the constraints with the Generatable annotation.
Generatable annotation
Generatable, and all the fields of the data class should be declared in the parameter of the constructor. Guide annotation can be used to add further descriptions to the fields.
For example, here is a generatable data class for holding information of a city:
Apply Generatable data class in GenerationOptions
Then we can usesetResponseFormatType() in GenerationOptions to set up the constraint.
JSONSchemaGenerator:
LeapGeneratableSchematizationException will be thrown.
Deserialize Generatable data class
Finally, you can useGeneratableFactory.createFromJSONObject() to deserialize the JSON string generated by the model into the generatable data class:
LeapGeneratableDeserializationException will be thrown.
Supported data types
Not all Kotlin data types are supported in Leap Android SDK constrained generation. Here is the list of supported JSON Schema types:- String (
Stringin Kotlin) - Integer (
IntandLongin Kotlin) - Number (
FloatandDoublein Kotlin) - Boolean (
Booleanin Kotlin) - Enum (Enum class in Kotlin. The plain name string will be used as values)
- Object (Any data classes annotated with [Generatable])
- Array (
ListandMutableListof any other supported data types, and arrays of integer, float numbers and boolean in Kotlin)