Skip to content

[firebase_ai]: Unhandled UNEXPECTED_TOOL_CALL FinishReason crashes response parsing #18181

@slaser79

Description

@slaser79

Is there an existing issue for this?

  • I have searched the existing issues.

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

  1. Use gemini-3-flash-preview with function calling declarations
  2. Send a multi-turn conversation with 40+ prompt parts
  3. The model occasionally returns UNEXPECTED_TOOL_CALL finish reason
  4. 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs AttentionThis issue needs maintainer attention.platform: androidIssues / PRs which are specifically for Android.platform: iosIssues / PRs which are specifically for iOS.platform: webIssues / PRs which are specifically for web.plugin: ailabel issues for firebase_ai plugintype: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions