Cookbook

Recipes for extracting typed data and making model decisions. The snippets omit process startup and credential loading.

Text and structured output

Use these recipes to parse model output into Zig types. See the client guide for ordinary text generation without a schema.

Decisions

Use these recipes for fixed questions with typed choices and probabilities, rather than extracting strings or nested objects. See the Decisions guide for provider configuration.

Shared setup

Product extraction and the Decisions recipes take a configured *instructor.Client. Configure it as described in the client guide. Decisions requires OpenAI or a TypeSafe-compatible service.

The invoice, meeting, classification, and review functions take a *instructor.Session(instructor.OpenAI). Create the lower-level provider and session once, with application values for allocator, io, and api_key:

Zig
const instructor = @import("instructor");

var provider = instructor.OpenAI.init(.{
    .allocator = allocator,
    .io = io,
    .api_key = api_key,
    .base_url = "https://openrouter.ai/api/v1",
    .endpoint = .chat_completions,
});
defer provider.deinit();
var session = instructor.session(allocator, &provider);
defer session.deinit();

Pass &session to those functions. Image inspection uses a separate OpenAI Responses provider, as noted in that recipe. Keep providers alive until their sessions are deinitialized. Session values remain valid until session.reset() or session.deinit(); they have no individual deinit method.