Is there an existing issue for this?
Which plugins are affected?
firebase_ai
Which platforms are affected?
Android, iOS, Web
Description
FinishReason.parseValue() in api.dart throws FormatException when Gemini returns UNEXPECTED_TOOL_CALL as a finish reason. This is a new finish reason from the Gemini API that isn't in the SDK's enum.
static FinishReason parseValue(Object jsonObject) {
return switch (jsonObject) {
'STOP' => FinishReason.stop,
'MAX_TOKENS' => FinishReason.maxTokens,
// ... other cases
'MALFORMED_FUNCTION_CALL' => FinishReason.malformedFunctionCall,
_ => throw FormatException('Unhandled FinishReason format', jsonObject), // <-- crashes here
};
}
This is the same pattern as #17812 (MALFORMED_FUNCTION_CALL was missing and was added). UNEXPECTED_TOOL_CALL now needs the same treatment.
Impact
Suggested Fix
Add UNEXPECTED_TOOL_CALL to the FinishReason enum and parseValue switch:
enum FinishReason {
// ... existing values
malformedFunctionCall('MALFORMED_FUNCTION_CALL'),
unexpectedToolCall('UNEXPECTED_TOOL_CALL'), // <-- add this
other('OTHER');
Or better: map unknown finish reasons to FinishReason.other instead of throwing, to be forward-compatible with future Gemini API additions:
_ => FinishReason.other, // instead of throw FormatException
References
Reproducing the issue
- Use
gemini-3-flash-preview with function calling declarations
- Send a multi-turn conversation with 40+ prompt parts
- The model occasionally returns
UNEXPECTED_TOOL_CALL finish reason
- SDK throws
FormatException: Unhandled FinishReason format and the response is lost
Firebase AI SDK version
3.7.0 (also confirmed missing in 3.9.0 and 3.10.0)
Is there an existing issue for this?
Which plugins are affected?
firebase_ai
Which platforms are affected?
Android, iOS, Web
Description
FinishReason.parseValue()inapi.dartthrowsFormatExceptionwhen Gemini returnsUNEXPECTED_TOOL_CALLas a finish reason. This is a new finish reason from the Gemini API that isn't in the SDK's enum.This is the same pattern as #17812 (
MALFORMED_FUNCTION_CALLwas missing and was added).UNEXPECTED_TOOL_CALLnow needs the same treatment.Impact
FormatExceptionis thrown during stream parsing, so the entire response is lost — including any valid text or tool call data the model returnedgemini-3-flash-previewandgemini-2.5-flashare commonly affectedgoogleapis/java-genai) has already fixed this: Exception in Chat.sendMessage when model replies with UNEXPECTED_TOOL_CALL googleapis/java-genai#466Suggested Fix
Add
UNEXPECTED_TOOL_CALLto theFinishReasonenum andparseValueswitch:Or better: map unknown finish reasons to
FinishReason.otherinstead of throwing, to be forward-compatible with future Gemini API additions:References
Reproducing the issue
gemini-3-flash-previewwith function calling declarationsUNEXPECTED_TOOL_CALLfinish reasonFormatException: Unhandled FinishReason formatand the response is lostFirebase AI SDK version
3.7.0 (also confirmed missing in 3.9.0 and 3.10.0)