Generate JSON output with the Gemini API


Gemini generates unstructured text by default, but some applications require structured text. For these use cases, you can constrain Gemini to respond with JSON, a structured data format suitable for automated processing.

For example, these use cases require structured output from the model:

  • Build a database of companies by pulling company information out of newspaper articles.
  • Pull standardized information out of resumes.
  • Extract ingredients from recipes and display a link to a grocery website for each ingredient.

In your prompt, you can ask Gemini to produce JSON-formatted output, but note that Google can't guarantee that it will produce JSON and nothing but JSON. However, if you use Gemini 1.5 Pro, you can pass a specific JSON schema in a response_schema field so that Gemini always responds with an expected structure.

This guide shows you how to generate JSON using the generateContent method through the SDK of your choice. It focuses on text-only input, although Gemini can also produce JSON responses to multimodal queries. To learn more about multimodal prompting with video and audio files, see Prompting with media files.