Get the latest tech news

Use the Gemini API with OpenAI Fallback in TypeScript


If you want to use Gemini’s public API, but at the same time have a safe fallback in case you have exhausted the rate limits, you can use the OpenAI TS/JS library and a few helper functions. In my particular case I needed a type-safe solution for a chartmaker app with a fallback since Gemini’s gemini-2.5-pro-exp-03-25 model is restricted to 20 request/min. First, you need to define which models you want to use so that they appear as autosuggest when you use the helper functions:

If you want to use Gemini’s public API, but at the same time have a safe fallback in case you have exhausted the rate limits, you can use the OpenAI TS/JS library and a few helper functions. In my particular case I needed a type-safe solution for a chartmaker app with a fallback since Gemini’s gemini-2.5-pro-exp-03-25 model is restricted to 20 request/min. The helper function requires one argument; an array of 2 configuration objects for the desired AI queries (in principle, you can add as many as you want, or choose other AIs that are compatible with the OpenAI library):

Get the Android app

Or read this on Hacker News

Read more on:

Photo of TypeScript

TypeScript

Photo of Gemini API

Gemini API

Photo of openai fallback

openai fallback

Related news:

News photo

Why Microsoft's Developers are Porting TypeScript to Go

News photo

Microsoft is making TypeScript 10x faster with native implementation in Go | Bigger TypeScript projects should now become much more manageable in all code editors

News photo

How to Implement a Cosine Similarity Function in TypeScript