Menu

Stop Calling getGenerativeModel() Everywhere! The Hidden Traps of Scaling Firebase AI to Multi-Model Angular Apps
📰

Stop Calling getGenerativeModel() Everywhere! The Hidden Traps of Scaling Firebase AI to Multi-Model Angular Apps

DEV Community: firebase·Connie Leung·about 22 hours ago
#QGTcTpKm
#devtofirebase#devto#dev#cachedbackend#models
Reading 0:00
15s threshold

Calling getGenerativeModel() directly inside VisionService and TextToSpeechService duplicates firebase/ai imports, safety settings, and thinking configurations while recreating client-side GenerativeModel objects on every request and triggering unnecessary garbage collection. In this post, we extract AiModelFactory to centralize and cache model instances for multi-model routing, and we resolve two hidden caching traps: request-level generationConfig overwrites and stale AI backends after Remote Config updates.

Architecture & Mental Model

VisionService and TextToSpeechService delegate model creation and lifecycle management to AiModelFactory :

On-Demand Model Requests : VisionService calls getModel(options) with AiModelOptions , while TextToSpeechService calls getTtsModel() .

Backend Validation and Cache Eviction ( getValidBackend() ) : Before returning a model, getValidBackend() compares ConfigService.getAiBackend() against #cachedBackend .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.