```javascript
import Instructor from "@instructor-ai/instructor"; // npm install @instructor-ai/instructor
import { Groq } from "groq-sdk";
import { z } from "zod"; // npm install zod

// Set up the client with Instructor
const groq = new Groq();
const instructor = Instructor({
  client: groq,
  mode: "TOOLS"
})

// Define a complex nested schema
const AddressSchema = z.object({
  street: z.string(),
  city: z.string(),
  state: z.string(),
  zip_code: z.string(),
  country: z.string(),
});

const ContactInfoSchema = z.object({
  email: z.string().email(),
  phone: z.string().optional(),
  address: AddressSchema,
});

const ProductVariantSchema = z.object({
  id: z.string(),
  name: z.string(),
  price: z.number().positive(),
  inventory_count: z.number().int().nonnegative(),
  attributes: z.record(z.string()),
});

const ProductReviewSchema = z.object({
  user_id: z.string(),
  rating: z.number().min(1).max(5),
  comment: z.string(),
  date: z.string(),
});

const ManufacturerSchema = z.object({
  name: z.string(),
  founded: z.string(),
  contact_info: ContactInfoSchema,
});

const ProductSchema = z.object({
  id: z.string(),
  name: z.string(),
  description: z.string(),
  main_category: z.string(),
  subcategories: z.array(z.string()),
  variants: z.array(ProductVariantSchema),
  reviews: z.array(ProductReviewSchema),
  average_rating: z.number().min(1).max(5),
  manufacturer: ManufacturerSchema,
});

// System prompt with clear instructions about the complex structure
const systemPrompt = `
You are a product catalog API. Generate a detailed product with ALL required fields.
Your response must be a valid JSON object matching the schema I will use to validate it.
`;

async function getComplexProduct() {
  try {
    // Use instructor to create and validate in one step
    const product = await instructor.chat.completions.create({
      model: "openai/gpt-oss-20b",
      response_model: {
        name: "Product",
        schema: ProductSchema,
      },
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: "Give me details about a high-end camera product" },
      ],
      max_retries: 3,
    });

    // Print the validated complex object
    console.log(`Product: ${product.name}`);
    console.log(`Description: ${product.description.substring(0, 100)}...`);
    console.log(`Variants: ${product.variants.length}`);
    console.log(`Reviews: ${product.reviews.length}`);
    console.log(`Manufacturer: ${product.manufacturer.name}`);
    console.log(`\nManufacturer Contact:`);
    console.log(`  Email: ${product.manufacturer.contact_info.email}`);
    console.log(`  Address: ${product.manufacturer.contact_info.address.city}, ${product.manufacturer.contact_info.address.country}`);

    return product;
  } catch (error) {
    console.error("Error:", error);
  }
}

// Run the example
getComplexProduct();
```

---

## Text Chat: Basic Validation Zod (js)