Skip to content

🚀 [firebase_ai] Support system/context message turns in chat history for backend or memory updates #17797

@lhengl

Description

@lhengl

What feature would you like to see?

Feature Summary

Add support for injecting backend- or system-originated events and memory updates into the chat history as their own distinct turn type. This would allow these messages to be properly recognized by the AI model—without being misattributed to the user or model—and ensure the model has the right context for generating accurate, helpful responses. This could be accomplished by supporting a new message role (such as system or context) in the chat history, or by providing an explicit API for adding non-user, non-model context turns.

Description:

I’m building a Flutter AI chat app with firebase_ai: ^2.3.0 and need to inject extra context—such as backend-triggered events or memory updates—into the chat history so the model can provide informed responses.

However, adding Content.system() as a turn in the chat history results in the following error:

I/flutter (22326): Please use a valid role: user, model.
I/flutter (22326): #0   DeveloperSerialization.parseGenerateContentResponse (package:firebase_ai/src/developer/api.dart:103:56)

System content is only allowed as a system instruction, not as a message turn.

Why this is a problem

While FunctionResponse works for tool call responses, many events—like backend updates ("Your booking was rescheduled")—are triggered asynchronously, not by a user or model prompt or a model-initiated function call. Injecting these as FunctionResponse technically seems possible, but it feels like a workaround, and I'm not even sure if this actually works as intended since there was no function invocation.

Using a role of user for these events doesn't make sense, because the update or context did not originate from the user.

Using a role of model also doesn't make sense, as these messages are not generated by the model.

Question

  1. Is it supported or recommended to use FunctionResponse for backend events that aren't the result of a function call?
  2. Or is there a better way (e.g., a dedicated system/context message type, or allowing Content.system() in the message history) to inject backend or app-originated events into the chat?

Why this matters

  1. Enables the app to push backend or memory updates into the conversation history in the correct order
  2. Cleanly distinguishes between user/model/tool and system-originated events

Version:

  • firebase_ai: 2.3.0
  • Flutter stable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs AttentionThis issue needs maintainer attention.plugin: ailabel issues for firebase_ai plugintype: enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions