Skip to content

Add support for system instructions in the ondevice sdk. - #8663

Open
VinayGuthal wants to merge 2 commits into
mainfrom
add_system_instructions
Open

VinayGuthal wants to merge 2 commits into
mainfrom
add_system_instructions

Conversation

@VinayGuthal

@VinayGuthal VinayGuthal commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Adds support for systemInstruction when generating content using on-device and hybrid inference modes.

Summary of Changes

  • firebase-ai-ondevice-interop: Added an optional systemInstruction: TextPart? property to GenerateContentRequest and updated api.txt.
  • firebase-ai-ondevice: Updated GenerateContentRequest.toMlKit() in Converters.kt to convert the interop TextPart into an ML Kit SystemInstruction.
  • firebase-ai:
    • Passed systemInstruction from GenerativeModel.Builder to OnDeviceGenerativeModelProvider.
    • Updated OnDeviceGenerativeModelProvider to validate that systemInstruction contains only text parts (throwing FirebaseAIOnDeviceInvalidRequestException for non-text parts), concatenate multiple TextPart instances with a newline, and attach it to the on-device request.
  • Tests & Changelogs: Added unit tests in ConvertersTest, GenerativeModelBuilderTests, and OnDeviceGenerativeModelProviderTests, and updated CHANGELOG.md across all three modules.

@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 1, 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
VinayGuthal requested a review from rlazo October 1, 2026 20:39
@VinayGuthal VinayGuthal changed the title add system instructions Add support for system instructions in the ondevice sdk. Oct 1, 2026
@rlazo

rlazo commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

/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 systemInstruction in on-device content generation requests across the firebase-ai-ondevice-interop, firebase-ai-ondevice, and firebase-ai modules. It includes API updates, converters, validation logic to ensure only text parts are used in system instructions, and comprehensive unit tests. The feedback suggests optimizing the validation logic in OnDeviceGenerativeModelProvider to avoid filtering the parts list twice in the happy path.

Comment on lines +251 to +268
val unsupportedSystemParts = instruction.parts.filter { it !is TextPart }
if (unsupportedSystemParts.isNotEmpty()) {
throw FirebaseAIException.from(
FirebaseAIOnDeviceInvalidRequestException(
IllegalArgumentException(
"On-device model does not support non-text parts in systemInstruction: ${unsupportedSystemParts.map { it::class.java.simpleName }}"
)
)
)
}
val systemTextParts =
instruction.parts.filterIsInstance<TextPart>().also {
if (it.size > 1)
Log.w(
TAG,
"On-device model does not support multiple text parts in systemInstruction, concatenating them instead"
)
}

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.

medium

We can optimize this logic to avoid filtering the parts list twice in the happy path (when all parts are TextPart). By checking if the size of the filtered TextPart list is less than the total parts list, we can determine if there are any unsupported parts and only perform the second filter/error handling when necessary.

        val systemTextParts = instruction.parts.filterIsInstance<TextPart>()
        if (systemTextParts.size < instruction.parts.size) {
          val unsupportedSystemParts = instruction.parts.filter { it !is TextPart }
          throw FirebaseAIException.from(
            FirebaseAIOnDeviceInvalidRequestException(
              IllegalArgumentException(
                \"On-device model does not support non-text parts in systemInstruction: ${unsupportedSystemParts.map { it::class.java.simpleName }}\"
              )
            )
          )
        }
        if (systemTextParts.size > 1) {
          Log.w(
            TAG,
            \"On-device model does not support multiple text parts in systemInstruction, concatenating them instead\"
          )
        }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

can you give a suggested commit

@rlazo rlazo 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.

Shouldn't the other api.txt files be updated too?

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

- [feature] Added `systemInstruction` property to `GenerateContentRequest`.

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.

include the PR number

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

- [feature] Added support for `systemInstruction` in on-device content generation requests.

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.

include the pr number

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

- [feature] Added support for `systemInstruction` in on-device and hybrid inference modes.

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.

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