Skip to content

[AI] Automatic function calling#15915

Draft
andrewheard wants to merge 47 commits intomainfrom
ah/ai-gen-model-session-func
Draft

[AI] Automatic function calling#15915
andrewheard wants to merge 47 commits intomainfrom
ah/ai-gen-model-session-func

Conversation

@andrewheard
Copy link
Contributor

@andrewheard andrewheard commented Mar 7, 2026

Added support for automatic function calling for functions declared by conforming to FoundationModels.Tool (iOS 26+) or a similar FunctionTool protocol for backwards compatibility. The following usage example is a direct equivalent to the existing manual function calling example from the Firebase AI Logic documentation.

Usage Example

Step 1: Write the function

struct FetchWeather: FoundationModels.Tool {
  let description = "Get the weather conditions for a specific city on a specific date."

  @Generable
  struct WeatherQuery {
    @Generable
    struct Location {
      @Guide(description: "The city of the location.")
      let city: String

      @Guide(description: "The US state of the location.")
      let state: String
    }

    @Guide(description: """
    The name of the city and its state for which to get the weather. Only cities in the
    USA are supported.
    """)
    let location: Location

    @Guide(description: """
    The date for which to get the weather. Date must be in the format: YYYY-MM-DD.
    """)
    let date: String
  }

  @Generable
  struct WeatherConditions {
    let temperature: Double
    let chancePrecipitation: String
    let cloudConditions: String
  }

  // TODO(developer): Write a standard function that would call an external weather API.
  func call(arguments: WeatherQuery) async throws -> WeatherConditions {
    // For demo purposes, this hypothetical response is hardcoded here in the expected format.
    return WeatherConditions(
      temperature: 38,
      chancePrecipitation: "56%",
      cloudConditions: "partlyCloudy"
    )
  }
}

Step 2: Provide the function declaration during model initialization

// Create a `GenerativeModelSession` instance with a model that supports your use case
let session = FirebaseAI.firebaseAI().generativeModelSession(
  model: "gemini-3-flash-preview",
  // Provide the function declaration to the model.
  tools: [FetchWeather()]
)

Step 3: Generate the final response

let prompt = "What was the weather in Boston on October 17, 2024?"
let response = try await session.respond(to: prompt)

// Log the text response.
print(response.content)

On October 17, 2024, the weather in Boston, MA, was partly cloudy with a temperature of 38°F and a 56% chance of precipitation.

#no-changelog

andrewheard and others added 30 commits February 20, 2026 17:34
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…firebase-ios-sdk into ah/ai-generative-model-session

# Conflicts:
#	FirebaseAI/Tests/TestApp/Tests/Integration/GenerativeModelSessionTests.swift
…ssion-func

# Conflicts:
#	FirebaseAI/Sources/FirebaseAI.swift
#	FirebaseAI/Sources/GenerativeModelSession.swift
@gemini-code-assist
Copy link
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

@danger-firebase-ios
Copy link

1 Warning
⚠️ New public headers were added, did you remember to add them to the umbrella header?

Generated by 🚫 Danger

Base automatically changed from ah/ai-generative-model-session to main March 10, 2026 04:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants