defineSchema
Creates a type-safe schema definition for LLM programs.
Signature
function defineSchema<I extends ZodRawShape, O extends ZodRawShape>(
definition: SchemaDefinition<I, O>
): Schema<I, O>Parameters
| Parameter | Type | Description |
|---|---|---|
| definition.description | string | What the LLM should do (becomes the system prompt) |
| definition.inputs | ZodRawShape | Input field definitions |
| definition.outputs | ZodRawShape | Output field definitions |
Returns
A Schema object with the following properties and methods:
| Property/Method | Type | Description |
|---|---|---|
| description | string | The schema description |
| inputs | ZodObject | Zod schema for inputs |
| outputs | ZodObject | Zod schema for outputs |
| getInputKeys() | string[] | Returns array of input field names |
| getOutputKeys() | string[] | Returns array of output field names |
| validateInput(data) | I | Validates and returns typed input |
| validateOutput(data) | O | Validates and returns typed output |
Examples
Basic Usage
import { defineSchema, z } from "@mzhub/promptc";
const Summarizer = defineSchema({
description: "Summarize the given text in 2-3 sentences",
inputs: {
text: z.string(),
maxLength: z.number().optional()
},
outputs: {
summary: z.string(),
wordCount: z.number()
}
});With Descriptions
const EntityExtractor = defineSchema({
description: "Extract named entities from text",
inputs: {
text: z.string().describe("The source text"),
types: z.array(z.string()).describe("Entity types to extract")
},
outputs: {
entities: z.array(z.object({
name: z.string(),
type: z.string(),
confidence: z.number().min(0).max(1)
}))
}
});Validation
const schema = defineSchema({
description: "Test",
inputs: { name: z.string() },
outputs: { greeting: z.string() }
});
// Validate input (throws on failure)
const validInput = schema.validateInput({ name: "Alice" });
// Validate output
const validOutput = schema.validateOutput({ greeting: "Hello, Alice!" });
// Get field names
console.log(schema.getInputKeys()); // ["name"]
console.log(schema.getOutputKeys()); // ["greeting"]See also: Schemas & Zod Guide