Skip to content

🚀 [Firebase_ai] Expose ApiClient and allow passing custom ApiClient for generativeModel #17960

@villecm

Description

@villecm

Expose ApiClient and allow Allow custom ApiClient for generativeModel

Problem
Currently, generativeModel restricts the injection of a custom ApiClient primarily to "package private test-only" methods (createModelWithClient).

This prevents critical production use cases, such as:

  • Middleware: Injecting middleware for server-side function calls or request transformation.
  • Network Config: Managing custom proxies, timeouts, or logging.

Additionally, this restriction forces code duplication between production logic (createGenerativeModel) and test logic (createModelWithClient), which are nearly identical except for the client parameter.

Proposal
Refactor GenerativeModel to unify initialization logic and officially support a custom client.

  1. Refactor Internal Constructor: Update GenerativeModel._ to accept an optional ApiClient? client. If provided, use it; otherwise, instantiate the default HttpApiClient.
  2. Update Public API: Add an optional ApiClient? client parameter to createGenerativeModel.
  3. Cleanup: Deprecate or remove createModelWithClient and _constructTestModel as they become redundant.

Reasoning

  • Extensibility: Enables the creation of middleware that complements current features (e.g., orchestrating server-side function calls).
  • Maintainability (DRY): Removes duplicate constructor chains, ensuring testing logic mirrors production logic.
  • Clarity: Signals that using a custom client is a supported feature for advanced network configurations.

Reference
Draft implementation available here: main...villecm:flutterfire:main

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