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

const client = new Groq();

// Define a schema with Zod
const ProductSchema = z.object({
  id: z.string(),
  name: z.string(),
  price: z.number().positive(),
  description: z.string(),
  in_stock: z.boolean(),
  tags: z.array(z.string()).default([]),
});

// Create a prompt that clearly defines the expected structure
const systemPrompt = `
You are a product catalog assistant. When asked about products,
always respond with valid JSON objects that match this structure:
{
  "id": "string",
  "name": "string",
  "price": number,
  "description": "string",
  "in_stock": boolean,
  "tags": ["string"]
}
Your response should ONLY contain the JSON object and nothing else.
`;

async function getStructuredResponse() {
  try {
    // Request structured data from the model
    const completion = await client.chat.completions.create({
      model: "openai/gpt-oss-20b",
      response_format: { type: "json_object" },
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: "Tell me about a popular smartphone product" },
      ],
    });

    // Extract the response
    const responseContent = completion.choices[0].message.content;
    
    // Parse and validate JSON
    const jsonData = JSON.parse(responseContent || "");
    const validatedData = ProductSchema.parse(jsonData);
    
    console.log("Validation successful! Structured data:");
    console.log(JSON.stringify(validatedData, null, 2));
    
    return validatedData;
  } catch (error) {
    if (error instanceof z.ZodError) {
      console.error("Schema validation failed:", error.errors);
    } else if (error instanceof SyntaxError) {
      console.error("JSON parsing failed: The model did not return valid JSON");
    } else {
      console.error("Error:", error);
    }
  }
}

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

---

## Text Chat: Streaming Chat Completion (js)