JSON Generation Guide
LLM usage is not limited to Chatbot’s and human readable text generation.
In prompt configuration choose “Output type”:
- Object
- Array
- Enum (coming soon!)
- No Schema (coming soon!)
When we expect LLM to return an object or an array we need to be sure that LLM indeed returns the response in correct format, which is not always true especially if the schema conflicts with rules described in System message.
I strongly recommend setting strict output schema in Mensa instead of parsing and validating the output manually.
- When you define the schema it is converted to compatible format and passed to LLM provider together with the prompt, which increases the likelihood of correct output considerably because most of modern models are trained to follow the schema.
- Mensa auto-generates client validation code for you and ensures it always match the schema that is send to the model.
Mensa supports defining schema using Zod as well as JSON Schema. Both schemas are automatically synced with each other.
For example, we create a seed generator that produces fake user.
System prompt:
You are a seed data generator. Generate an object representing a user with name, salary and birth date.
Output type:
Object
Schema:
z.object({ name: z.string(), salary: z.number().min(1000).max(100000), birthDate: z.date(),}).strict();Use auto-generated code for integration, and inspect object in your editor.
It is strongly typed. Start typing object.n and it will suggest object.name. Try to access object.age and you will see Typescript error.
Further reading
Section titled “Further reading”- Remember that changing output format or schema is a Breaking Change