DEV Community

Shrijith Venkatramana
Shrijith Venkatramana

Posted on • Edited on

How To Generate Structured Output (JSON, YAML) in Gemini AI

Hello, I'm Shrijith. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

## Step 1: Get a Gemini API Key

Use Google's AI Studio to get a free API Key first.

Wewill be using the API key to make Gemini requests.

Step 2: Install SDK

Create a project and install dependency:

mkdir structured-gemini
cd structured-gemini
npm install @google/generative-ai
Enter fullscreen mode Exit fullscreen mode

Import the genai library and configure with API key in sg.mjs:

import { GoogleGenerativeAI } from "@google/generative-ai";

// Access your API key as an environment variable
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
Enter fullscreen mode Exit fullscreen mode

Step 3: Generate a valid JSON using SchemaType

In this section, we will try to get gemini to give us the top 10 mystery movies of all time, with some metadata about each movie as an array of JSON objects.

Step 3.1 - Define Schema

import { GoogleGenerativeAI, SchemaType } from "@google/generative-ai";

const schema = {
  description: "Top 10 mystery movies of all time",
  type: SchemaType.ARRAY,
  items: {
    type: SchemaType.OBJECT,
    properties: {
      title: { type: SchemaType.STRING, description: "Title of the movie", nullable: false },
      director: { type: SchemaType.STRING, description: "Director of the movie", nullable: false },
      year: { type: SchemaType.INTEGER, description: "Year of release", nullable: false },
      imdbRating: { type: SchemaType.NUMBER, description: "IMDb rating of the movie", nullable: false },
    },
    required: ["title", "director", "year", "imdbRating"],
  },
};
Enter fullscreen mode Exit fullscreen mode

You can see that we are requesting an array of objects with highly specific requirements.

3.2 - Define Model

In the next step, we define a model, where we explicitly request a json response:

const model = genAI.getGenerativeModel({
  model: "gemini-1.5-pro",
  generationConfig: {
    responseMimeType: "application/json",
    responseSchema: schema,
  },
});
Enter fullscreen mode Exit fullscreen mode

3.3 Generate JSON and Pretty Print the Result

async function fetchMovies() {
  const result = await model.generateContent(
    "List the top 10 mystery movies of all time with their title, director, year of release, and IMDb rating."
  );
  let movies = JSON.parse(result.response.text())
  console.log(JSON.stringify(movies, null, 2));
}

fetchMovies().catch(console.error);
Enter fullscreen mode Exit fullscreen mode

When I run the above with node sg.mjs, I get the following output (pretty cool!):

[
  {
    "director": "Alfred Hitchcock",
    "imdbRating": 8.3,
    "title": "Vertigo",
    "year": 1958
  },
  {
    "director": "Orson Welles",
    "imdbRating": 8.3,
    "title": "Citizen Kane",
    "year": 1941
  },
  {
    "director": "Billy Wilder",
    "imdbRating": 8.3,
    "title": "Double Indemnity",
    "year": 1944
  },
  {
    "director": "Akira Kurosawa",
    "imdbRating": 8.2,
    "title": "Rashomon",
    "year": 1950
  },
  {
    "director": "John Huston",
    "imdbRating": 8.1,
    "title": "The Maltese Falcon",
    "year": 1941
  },
  {
    "director": "Carol Reed",
    "imdbRating": 8.1,
    "title": "The Third Man",
    "year": 1949
  },
  {
    "director": "Stanley Kubrick",
    "imdbRating": 8.3,
    "title": "2001: A Space Odyssey",
    "year": 1968
  },
  {
    "director": "David Fincher",
    "imdbRating": 8.2,
    "title": "Seven",
    "year": 1995
  },
  {
    "director": "Christopher Nolan",
    "imdbRating": 8.4,
    "title": "The Prestige",
    "year": 2006
  },
  {
    "director": "Rian Johnson",
    "imdbRating": 7.9,
    "title": "Knives Out",
    "year": 2019
  }
]
Enter fullscreen mode Exit fullscreen mode

Step 4: Getting a YAML version of the same output

Guess how difficult it'd be to get a YAML version of the same output?

I tried changing application/json to text/yaml in the mime field.

Turns out that doesn't work:

YAML Generation Failure

So, a workaround is to convert JSON to YAML, since JSON can easily be represented in YAML.

Get JSON to YAML converter

npm i @catalystic/json-to-yaml
Enter fullscreen mode Exit fullscreen mode

Import and use the converter

import { convert } from "@catalystic/json-to-yaml";

async function fetchMovies() {
  const result = await model.generateContent(
    "List the top 10 mystery movies of all time with their title, director, year of release, and IMDb rating."
  );
  let movies = JSON.parse(result.response.text())
  console.log(JSON.stringify(movies, null, 2));

  const yaml = convert(movies);
  console.log("\n==============\n")
  console.log(yaml)
}
Enter fullscreen mode Exit fullscreen mode

When I run this, I get the YAML output as expected:


yaml

  - director: "Alfred Hitchcock"
    imdbRating: 8.3
    title: "Vertigo"
    year: 1958
  - director: "Orson Welles"
    imdbRating: 8.3
    title: "Citizen Kane"
    year: 1941
  - director: "Billy Wilder"
    imdbRating: 8.3
    title: "Double Indemnity"
    year: 1944
  - director: "Akira Kurosawa"
  imdbRating: 8.2
    title: "Rashomon"
    year: 1950
  - director: "Carol Reed"
    imdbRating: 8.1
    title: "The Third Man"
    year: 1949
  - director: "John Huston"
    imdbRating: 8.1
    title: "The Maltese Falcon"
    year: 1941
  - director: "Stanley Kubrick"
    imdbRating: 8
    title: "2001: A Space Odyssey"
    year: 1968
  - director: "Roman Polanski"
    imdbRating: 8
    title: "Chinatown"
    year: 1974
  - director: "David Fincher"
    imdbRating: 8
    title: "Seven"
    year: 1995
  - director: "Christopher Nolan"
    imdbRating: 8
    title: "Memento"
    year: 2000

 [![git-lrc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzvpkxm9mga1pweneahx.png)](https://github.com/HexmosTech/git-lrc) 
 *AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production. 

 git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.* 


 Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use. 

 ⭐ Star it on GitHub: 
 

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit

git-lrc logo

git-lrc

Free, Unlimited AI Code Reviews That Run on Commit


git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • 🤖 AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • 🔍 Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • 🔁 Build a habit, ship better code. Regular review → fewer bugs → more robust code → better results in your team.
  • 🔗 Why git? Git is universal. Every editor, every IDE, every AI…



Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
abubakersiddique761 profile image
Abubaker Siddique

It's useful man.