Skip to content

Add thinking mode for ai logic hybrid - #8678

Open
VinayGuthal wants to merge 7 commits into
mainfrom
add_thinking_mode
Open

VinayGuthal wants to merge 7 commits into
mainfrom
add_thinking_mode

Conversation

@VinayGuthal

@VinayGuthal VinayGuthal commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Adds support for thinking mode (enableThinking) when generating content using on-device and hybrid inference modes.

Summary of Changes

  • firebase-ai-ondevice-interop:
    • Added enableThinking: Boolean? to GenerateContentRequest.
    • Added thoughtProcess: List<Candidate> to GenerateContentResponse.
    • Added isThinkingModeAvailable(): Boolean to GenerativeModel.
    • Updated api.txt.
  • firebase-ai-ondevice:
    • Implemented GenerativeModelImpl.isThinkingModeAvailable() delegating to ML Kit's GenerativeModel.isThinkingModeAvailable().
    • Updated Converters.kt to pass enableThinking to the ML Kit request and map thoughtProcess candidates to the interop GenerateContentResponse.
  • firebase-ai:
    • Added enableThinking: Boolean? = null to OnDeviceConfig and updated api.txt.
    • Updated OnDeviceGenerativeModelProvider to check onDeviceModel.isThinkingModeAvailable() when enableThinking == true (throwing FirebaseAIOnDeviceNotAvailableException so PREFER_ON_DEVICE falls back to cloud when unsupported) and forward enableThinking to the on-device request.
    • Updated Candidate and OnDeviceGenerativeModelProvider response mapping to convert interop thoughtProcess candidates into TextPart entries with isThought = true (populating GenerateContentResponse.thoughtSummary for both unary and streaming responses).
  • Tests & Changelogs: Added unit tests in ConvertersTest, GenerativeModelImplTest, and OnDeviceGenerativeModelProviderTests, and updated CHANGELOG.md across all three modules.

Tested at firebase/quickstart-android#2848

@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
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the 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 counterproductive. 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.

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

📝 PRs merging into main branch

Our main branch should always be in a releasable state. If you are working on a larger change, or if you don't want this change to see the light of the day just yet, consider using a feature branch first, and only merge into the main branch when the code complete and ready to be released.

@VinayGuthal

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 adds support for thinking mode in on-device content generation, introducing configuration options like enableThinking, tracking the model's reasoning process via thoughtProcess, and adding availability checks. The review feedback highlights two key improvements: first, updating the candidate mapping logic to accept a list of thoughts rather than a single thought to prevent reasoning steps from being discarded when there is only one candidate; second, disabling the thinking mode check during token counting to avoid unnecessary failures, as tokenization does not execute the model's thinking process.

Comment thread ai-logic/firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Candidate.kt Outdated
VinayGuthal and others added 3 commits October 8, 2026 10:37
…nerativemodel/OnDeviceGenerativeModelProvider.kt

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@VinayGuthal VinayGuthal changed the title Add thinking mode. Add thinking mode for ai logic hybrid Oct 8, 2026
@VinayGuthal

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 support for thinking mode in on-device content generation, adding enableThinking to configuration and request classes, thoughtProcess to response classes, and isThinkingModeAvailable to the generative model interface. Feedback on these changes highlights a critical issue in OnDeviceGenerativeModelProvider.toPublicResponse where sequential thought steps are incorrectly sliced across multiple candidates or mapped to separate candidates when the response candidates list is empty. Refactoring is suggested to associate the entire thoughtProcess with each candidate, which would also allow the removal of the now-redundant fromInteropThought helper function.

Comment thread ai-logic/firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Candidate.kt Outdated
@VinayGuthal

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 support for "thinking mode" in on-device content generation by adding enableThinking to request configurations, thoughtProcess to responses, and isThinkingModeAvailable to model interfaces across the firebase-ai and firebase-ai-ondevice modules. Feedback recommends wrapping the initialization block of generateContentStream in a try-catch to ensure setup exceptions are properly wrapped in FirebaseAIException, and filtering out empty thoughts in fromInteropThought to maintain consistency with fromInterop.

VinayGuthal and others added 2 commits October 8, 2026 12:04
…pe/Candidate.kt

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@VinayGuthal

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 support for thinking mode in on-device content generation. It adds enableThinking to request configurations, exposes thoughtProcess in responses, and provides a method to check if thinking mode is available on the model. These changes are propagated through the interop layers, public APIs, and converters, accompanied by corresponding unit tests. Feedback on the changes suggests removing a fragile assertion in ConvertersTest.kt that checks the default value of enableThinking when not explicitly set, as it contradicts the test's stated design principle.

@VinayGuthal
VinayGuthal requested a review from rlazo October 9, 2026 13:37
@@ -1,5 +1,6 @@
# Unreleased

- [feature] Added `enableThinking` to `GenerateContentRequest`, `thoughtProcess` to `GenerateContentResponse`, and `isThinkingModeAvailable` to `GenerativeModel` to support thinking mode.

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.

Max 80 chars per line. Also, add PR number

topK = this@toMlKit.topK
seed = this@toMlKit.seed
systemInstruction = this@toMlKit.systemInstruction?.toMlKitSystemInstruction()
this@toMlKit.enableThinking?.let { enableThinking = it }

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.

This statement logic is reversed to all the others (not a direct assignment)

@@ -1,5 +1,6 @@
# Unreleased

- [feature] Added support for thinking mode in on-device content generation requests and responses.

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.

Add PR number

nonEmptyThoughts.forEach { thought ->
part(TextPart(text = thought.text, isThought = true, thoughtSignature = null))
}
if (candidate.text.isNotEmpty() || nonEmptyThoughts.isEmpty()) {

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.

could you walk me through this part? All thoughts are included in the content, but if there are no thoughts, an empty text is used?

@@ -1,5 +1,6 @@
# Unreleased

- [feature] Added `enableThinking` to `OnDeviceConfig` to support [thinking mode](https://firebase.google.com/docs/ai-logic/thinking) in on-device and hybrid inference modes, with automatic cloud fallback when the on-device model does not support thinking.

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.

Max length 80, include PR number

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants