AI Model Wiki

    Skip to main content
    10 min read
    Lesson 3 of 6

    Structured Outputs and Function Calling

    From the course Building with AI APIs

    Getting AI to return structured, predictable data — not just free-form text — is essential for building reliable applications.

    Why Structured Outputs?

    Free-form text is great for chatbots, but most applications need structured data:

    • Extracting entities from text (names, dates, prices)
    • Generating form-fillable data
    • Creating database records from natural language
    • Building tool-using AI agents

    JSON Mode

    The simplest approach — ask the model to respond in JSON:

    const response = await openai.chat.completions.create({
      model: "gpt-5",
      response_format: { type: "json_object" },
      messages: [
        { role: "system", content: "Extract product info as JSON with keys: name, price, category" },
        { role: "user", content: "The new MacBook Pro 16-inch starts at $2499 in the laptop category." }
      ]
    });
    // Returns: {"name": "MacBook Pro 16-inch", "price": 2499, "category": "laptop"}
    

    Structured Outputs with Schemas

    For guaranteed structure, provide a JSON Schema:

    const response = await openai.chat.completions.create({
      model: "gpt-5",
      response_format: {
        type: "json_schema",
        json_schema: {
          name: "product",
          schema: {
            type: "object",
            properties: {
              name: { type: "string" },
              price: { type: "number" },
              in_stock: { type: "boolean" }
            },
            required: ["name", "price", "in_stock"]
          }
        }
      },
      messages: [...]
    });
    

    Function Calling

    Function calling lets the AI decide when to call functions you define:

    const tools = [{
      type: "function",
      function: {
        name: "get_weather",
        description: "Get current weather for a location",
        parameters: {
          type: "object",
          properties: {
            location: { type: "string", description: "City name" },
            units: { type: "string", enum: ["celsius", "fahrenheit"] }
          },
          required: ["location"]
        }
      }
    }];
    

    When the user asks "What's the weather in Tokyo?", the model returns a function call instead of text. Your code executes the function and feeds the result back.

    Building AI Agents

    Function calling enables AI agents — systems that can take actions:

    1. User makes a request
    2. AI decides which function(s) to call
    3. Your code executes the functions
    4. Results are fed back to the AI
    5. AI synthesizes and responds (or calls more functions)

    This pattern powers AI assistants that can search databases, call APIs, manage calendars, and more.

    Key Takeaways

    • •JSON mode ensures responses are valid JSON
    • •JSON Schema mode guarantees specific structure with required fields
    • •Function calling lets AI decide when to invoke your code
    • •AI agents use function calling in loops to take complex actions
    • •Always validate AI outputs even with structured output modes
    0 of 6 lessons marked complete

    Progress is kept in this browser only.