Do stream responses support structured output?

Hey everyone! I’ve seen that Gemini supports structured output, but I cannot find info about if this works with stream responses. So for example in this code, is there any way I can specify the structure of response?

const handleGemini = async (prompt: string, res: Response) => {
  const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
  const model = genAI.getGenerativeModel({ model: "gemini-2.5-pro-exp-03-25" });

  const result = await model.generateContentStream([{ text: prompt }]);

  for await (const chunk of result.stream) {
    const text = chunk.text();
    if (text) {
      res.write(text);
    }
  }

  return res.end();
};

Hi @Denys , Welcome to the forum.

Yes, gemini supports structured output with stream responses. Please check out the working code as below with new JS SDK.

import { GoogleGenAI, Type } from "@google/genai";
import fs from "fs";
import dotenv from 'dotenv';
dotenv.config();
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

async function main() {
  const response = await ai.models.generateContentStream({
      model: 'gemini-2.0-flash',
      contents: 'List 10 popular cookie recipes.',
      config: {
          responseMimeType: 'application/json',
          responseSchema: {
              type: Type.ARRAY,
              items: {
                  type: Type.OBJECT,
                  properties: {
                      'recipeName': {
                          type: Type.STRING,
                          description: 'Name of the recipe',
                          nullable: false,
                      },
                  },
                  required: ['recipeName'],
              },
          },
      },
  });

  for await (const chunk of response) {
    console.log(chunk.text);
  }
}



await main();