```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 Groq client with Instructor
const client = new Groq();
const instructor = Instructor({
  client,
  mode: "TOOLS"
});

// Define your schema with Zod
const RecipeIngredientSchema = z.object({
  name: z.string(),
  quantity: z.string(),
  unit: z.string().describe("The unit of measurement, like cup, tablespoon, etc."),
});

const RecipeSchema = z.object({
  title: z.string(),
  description: z.string(),
  prep_time_minutes: z.number().int().positive(),
  cook_time_minutes: z.number().int().positive(),
  ingredients: z.array(RecipeIngredientSchema),
  instructions: z.array(z.string()).describe("Step by step cooking instructions"),
});

async function getRecipe() {
  try {
    // Request structured data with automatic validation
    const recipe = await instructor.chat.completions.create({
      model: "openai/gpt-oss-20b",
      response_model: {
        name: "Recipe",
        schema: RecipeSchema,
      },
      messages: [
        { role: "user", content: "Give me a recipe for chocolate chip cookies" },
      ],
      max_retries: 2, // Instructor will retry if validation fails
    });

    // No need for try/catch or manual validation - instructor handles it!
    console.log(`Recipe: ${recipe.title}`);
    console.log(`Prep time: ${recipe.prep_time_minutes} minutes`);
    console.log(`Cook time: ${recipe.cook_time_minutes} minutes`);
    console.log("\nIngredients:");
    recipe.ingredients.forEach((ingredient) => {
      console.log(`- ${ingredient.quantity} ${ingredient.unit} ${ingredient.name}`);
    });
    console.log("\nInstructions:");
    recipe.instructions.forEach((step, index) => {
      console.log(`${index + 1}. ${step}`);
    });

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

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

---

## Text Chat: Json Mode (js)