Skip to main content

GPTRouter Examples

This contains a collection of Python examples demonstrating the use of the GPTRouter API for text generation with various use cases and configurations.

Getting Started


  1. Getting The Server Running
    • You would need to have the GPTRouter server running, to run it locally you can have a look here
    • or you can use our Preview Deployment with baseURL and to get an API key please fill the form here and get the preview key delivered to you over the email

You can try out the GPTRouter using our PythonSDK or via the API Docs meanwhile we are working on JS and other Clients and are looking for contributors

Using the Python SDK

  1. Installing the SDK
pip install gptrouter

Or with conda:

conda install gptrouter -c conda-forge
  1. Create a .env file based on the template:

  2. Edit the .env file and fill in your GPT_ROUTER_API_TOKEN:


Examples Overview

Below is a link of each example included in the repository:

ExampleGeneration TypeDescription
Async Non StreamingTexthere 🚗
Async StreamingTexthere 🚁
Sync StreamingTexthere 🚙
Sync Non StreamingTexthere ✈️
Image GenerationsAudioExamples Coming Soon

Running the Examples

After setting up your .env with the API token and installing the necessary dependencies, you can run each example script using the python command. For instance:


Please note that the output will vary based on the input provided and the model and parameters specified in each example.

Managing the Fallback Order

we can pass the order of Priority for model in case of failure using ordered_generation_requests=[generation_request] since ordered_generation_requests takes a list input of models and providers that are accessed based on healthChecks and latency

to add multiple models you can do something like

from gpt_router.models import ModelGenerationRequest
from gpt_router.enums import ModelsEnum, ProvidersEnum

generation_request_1 = ModelGenerationRequest(

generation_request_2 = ModelGenerationRequest(

Managing Health Check Behaviour

To customise the behaviour of the Model Router based on your needs, head over to constants.ts file and you can change the following variables

// Interval for checking Health of models ,default to 5 minutes 
export const HEALTH_CHECK_CRON_JOB_EXPRESSION = "*/5 * * * *";

// To bypass an model if it has more latency and move to next model in priority list