Docs
Core concepts
Output

Output

Output method allows you to define the expected structure of the data returned by your LLM function.

Usage

To define the output schema, you can pass a zod schema as an argument to the output function. Zod (opens in a new tab) is a schema validation library that provides a convenient way to define and validate data schemas.

import { z } from "zod";
import { createAiFunction } from "llm-functions-ts";
 
const outputSchema = z.object({
  countryNames: z.array(z.string()),
});
 
// Pass the output schema to the output function
const generateCountryNames = createAiFunction
  .output(outputSchema)
  .create()

Here's the generated type signature of your LLM function

() => Promise<{
  countryNames: string[];
}>;

This allows you to query an LLM and return a data structure that conforms to the types already defined in your existing codebase.

Validation of Response with Zod Schema

llm-functions-ts ensures that the response from the LLM function adheres to the specified output schema. When a response is received, it is validated against the output schema using the Zod library. If the response fails the validation, indicating that it doesn't match the expected structure, llm-functions-ts will loop back the response and the validation error to the LLM to obtain a new response that conforms to the defined schema.