JSON Mode REST

quickstartsrestgemini-cookbookgemini-apigemini
Copyright 2025 Google LLC.
[ ]

Gemini API: JSON Mode Quickstart with REST

The Gemini API can be used to generate a JSON output if you set the schema that you would like to use. This notebook provides a code example that shows you how to get started with JSON mode using curl.

You can run this in Google Colab, or you can copy/paste the curl commands into your terminal.

Authentication

To run this notebook, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you are running in a different environment, you can store your key in an environment variable. See Authentication to learn more.

[ ]
[ ]

Activate JSON Mode

To activate JSON mode, set response_mime_type to application/json in the generationConfig.

[ ]
MODEL_ID
[ ]
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "[{\"recipe_name\":\"Chocolate Chip Cookies\"},{\"recipe_name\":\"Peanut Butter Cookies\"},{\"recipe_name\":\"Oatmeal Raisin Cookies\"},{\"recipe_name\":\"Sugar Cookies\"},{\"recipe_name\":\"Shortbread Cookies\"}] \n"
          }
        ],
        "role": "model"

To turn off JSON mode, set response_mime_type to text/plain (or omit the response_mime_type parameter).

Next Steps

Useful API references:

Check the structured ouput documentation or the GenerationConfig API reference for more details

Related examples

  • The constrained output is used in the Text summarization example to provide the model a format to summarize a story (genre, characters, etc...)
  • The Object detection examples are using the JSON constrained output to normalize the output of the detection.

Continue your discovery of the Gemini API

JSON is not the only way to constrain the output of the model, you can also use an Enum. Function calling and Code execution are other ways to enhance your model by using your own functions or by letting the model write and run them.