Skip to content

[AI] Add HybridModel to support a fallback model - #16110

Merged
andrewheard merged 9 commits into
mainfrom
ah/ai-hybrid-model
Apr 23, 2026
Merged

[AI] Add HybridModel to support a fallback model#16110
andrewheard merged 9 commits into
mainfrom
ah/ai-hybrid-model

Conversation

@andrewheard

Copy link
Copy Markdown
Contributor

Added a new HybridModel type that is a combination of a primary LanguageModel and a secondary model to fallback to if the primary model is unavailable or throws an error. This is preparing for hybrid inference using the on-device SystemLanguageModel from the Foundation Models framework, as well GeminiModel in the cloud.

Since HybridModel supports any model conforming to LanguageModel, this also means that fallback support is available for multiple Gemini models or backends (Gemini Developer API and Vertex AI API). This feature is used in the integration tests, which specify invalid model names to simulate a model being unavailable and falling back to another valid model name.

#no-changelog

@gemini-code-assist

Copy link
Copy Markdown
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
Copy Markdown
1 Warning
⚠️ New public headers were added, did you remember to add them to the umbrella header?

Generated by 🚫 Danger

@gemini-code-assist

Copy link
Copy Markdown
Contributor

It looks like you're trying to trigger a pull request summary and a code review. The /gemini summary and /gemini review commands are used to initiate these actions. Please post these commands as new top-level comments on the pull request to trigger a new summary or review. I am unable to execute commands within a reply to a comment. For more information on how to use Gemini Code Assist, please refer to the help message by typing /gemini help.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

This pull request introduces a flexible fallback mechanism for generative models by adding a HybridModel type. This allows developers to specify a primary model and a secondary fallback model, ensuring that requests can still succeed even if the primary model is unavailable or encounters errors. The implementation includes necessary session management to handle these transitions gracefully while respecting existing conversation history.

Highlights

  • New HybridModel Type: Introduced a HybridModel that allows for a primary language model with a secondary fallback model, enabling robust inference scenarios.
  • Session Fallback Logic: Implemented HybridModelSession to handle automatic switching to the secondary model if the primary model fails, provided no conversation history has been established.
  • Integration Testing: Added comprehensive integration tests to verify fallback behavior across various scenarios, including nested hybrid models and streaming responses.
  • API Updates: Updated GenerateContentResponse to include modelVersion and exposed hybridModel factory methods for easier configuration.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Activity
  • Pull request created by andrewheard.
  • Gemini Code Assist bot provided setup instructions.
  • Danger bot issued a warning regarding new public headers.
  • Author requested a summary and review via commands.
  • Bot clarified that commands must be top-level comments.
  • Author re-issued the summary command.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a hybrid model feature that allows for a primary model with a secondary fallback. It includes the HybridModel, HybridModelProvider, and HybridModelSession implementations, along with updates to GenerateContentResponse to track the model version used. Feedback focuses on refining the fallback logic: ensuring streaming responses do not produce corrupted output if a failure occurs after data has been yielded, maintaining session consistency in multi-turn conversations by sticking to the secondary model after an initial fallback, and optimizing resource usage by lazily initializing the secondary session.

Comment thread FirebaseAI/Sources/Types/Internal/HybridModelSession.swift
Comment thread FirebaseAI/Sources/Types/Internal/HybridModelSession.swift
Comment thread FirebaseAI/Sources/Types/Public/HybridModel.swift Outdated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a hybrid model feature that enables a primary language model to fall back to a secondary model upon failure. Key changes include the implementation of HybridModel, HybridModelProvider, and HybridModelSession, along with updates to GenerateContentResponse to track the model version. The review feedback identifies a critical bug in the streaming fallback logic that could cause hangs, suggests optimizing resource usage through lazy initialization of the secondary session, and recommends improvements to error message formatting and documentation typos.

Comment thread FirebaseAI/Sources/Types/Internal/HybridModelSession.swift
Comment thread FirebaseAI/Sources/Types/Public/HybridModel.swift
Comment thread FirebaseAI/Sources/Types/Public/HybridModel.swift Outdated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a hybrid model feature to the FirebaseAI library, enabling a primary model to fallback to a secondary model if the primary session fails. It includes the implementation of HybridModel, HybridModelProvider, and HybridModelSession, along with updates to GenerateContentResponse to track the modelVersion. Review feedback recommends making the modelVersion property public for improved debugging and telemetry, refactoring the hybrid session to lazily initialize the secondary model for better resource efficiency, and correcting a version typo in a TODO comment.

Comment thread FirebaseAI/Sources/GenerateContentResponse.swift
Comment thread FirebaseAI/Sources/Types/Public/HybridModel.swift
@andrewheard
andrewheard marked this pull request as ready for review April 23, 2026 06:23
@andrewheard
andrewheard requested a review from a team as a code owner April 23, 2026 06:23
@andrewheard
andrewheard requested a review from paulb777 April 23, 2026 06:23
@andrewheard
andrewheard merged commit 1067db6 into main Apr 23, 2026
132 of 133 checks passed
@andrewheard
andrewheard deleted the ah/ai-hybrid-model branch April 23, 2026 15:36
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