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 the model is not guaranteed to produce JSON and nothing but JSON.
For a more deterministic response, you can pass a specific JSON schema in a
responseSchema
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 requests that include
images, videos, and
audio.