Is there an existing issue for this?
Which plugins are affected?
Other
Which platforms are affected?
Web
Description
Affected plugin: firebase_ai (not in the dropdown). The code is pure Dart, so other platforms are likely affected too; we have only built for Web.
When TemplateChatSession.sendMessage auto-executes functions (TemplateAutoFunctionDeclaration), only the original message and the final model response are added to history:
// lib/src/server_template/template_chat.dart, sendMessage
if (!shouldAutoExecute) {
if (response.candidates case [final candidate, ...]) {
_history.add(message);
// ...
_history.add(normalizedContent);
}
return response;
}
The intermediate turns collected in requestHistory are discarded: the model's functionCall content and the function responses. On the next sendMessage, the model no longer sees what its tools returned. For example, a createCharacter call that returned a new id is gone from history, so a follow-up call that needs that id has nothing to refer to.
The other three code paths keep the whole exchange with _history.addAll(requestHistory):
TemplateChatSession.sendMessageStream
ChatSession.sendMessage
ChatSession.sendMessageStream
So this looks like an oversight in this one path rather than intended behavior.
Expected: after auto-executed function turns, history contains the user message, each model functionCall turn, each function response turn and the final response, as sendMessageStream does. Likely fix:
_history.addAll(requestHistory);
_history.add(normalizedContent);
Actual: history contains only the user message and the final response.
This is still the code on main as of today. We found it while building function calling on a server prompt template, and worked around it by using sendMessageStream.
Reproducing the issue
- Create a server prompt template that declares one function, e.g.
getValue, and includes {{history}}.
final chat = FirebaseAI.googleAI().templateGenerativeModel().startChat('<template-id>', inputs: {}, tools: [TemplateTool.functionDeclarations([TemplateAutoFunctionDeclaration(name: 'getValue', parameters: {}, callable: (_) => {'value': 42})])]);
await chat.sendMessage(Content.text('Call getValue and tell me the result.')); The model calls the function and the SDK auto-executes it.
- Inspect
chat.history: it has 2 entries (user message, final reply). The functionCall and functionResponse turns are missing. Doing the same with sendMessageStream gives 4 entries.
Firebase Core version
4.14.0
Flutter Version
3.44.1
Relevant Log Output
No error is raised; the turns are silently missing from `chat.history`.
Flutter dependencies
Expand Flutter dependencies snippet
firebase_ai: 4.0.0
firebase_core: 4.14.0
Additional context and comments
Related, but already fixed on main in 6eb250f (#18685), not yet released: in 4.0.0, function responses are sent with role function, which the template endpoint rejects with Role 'function' is not supported. Please use a valid role: SYSTEM, SYSTEM_1, USER, ASSISTANT, DEVELOPER, CONTEXT, USER_CONTEXT, MODEL, USER. Mentioned only so it isn't re-reported. A release including that fix would be welcome.
Is there an existing issue for this?
Which plugins are affected?
Other
Which platforms are affected?
Web
Description
Affected plugin:
firebase_ai(not in the dropdown). The code is pure Dart, so other platforms are likely affected too; we have only built for Web.When
TemplateChatSession.sendMessageauto-executes functions (TemplateAutoFunctionDeclaration), only the original message and the final model response are added tohistory:The intermediate turns collected in
requestHistoryare discarded: the model'sfunctionCallcontent and the function responses. On the nextsendMessage, the model no longer sees what its tools returned. For example, acreateCharactercall that returned a new id is gone from history, so a follow-up call that needs that id has nothing to refer to.The other three code paths keep the whole exchange with
_history.addAll(requestHistory):TemplateChatSession.sendMessageStreamChatSession.sendMessageChatSession.sendMessageStreamSo this looks like an oversight in this one path rather than intended behavior.
Expected: after auto-executed function turns,
historycontains the user message, each modelfunctionCallturn, each function response turn and the final response, assendMessageStreamdoes. Likely fix:Actual:
historycontains only the user message and the final response.This is still the code on
mainas of today. We found it while building function calling on a server prompt template, and worked around it by usingsendMessageStream.Reproducing the issue
getValue, and includes{{history}}.final chat = FirebaseAI.googleAI().templateGenerativeModel().startChat('<template-id>', inputs: {}, tools: [TemplateTool.functionDeclarations([TemplateAutoFunctionDeclaration(name: 'getValue', parameters: {}, callable: (_) => {'value': 42})])]);await chat.sendMessage(Content.text('Call getValue and tell me the result.'));The model calls the function and the SDK auto-executes it.chat.history: it has 2 entries (user message, final reply). ThefunctionCallandfunctionResponseturns are missing. Doing the same withsendMessageStreamgives 4 entries.Firebase Core version
4.14.0
Flutter Version
3.44.1
Relevant Log Output
Flutter dependencies
Expand
Flutter dependenciessnippetAdditional context and comments
Related, but already fixed on
mainin 6eb250f (#18685), not yet released: in 4.0.0, function responses are sent with rolefunction, which the template endpoint rejects withRole 'function' is not supported. Please use a valid role: SYSTEM, SYSTEM_1, USER, ASSISTANT, DEVELOPER, CONTEXT, USER_CONTEXT, MODEL, USER.Mentioned only so it isn't re-reported. A release including that fix would be welcome.