Function Calling & Structured Output
From the course Google Gemini API Development
Function Calling with Gemini
Gemini supports function calling to connect AI responses with external tools and APIs.
Defining Functions
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "What's the weather in Tokyo?",
config: {
tools: [{
functionDeclarations: [{
name: "getWeather",
description: "Get current weather for a location",
parameters: {
type: "object",
properties: {
location: { type: "string", description: "City name" },
unit: { type: "string", enum: ["celsius", "fahrenheit"] }
},
required: ["location"]
}
}]
}],
},
});
Structured Output
Force Gemini to output valid JSON with a schema:
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "List 5 programming languages with their use cases",
config: {
responseMimeType: "application/json",
responseSchema: {
type: "array",
items: {
type: "object",
properties: {
name: { type: "string" },
useCase: { type: "string" }
},
required: ["name", "useCase"]
}
},
},
});
Advanced Patterns
- Parallel function calls — Gemini can request multiple functions at once
- Function call modes — AUTO, ANY, or NONE to control tool behavior
- Nested schemas — complex object hierarchies for structured output
- Enum constraints — restrict outputs to predefined values
Common Pitfall: Relying Solely on responseMimeType Without a Schema
Setting responseMimeType: "application/json" asks Gemini to produce valid JSON, but without a responseSchema the model decides the JSON structure itself — which varies between calls. Always provide a schema for predictable output:
// Unpredictable: model chooses structure
config: { responseMimeType: "application/json" }
// Predictable: model follows your schema exactly
config: {
responseMimeType: "application/json",
responseSchema: {
type: "array",
items: {
type: "object",
properties: {
name: { type: "string" },
useCase: { type: "string" },
},
required: ["name", "useCase"],
},
},
}
Best Practice: Handle the NONE Stop Reason
When Gemini cannot execute any of the provided functions (e.g., because none match the user's request), it may return with a finishReason of "STOP" and no tool calls. Always check whether the model produced tool calls before trying to process them:
const candidate = response.candidates?.[0];
const hasToolCalls = candidate?.content?.parts?.some(p => p.functionCall);
if (!hasToolCalls) {
// Model answered directly or could not find an appropriate tool
return candidate?.content?.parts?.[0]?.text ?? "No response";
}
Named Product Reference: Genkit by Firebase
Firebase Genkit is Google's open-source framework for building AI-powered apps with Gemini. It provides a plugin system for LLM providers, vector stores, and tracing — similar in concept to LangChain but with tighter Gemini and Firebase integration. Genkit's built-in flow tracing UI shows the inputs and outputs of each step, making it easier to debug complex function-calling pipelines.
Key Takeaways
- •Define functions with JSON Schema for Gemini to call external APIs
- •Use responseMimeType and responseSchema for guaranteed JSON output
- •Parallel function calls improve efficiency for independent operations
- •Function call modes control when and how Gemini uses tools
Progress is kept in this browser only.