Data Schemas
It is possible to force the LLM to generate data that conforms to a specific schema. This technique works reasonably well and GenAIScript also provides automatic validation “just in case”.
You will notice that the schema supported by GenAIScript is much simpler than the full-blow JSON schema specification. We recommend using simple schemas to avoid confusing the LLM; then port them to your application specific data format later on.
defSchema
Use defSchema
to define a JSON/YAML schema for the prompt output.
👤 user
🤖 assistant
Native zod support
A Zod type can be passed in defSchema
and it will be automatically converted to JSON schema.
The GenAIScript also exports the z
object from Zod for convenience.
Prompt encoding
Following the “All You Need Is Types” approach from TypeChat, the schema is converted TypeScript types before being injected in the LLM prompt.
You can change this behavior by using the { format: "json" }
option.
Use the schema
Then tell the LLM to use this schema to generate data.
Validation
When a JSON/YAML payload is generated with the schema identifier, GenAIScript automatically validates the payload against the schema.
Repair
GenAIScript will automatically try to repair the data by issues additional messages back to the LLM with the parsing output.
Runtime Validation
Use parsers.validateJSON
to validate JSON when running the script.