The Gemini API allows you to supply a schema to define function arguments (for function calling), or to constrain its output in JSON or using an Enum. This tutorial gives some examples using enums.
You can create your API key using Google AI Studio with a single click.
Remember to treat your API key like a password. Don’t accidentally save it in a notebook or source file you later commit to GitHub. In this notebook we will be storing the API key in a .env file. You can also set it as an environment variable or use a secret manager.
Another option is to set the API key as an environment variable. You can do this in your terminal with the following command:
$ export GEMINI_API_KEY="<YOUR_API_KEY>"
Load the API key
To load the API key from the .env file, we will use the dotenv package. This package loads environment variables from a .env file into process.env.
$ npm install dotenv
Then, we can load the API key in our code:
const dotenv =require("dotenv") astypeofimport("dotenv");dotenv.config({ path:"../.env",});const GEMINI_API_KEY =process.env.GEMINI_API_KEY??"";if (!GEMINI_API_KEY) {thrownewError("GEMINI_API_KEY is not set in the environment variables");}console.log("GEMINI_API_KEY is set in the environment variables");
GEMINI_API_KEY is set in the environment variables
Note
In our particular case the .env is is one directory up from the notebook, hence we need to use ../ to go up one directory. If the .env file is in the same directory as the notebook, you can omit it altogether.
With the new SDK, now you only need to initialize a client with you API key (or OAuth if using Vertex AI). The model is now set in each call.
const google =require("@google/genai") astypeofimport("@google/genai");const ai =new google.GoogleGenAI({ apiKey: GEMINI_API_KEY });
Select a model
Now select the model you want to use in this guide, either by selecting one in the list or writing it down. Keep in mind that some models, like the 2.5 ones are thinking models and thus take slightly more time to respond (cf. thinking notebook for more details and in particular learn how to switch the thiking off).
For more information about all Gemini models, check the documentation for extended information on each of them.
In the simplest case is you need the model to choose one option from a list of choices, use an enum class to define the schema. Ask it to identify this instrument:
Pass the enum class as the responseSchema, and for this simplest case you can use the responseMimeType = "text/x.enum" option to get one of those enum members as the response.
const instrument_file =await ai.files.upload({ file: filePath, config: { displayName:"instrument.jpg", mimeType:"image/jpeg", },});const instrument_response =await ai.models.generateContent({ model: MODEL_ID, contents: [ google.createPartFromUri(instrument_file.uri??"", instrument_file.mimeType??""),"what is the category of this instrument?", ], config: { responseMimeType:"text/x.enum", responseSchema: InstrumentSchema, },});console.log("Instrument category:", instrument_response.text);
Instrument category: Keyboard
You can also use enums with responseMimeType = "application/json". In this simple case the response will be identical but in quotes.
const instrument_json_response =await ai.models.generateContent({ model: MODEL_ID, contents: [ google.createPartFromUri(instrument_file.uri??"", instrument_file.mimeType??""),"what is the category of this instrument?", ], config: { responseMimeType:"application/json", responseSchema: InstrumentSchema, },});console.log("Instrument category (JSON):", instrument_json_response.text);
Instrument category (JSON): "Keyboard"
Outside of simple multiple choice problems, an enum can be used anywhere in the schema for JSON or function calling. For example, ask it for a list of recipe titles, and use a Grade enum to give each one a popularity-grade:
import { Schema, Type } from"@google/genai";enum Grade { A_PLUS ="A+", A ="A", B ="B", C ="C", D ="D", F ="F",}const RecipeSchema: Schema = { type: Type.OBJECT, description:"A recipe for a dish", properties: { recipeName: { type: Type.STRING, description:"The name of the recipe", }, grade: { type: Type.STRING, description:"The grade of the recipe", enum:Object.values(Grade), }, }, required: ["recipeName","grade"],};const RecipeListSchema: Schema = { type: Type.ARRAY, description:"A list of recipes with their grades", items: RecipeSchema,};const recipe_response =await ai.models.generateContent({ model: MODEL_ID, contents: ["List about 10 cookie recipes, grade them based on popularity"], config: { responseMimeType:"application/json", responseSchema: RecipeListSchema, },});console.log("Recipe response:");console.log(JSON.stringify(JSON.parse(recipe_response.text??""),null,2));
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 uniiformize the output of the detection.
Continue your discovery of the Gemini API
An Enum is not the only way to constrain the output of the model, you can also use an JSON schema. Function calling and Code execution are other ways to enhance your model by either let him use your own functions or by letting it write and run them.