Provide tools to the model when accessing the Gemini API through Apple's Foundation Models framework


The examples on this page assume that you've completed the Get started: Access the Gemini API through Apple's Foundation Models framework .


You can provide Gemini built-in tools to Gemini models when accessing the Gemini API through Apple's Foundation Models framework to connect the model to external data sources.

The page shows you how to use the following built-in tools for Gemini models:

Grounding with Google Search connects a Gemini model to real-time, publicly-available web content. This allows the model to provide more accurate, up-to-date answers and cite verifiable sources.

For details, best practices, and use cases, see the general Grounding with Google Search guide.

Supported models

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite
  • gemini-3-pro-image-preview (aka "Nano Banana Pro")
  • gemini-3.1-flash-image-preview (aka "Nano Banana 2")

Provide the googleSearch tool as part of creating the geminiLanguageModel :

  import 
  
 FoundationModels 
 import 
  
 FirebaseCore 
 import 
  
 FirebaseAILogic 
 // Initialize the Gemini Developer API backend service. 
 let 
  
 ai 
  
 = 
  
 FirebaseAI 
 . 
 firebaseAI 
 ( 
 backend 
 : 
  
 . 
 googleAI 
 ()) 
 // Initialize a `geminiLanguageModel` with a Gemini model that supports your use case. 
 let 
  
 model 
  
 = 
  
 ai 
 . 
 geminiLanguageModel 
 ( 
  
 name 
 : 
  
 " GEMINI_MODEL_NAME 
" 
 , 
  
 // Provide Google Search as a tool that the model can use to generate its response. 
  
 serverTools 
 : 
  
 [ 
 GeminiTool 
 . 
 googleSearch 
 ()] 
 ) 
 let 
  
 session 
  
 = 
  
 LanguageModelSession 
 ( 
 model 
 : 
  
 model 
 ) 
 let 
  
 response 
  
 = 
  
 try 
  
 await 
  
 session 
 . 
 respond 
 ( 
 to 
 : 
  
 "What is the weather in Toronto today?" 
 ) 
 for 
  
 entry 
  
 in 
  
 response 
 . 
 transcriptEntries 
  
 { 
  
 if 
  
 case 
  
 let 
  
 . 
 response 
 ( 
 responseEntry 
 ) 
  
 = 
  
 entry 
  
 { 
  
 if 
  
 let 
  
 groundingMetadata 
  
 = 
  
 responseEntry 
  
 . 
 metadata 
 [ 
 "groundingMetadata" 
 ] 
  
 as 
 ? 
  
 GroundingMetadata 
  
 { 
  
 for 
  
 chunk 
  
 in 
  
 groundingMetadata 
 . 
 groundingChunks 
  
 { 
  
 let 
  
 webChunk 
  
 = 
  
 chunk 
 . 
 web 
  
 // use the webChunk 
  
 } 
  
 } 
  
 } 
 } 
 // Make sure to comply with the "Grounding with Google Search" usage requirements, 
 // which includes how you use and display the grounded result 
 
 

Grounding with Google Maps

Grounding with Google Maps connects a Gemini model to geospatial data from Google Maps so that you can build location-aware functionality into your apps.

For details, best practices, and use cases, see the general Grounding with Google Maps guide.

Supported models

  • gemini-3.1-pro-preview
  • gemini-3.5-flash
  • gemini-3.1-flash-lite

Enable the Google Maps tool

Provide the googleMaps tool as part of creating the geminiLanguageModel . You can also optionally provide coordinates in the tool's configuration.

  import 
  
 FoundationModels 
 import 
  
 FirebaseCore 
 import 
  
 FirebaseAILogic 
 // Initialize the Gemini Developer API backend service. 
 let 
  
 ai 
  
 = 
  
 FirebaseAI 
 . 
 firebaseAI 
 ( 
 backend 
 : 
  
 . 
 googleAI 
 ()) 
 // Initialize a `geminiLanguageModel` with a Gemini model that supports your use case. 
 let 
  
 model 
  
 = 
  
 ai 
 . 
 geminiLanguageModel 
 ( 
  
 name 
 : 
  
 " GEMINI_MODEL_NAME 
" 
 , 
  
 // Provide Google Maps as a tool that the model can use to generate its response. 
  
 serverTools 
 : 
  
 [ 
 GeminiTool 
 . 
 googleMaps 
 ()] 
 ) 
 let 
  
 session 
  
 = 
  
 LanguageModelSession 
 ( 
 model 
 : 
  
 model 
 ) 
 let 
  
 response 
  
 = 
  
 try 
  
 await 
  
 session 
  
 respond 
 ( 
 to 
 : 
  
 "Where is a good place to grab a coffee near Alameda, CA?" 
 ) 
 for 
  
 entry 
  
 in 
  
 response 
 . 
 transcriptEntries 
  
 { 
  
 if 
  
 case 
  
 let 
  
 . 
 response 
 ( 
 responseEntry 
 ) 
  
 = 
  
 entry 
  
 { 
  
 if 
  
 let 
  
 groundingMetadata 
  
 = 
  
 responseEntry 
  
 . 
 metadata 
 [ 
 "groundingMetadata" 
 ] 
  
 as 
 ? 
  
 GroundingMetadata 
  
 { 
  
 for 
  
 chunk 
  
 in 
  
 groundingMetadata 
 . 
 groundingChunks 
  
 { 
  
 let 
  
 mapsChunk 
  
 = 
  
 chunk 
 . 
 maps 
  
 // use the mapsChunk 
  
 } 
  
 } 
  
 } 
 } 
 // Make sure to comply with the "Grounding with  Google Maps 
 
" usage requirements, 
 // which includes how you meet service usage requirements 
 
 


Give feedbackabout accessing the Gemini API through Apple's Foundation Models framework


Create a Mobile Website
View Site in Mobile | Classic
Share by: