Skip to content

FR: Structured output #9295

@svdgoor

Description

@svdgoor

Operating System

NestJS backend run on Windows

Environment (if applicable)

Node v22.14.0

Firebase SDK Version

=> Found "[email protected]"

Firebase SDK Product(s)

AI

Project Tooling

Not important, but our backend is NestJS, which is where we run live API

Detailed Problem Description

What you were trying to achieve.

Our system emits various events, some of which we would like to relay to our end users. To be able to triage this, we need the live API to emit structured output so we can distinguish between when it decides to and decides not to take action on a certain event it receives.

What actually happened.

The bidirectional API does not support Generating Structured Output, where the regular AI suite does.

Any error messages or unexpected behavior you observed.

N/a

Relevant log snippets or console output (if available).

N/a / already well defined by users of the GA structured output

Steps and code to reproduce issue

import { initializeApp } from "firebase/app";
import { 
  getAI, 
  getGenerativeModel, 
  getLiveGenerativeModel, 
  GoogleAIBackend, 
  Schema, 
  ResponseModality 
} from "firebase/ai";

// Replace with your Firebase config
const firebaseConfig = { /* ... */ };

// Initialize Firebase app + AI backend
const firebaseApp = initializeApp(firebaseConfig);
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

/**
 * --- Structured output (works in non-live APIs) ---
 */
const enumSchema = Schema.enumString({
  enum: ["drama", "comedy", "documentary"],
});

const structuredModel = getGenerativeModel(ai, {
  model: "gemini-2.5-flash",
  generationConfig: {
    responseMimeType: "text/x.enum",
    responseSchema: enumSchema,
  },
});

async function runStructuredExample() {
  let prompt = `The film aims to educate and inform viewers about real-life
  subjects, events, or people. It offers a factual record of a particular topic
  by combining interviews, historical footage, and narration.`;

  let result = await structuredModel.generateContent(prompt);
  console.log("Structured output:", result.response.text()); // → "documentary"
}

/**
 * --- Live API (works for streaming/audio, but no structured schemas yet) ---
 */
const liveModel = getLiveGenerativeModel(ai, {
  model: "gemini-2.0-flash-live-preview-04-09",
  generationConfig: {
    responseModalities: [ResponseModality.AUDIO],
    // ⚠ No responseSchema support here (yet)
  },
});

async function runLiveExample() {
  const session = await liveModel.connect();

  // Example: start an audio conversation
  const audioConversationController = await startAudioConversation(session);

  // Later you can stop it:
  // await audioConversationController.stop();
}

// Run them
await runStructuredExample();
await runLiveExample();

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions