Skip to content
OPQAI.
Sourced intermediate / 💻 Coding

Stream and Validate AI JSON Output with trickle-structured

Job to be done: Stream and validate structured JSON output from an AI model in real-time

🇳🇬 Ways to use this in Nigeria

Ideas to get you started, adapt to your situation.

  • Entrepreneur

    As an entrepreneur building an AI-powered product, use this to instantly get validated JSON data, like product features or user preferences, from an AI model for your app's real-time display.

  • Student

    As a computer science student, use this to build a project that extracts specific entities, like names and dates, from research papers into a validated JSON object for analysis or a web app.

  • 9-5 employee

    As a developer, use this to build an internal tool that extracts specific fields, like client names or project IDs, from unstructured emails into a validated JSON object for a project management system.

What you’ll get

You will learn how to get structured data, like a JSON object, from an AI model and have it automatically checked for correctness as it arrives. This is useful when you need the AI to give you data in a specific format, like a list of products or user details, and you want to start using that data immediately without waiting for the entire response or worrying about errors.

Tools you need

  • OpenAI API (paid): This is the service that provides access to powerful AI models like GPT-4o. You will need an API key to use it.
  • trickle-structured (free): A small, zero-dependency package that simplifies getting and validating streaming data from AI models.
  • zod (free): A TypeScript-first schema declaration and validation library. It helps define the expected structure of your data.
  • gpt-4o (paid): A specific, advanced AI model from OpenAI that can generate text and structured data.

Steps

  1. Set up your project: You will need a JavaScript environment (like Node.js) and to install the necessary packages. Open your terminal or command prompt and run:

    # macOS or Linux
    npm install trickle-structured zod openai
    # Windows (PowerShell)
    npm install trickle-structured zod openai

    You should see messages indicating that the packages have been successfully installed.

  2. Define your data structure: Use zod to describe the shape of the JSON data you expect from the AI. For example, to expect a user object with an ID, name, and active status:

    import { z } from "zod";
    
    const User = z.object({
      id: z.number(),
      name: z.string(),
      active: z.boolean()
    });

    You should see no errors after defining your schema.

  3. Get your OpenAI API key: Go to the OpenAI website, log in, and find your API keys. Copy your secret key.

  4. Create the streaming request: Use the trickle-structured package to create a stream that sends your request to the OpenAI API and expects data matching your zod schema. Replace YOUR_OPENAI_API_KEY with your actual key and adjust the messages content as needed.

    import { structured } from "trickle-structured";
    import { z } from "zod";
    
    const User = z.object({
      id: z.number(),
      name: z.string(),
      active: z.boolean()
    });
    
    const key = "YOUR_OPENAI_API_KEY"; // Replace with your actual API key
    
    const messages = [
      { role: "system", content: "You are a helpful assistant designed to output JSON." },
      { role: "user", content: "Create a user profile with id, name, and active status." }
    ];
    
    const stream = structured({
      url: "https://api.openai.com/v1/chat/completions",
      method: "POST",
      headers: {
        authorization: `Bearer ${key}`,
        "content-type": "application/json"
      },
      body: JSON.stringify({
        model: "gpt-4o",
        stream: true,
        response_format: { type: "json_object" },
        messages,
      }),
    }, User);

    This code sets up the connection and tells the AI what kind of data to send back. You should see the stream variable created without errors.

  5. Process the streaming data: You can now loop through the stream to get partial updates of the JSON object as it’s being generated. This allows you to display or use the data as it arrives. The stream.value will hold the final, validated object.

    async function processStream() {
      console.log("Streaming data...");
      try {
        for await (const partial of stream) {
          console.log("Partial update:", partial);
          // You can update your UI here with the partial data
        }
        const user = await stream.value;
        console.log("Final validated user:", user);
      } catch (error) {
        console.error("An error occurred:", error);
      }
    }
    
    processStream();

    When you run this code, you should see “Partial update:” logs as the AI generates the JSON, followed by the “Final validated user:” log once the complete and validated object is ready. If there’s an error, you’ll see the error message.

Original source

This workflow is based on a blog post by h1manshu01 on DEV Community. The author explains how to simplify the process of streaming structured output from AI models, addressing common challenges like partial parsing and data validation.

Notes & variations

  • Free-tier alternative: While the OpenAI API itself is paid, you can experiment with other AI providers that might offer free tiers for their APIs, such as Groq or Google’s Gemini API, if they support streaming JSON output and are compatible with trickle-structured.
  • Common mistake: Forgetting to set stream: true in the body of your request to the OpenAI API will prevent you from getting data in chunks, defeating the purpose of streaming.
  • Tip for better results: Ensure your zod schema is as precise as possible. The more accurate your schema, the better trickle-structured can validate and even repair minor inconsistencies in the AI’s output.

Keep going

More Coding workflows