Fix Gemini API Deadline Exceeded & Timeout Errors
Updated 10/13/2026
The DEADLINE_EXCEEDED error (often manifesting as HTTP status code 504, 408, or an SDK-specific timeout error) occurs when a request to the Google Gemini API takes longer to complete than the allocated time limit. This timeout can be enforced either by your local SDK client configuration, an intermediate proxy, or the Gemini API gateway itself.
This error is highly common when working with resource-intensive tasks, such as passing large video/audio files to gemini-1.5-pro, requesting complex code generation, or using high max_output_tokens settings. Follow these step-by-step instructions to resolve Gemini API timeouts and deadline exceeded errors.
1. Increase the SDK client timeout limits By default, many SDK clients and HTTP libraries have a conservative timeout limit (e.g., 30 or 60 seconds). For complex reasoning tasks or multimodal inputs, Gemini may take longer than this default window to generate a complete response.
You must explicitly increase the HTTP timeout threshold in your code.
Python SDK Example If you are using the official `google-generativeai` package, pass a custom timeout value using the `request_options` parameter:
`python import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY") model = genai.GenerativeModel('gemini-1.5-pro')
Set the timeout to 300 seconds (5 minutes) for heavy requests response = model.generate_content( "Analyze this long document and summarize it...", request_options={"timeout": 300.0} ) print(response.text) ```
Node.js SDK Example For the `@google/generative-ai` Node.js package, specify the timeout (in milliseconds) within the request configuration options:
`javascript const { GoogleGenAI } = require("@google/genai"); const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });
async function generate() { const response = await ai.models.generateContent({ model: 'gemini-1.5-pro', contents: 'Perform a detailed analysis of...', config: { // Note: Some legacy SDK versions set timeouts via request options timeout: 300000 // 5 minutes } }); console.log(response.text); } `
2. Switch to streaming responses Waiting for the entire model payload to generate can easily trigger socket timeouts on your server or serverless functions (like AWS Lambda or Vercel, which have strict execution limits).
By streaming the response, Gemini begins sending chunks of text as soon as they are processed. This keeps the connection active and prevents gateway timeouts.
Python Streaming Example ```python response = model.generate_content( "Write an exhaustive 2000-word guide on quantum computing.", stream=True )
for chunk in response: print(chunk.text, end="") `
Using streaming ensures that your local client receives data immediately, resetting any read-idle timers on your firewall or proxy.
3. Reduce input payload and media complexity Passing very large context windows (like 100MB+ PDF files or long video files) requires significant processing time before the first token can even be generated.
If you consistently hit timeouts with large files, optimize your inputs: * Downsample media: Compress videos to 720p or lower, and convert high-bitrate audio to 16kHz mono MP3 or WAV before uploading them via the Files API. * Break up text inputs: If passing a massive codebase or document, split it into smaller segments or use a semantic search (RAG) system to pass only the most relevant context. * Check file state: Ensure files uploaded via the Files API have completed their processing stage (ACTIVE status) before querying the model with them.
4. Lower model parameters If your request asks for a very high token count or uses complex generation parameters, the generation time scales up accordingly.
To speed up response times: * Lower max_output_tokens: Limit the maximum response length. If you do not set this, the model may attempt to output up to its maximum capacity, increasing the risk of a timeout. * Use gemini-1.5-flash: For high-throughput or speed-critical tasks, switch models. gemini-1.5-flash is significantly faster than gemini-1.5-pro and is far less likely to time out under normal conditions.
5. Implement exponential backoff If the `DEADLINE_EXCEEDED` error is accompanied by a `504` status code from the server side, Google's backend may be temporarily overloaded. Implement a retry mechanism with exponential backoff to handle these transient failures.
If using Python, you can utilize the tenacity library to automatically retry failed API calls:
`python from tenacity import retry, stop_after_attempt, wait_exponential
@retry(wait=wait_exponential(multiplier=1, min=4, max=10), stop=stop_after_attempt(5)) def call_gemini_with_retry(): return model.generate_content("Your prompt here") `
When to escalate If you have increased your client timeouts to over 5 minutes, implemented streaming, reduced your input size, and still receive consistent `DEADLINE_EXCEEDED` errors across multiple geographic locations, check the Google Cloud Service Health Dashboard. If there is an active outage on the Gemini API or Vertex AI endpoints, you must wait for Google to resolve the backend service degradation.
Related Gemini help
Set up your AI once so you repeat yourself less.
Try AI Setup ProfileTickd is independent and not affiliated with any AI company.