26 Maret 20267 min read

Developer's Guide to Structured LLM Outputs & Function Calling in Production

Achieving 100% reliable JSON schema responses from OpenAI and Anthropic models with Zod validation, tool calling, and structured error recovery.

AI/MLLLMOpenAIPrompt EngineeringTypeScriptZod

Unpredictable natural language outputs from LLMs break backend APIs. In production applications, developers require deterministic, strictly typed JSON structures guaranteed to match validation schemas.


1. Defining Zod Output Schema

import { z } from "zod";

export const NutritionAnalysisSchema = z.object({
  foodName: z.string().describe("Name of the identified food item"),
  estimatedCalories: z.number().describe("Total calorie count in kcal"),
  macronutrients: z.object({
    proteinGrams: z.number(),
    carbGrams: z.number(),
    fatGrams: z.number(),
  }),
  healthTips: z.array(z.string()).describe("Actionable dietary recommendations"),
  confidenceScore: z.number().min(0).max(1),
});

export type NutritionAnalysis = z.infer<typeof NutritionAnalysisSchema>;

2. Executing Structured Output via OpenAI SDK

import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { NutritionAnalysisSchema } from "./schema";

const openai = new OpenAI();

export async function analyzeMealImage(base64Image: string) {
  const response = await openai.beta.chat.completions.parse({
    model: "gpt-4o-2024-08-06",
    messages: [
      { role: "system", content: "You are an expert clinical dietitian." },
      {
        role: "user",
        content: [
          { type: "text", text: "Analyze the nutritional breakdown of this meal." },
          { type: "image_url", image_url: { url: `data:image/jpeg;base64,${base64Image}` } },
        ],
      },
    ],
    response_format: zodResponseFormat(NutritionAnalysisSchema, "nutrition_analysis"),
  });

  return response.choices[0].message.parsed;
}

Structured outputs eliminate parsing failures and make AI agents safe for mission-critical integration.

Bagikan

Artikel lainnya