Overview

The generateObject method allows you to generate structured objects from your workflows using a Zod or JSON schema. This is useful when you want to generate structured data such as lists, entities, or custom objects.

Usage

import Itzam from "itzam";
import { z } from "zod";

const itzam = new Itzam("YOUR_API_KEY");

const schema = z.array(
  z.object({
    name: z.string(),
    age: z.number(),
    profession: z.string(),
    description: z.string(),
  })
);

const response = await itzam.generateObject({
  input: "tell me who are the characters in atlas shrugged",
  workflowSlug: "code-assistant",
  schema,
});

console.log(response.object); // The generated object
console.log(response.metadata); // Metadata about the run

Parameters

ParameterTypeDescription
inputstringThe input text to generate a response for.
workflowSlugstringThe slug of the Workflow to use for generation.
groupIdstringOptional. Identifier for grouping related runs.
schemaZodType/JsonSchemaThe Zod or JSON schema describing the expected object structure.

Return Value

The generateObject method returns the following object:

type GenerateObjectResponse<T> = {
  object: T; // The generated object based on your schema
  metadata: {
    runId: string; // The ID of the run
    cost: string; // The cost of the run in USD
    model: {
      name: string; // The name of the model
      tag: string; // The tag of the model
    };
    durationInMs: number; // The duration of the run in milliseconds
    inputTokens: number; // The number of input tokens used for this generation
    outputTokens: number; // The number of output tokens used for this generation
  };
};

Error Handling

All errors thrown by the SDK extend from the base ItzamError class. See Error Handling for details.

Inspecting Runs

You can use the runId from the response metadata to fetch more details about the run using getRunById.