Force Google Gemini to return JSON using javascript

Felipe Lujan
3 min readSep 30, 2024

An example using node.JS and @google-cloud/vertexai — Since Google’s documentation only shows Python examples.

If you’re developing an enterprise application, you want to be using Gemini through the @google-cloud/vertexai npm package

Import

const {
FunctionDeclarationSchemaType,
VertexAI,
} = require("@google-cloud/vertexai");

Instantiate VertexAI

  const vertexAI = new VertexAI({
project: "PROJECT_ID",
location: "us-central1", // whichever region you are using
});

Prompt and JSON Schema — this is where the magic happens.

  let responseSchema = {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
summary: {
type: FunctionDeclarationSchemaType.STRING,
description:
"Body of the summary, do not include emojis or expressions like OMG, LOL, etc.",
nullable: false,
},
title: {
type: FunctionDeclarationSchemaType.STRING,
description: "short title for the summary",
nullable: false,
},
summarizationError: {
type: FunctionDeclarationSchemaType.BOOLEAN,
nullable: false,
description:
"whether the summary was generated successfully, true when the article only focuses on information about a captcha challenge",
},
category: {
type: FunctionDeclarationSchemaType.STRING,
enum: [
"politics",
"sports",
"entertainment",
"technology",
"economy",
"health",
"other",
],
description: "topic that best represents the article",
nullable: false,
},
language: {
type: FunctionDeclarationSchemaType.STRING,
enum: ["en", "es", "fr", "it", "de"],
description: "",
nullable: false,
},
},
};

Here’s what you can specify about each JSON key

export interface Schema {
/**
* Optional. The type of the property. {@link
* SchemaType}.
*/

type?: SchemaType;
/** Optional. The format of the property. */
format?: string;
/** Optional. The description of the property. */
description?: string;
/** Optional. Whether the property is nullable. */
nullable?: boolean;
/** Optional. The items of the property. {@link Schema} */
items?: Schema;
/** Optional. The enum of the property. */
enum?: string[];
/** Optional. Map of {@link Schema}. */
properties?: {
[k: string]: Schema;
};
/** Optional. Array of required property. */
required?: string[];
/** Optional. The example of the property. */
example?: unknown;
}

As you can tell from my example above, I only used type , description nullable enum (where applicable).

Sending the request to Gemini on VertexAI. Note the responseMimeType key under generationConfig

  const generativeModel = vertexAI.getGenerativeModel({
model: "gemini-1.5-flash-002", // Fresh out of the oven 🍞😋
generationConfig: {
maxOutputTokens: 2048,
responseSchema,
responseMimeType: "application/json", // this line forces JSON response
},
systemInstruction: {
role: "system",
parts: [{ text: prompt }],
},
});

const request = {
contents: [{ role: "user", parts: [{ text: "veeeery long text to summarize"}] }],
};
const result = await generativeModel.generateContent(request);

print out the response

    console.log(JSON.stringify(result, null, 2));

gives me

{
category: 'technology',
language: 'en',
summarizationError: false,
summary: "🚨Home Assistant, the open-source smart home platform, is going mainstream! Paulus Schoutsen, the founder, created the Open Home Foundation, a non-profit to ensure privacy and local control. They're launching new products like a Home Assistant hub sold on Amazon and a voice-control device to rival Google and Amazon. Plus, they're working with Nvidia on local AI for a super-smart home experience! This isn't just about convenience; it's about taking back control of your data and creating a sustainable smart home future. This move will shake up the smart home industry.",
title: 'Home Assistant Goes Mainstream'
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Felipe Lujan
Felipe Lujan

Written by Felipe Lujan

Google Developer Expert — Google Cloud.

No responses yet

Write a response