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

ParameterTypeDescription
definition.descriptionstringWhat the LLM should do (becomes the system prompt)
definition.inputsZodRawShapeInput field definitions
definition.outputsZodRawShapeOutput field definitions

Returns

A Schema object with the following properties and methods:

Property/MethodTypeDescription
descriptionstringThe schema description
inputsZodObjectZod schema for inputs
outputsZodObjectZod schema for outputs
getInputKeys()string[]Returns array of input field names
getOutputKeys()string[]Returns array of output field names
validateInput(data)IValidates and returns typed input
validateOutput(data)OValidates 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"]