Skip to content

[firebase_ai]: Not handling MALFORMED_FUNCTION_CALL FinishReason. #17812

@gspencergoog

Description

@gspencergoog

Is there an existing issue for this?

  • I have searched the existing issues.

Which plugins are affected?

Other

Which platforms are affected?

Web

Description

In api.dart, firebase_ai isn't handling the MALFORMED_FUNCTION_CALL finish reason, and instead throws an exception.

There should be a FinishReason enum for malformedFunctionCall and it should handle it and return it instead of throwing.

Here's the current code from api.dart that is the problem:

  static FinishReason parseValue(Object jsonObject) {
    return switch (jsonObject) {
      'UNSPECIFIED' => FinishReason.unknown,
      'STOP' => FinishReason.stop,
      'MAX_TOKENS' => FinishReason.maxTokens,
      'SAFETY' => FinishReason.safety,
      'RECITATION' => FinishReason.recitation,
      'OTHER' => FinishReason.other,
      _ => throw FormatException('Unhandled FinishReason format', jsonObject),
    };
  }

Reproducing the issue

Create an app and give it an invalid function call to send to Gemini. When it responds with a response that looks like this:

{
  "candidates": [
    {
      "finishReason": "MALFORMED_FUNCTION_CALL",
      "usageMetadata": {
        "promptTokenCount": 4474,
        "totalTokenCount": 4474,
        "promptTokensDetails": [
          {
            "modality": "TEXT",
            "tokenCount": 4474
          }
        ]
      },
      "modelVersion": "gemini-2.5-flash",
      "responseId": "aaaaaaa-bbbbbbbbb"
    }
  ]
}

You will get a FormatException.

Firebase Core version

firebase_ai 3.4.0

Flutter Version

3.38.0

Relevant Log Output

Flutter dependencies

Additional context and comments

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs AttentionThis issue needs maintainer attention.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