Handle errors, implement retries, and work with rate limits in Fli
Fli includes built-in error handling, automatic retries, and rate limiting to ensure reliable flight searches. This guide covers how to work with these features and implement additional error handling in your application.
Fli enforces a rate limit of 10 requests per second to prevent overwhelming the Google Flights API:
from fli.search.client import get_client# Get the shared client instanceclient = get_client()# Make requests - automatically rate limitedfor filters in filter_list: results = search.search(filters) # Automatically waits if rate limit exceeded
@limits(calls=10, period=1) - Maximum 10 calls per second
@sleep_and_retry - Automatically waits when limit is reached
@retry - Retries failed requests with exponential backoff
Do not create multiple SearchFlights instances in rapid succession. Use a single instance or the shared client via get_client() to ensure rate limiting works correctly.
# ❌ InvalidFlightSegment( departure_airport=[[Airport.JFK, 0]], arrival_airport=[[Airport.JFK, 0]], # Same as departure travel_date="2026-04-15")# Error: "Departure and arrival airports must be different"
Travel date in the past
# ❌ InvalidFlightSegment( departure_airport=[[Airport.JFK, 0]], arrival_airport=[[Airport.LAX, 0]], travel_date="2024-01-01" # In the past)# Error: "Travel date cannot be in the past"
Invalid time restrictions
# ❌ InvalidTimeRestrictions( earliest_departure=10, latest_departure=6 # Earlier than earliest!)# The validator will automatically swap these values
from fli.search import SearchFlightssearch = SearchFlights()try: results = search.search(filters) if not results: print("No flights found for the given criteria") else: print(f"Found {len(results)} flights")except Exception as e: error_msg = str(e) if "GET request failed" in error_msg or "POST request failed" in error_msg: print(f"Network error: {error_msg}") elif "Search failed" in error_msg: print(f"API error: {error_msg}") else: print(f"Unexpected error: {error_msg}")
The error messages from SearchFlights.search() include context about what failed, making debugging easier.
from fli.search.client import get_client# Reuse the same client for multiple searchesclient = get_client()# Perform multiple searchesfor filters in filter_list: results = search.search(filters)
If you encounter persistent API errors, ensure you’re not being blocked by Google Flights. The built-in rate limiting and browser impersonation help prevent this, but excessive requests can still trigger blocks.