504 Deadline Exceeded Error

Hi folks, I ran into an error named 504 deadline exceeded error for Gemini.

def generate_search_queries(prompt: str) → dict:
system_prompt = ()
try:
response = model.generate_content([system_prompt, prompt])
print(“:brain: Gemini output:”, response.text)
json_data = json.loads(response.text.strip())

    return {
        "domain": json_data.get("domain", "software vendors"),
        "location": json_data.get("location", "California"),
        "quantity": int(json_data.get("quantity", 3))
    }

except Exception as e:
    print("⚠️ Failed to parse Gemini output:", e)
    return {
        "domain": "software vendors",
        "location": "California",
        "quantity": 3
    }

def generate_next_prompt_variation(intent_prompt: str, used_prompts: list[str]) → str:
try:
system_prompt = ()
response = model.generate_content(system_prompt)
prompt = response.text.strip()
prompt = prompt.strip("-•1234567890. ").strip()

    print("🧠 New prompt from Gemini:", prompt)
    return prompt

except Exception as e:
    print("❌ Error generating prompt variation:", e)
    return ""

Can someone give me a resolution on this please?

Hi @neel,

Welcome to forum, Is there a way that I can replicate this by an example/sample to try out and I can escalate the issue to the team.

Thank you!

Hi @Siva_Sravana_Kumar_N , you can replicate this by adding some prompts to the script inside system_Prompt(" ") and you can use these imports. This script is independent of other scripts.
import os

import json

import google.generativeai as genai

from dotenv import load_dotenv

Thank you for explaining that and yes I understand that. I was wondering if I need to provide a specific type of prompt or include certain wording to request that you generate the output in JSON format. Also if any specific prompt is triggering the error.

should it be , because of the prompt?

I tried using a random prompt with structured output and it works fine for me. I am just trying to understand if I can shape a prompt close to what you are trying to do.

504 DEADLINE_EXCEEDED: The service is unable to finish processing within the deadline. Your prompt (or context) is too large to be processed in time. Set a larger ‘timeout’ in your client request to avoid this error.

from google import genai
from google.colab import userdata

prompt = """List a few popular cookie recipes in JSON format.

Use this JSON schema:

Recipe = {'recipe_name': str, 'ingredients': list[str]}
Return: list[Recipe]"""

client = genai.Client(api_key=userdata.get("GOOGLE_API_KEY"))
response = client.models.generate_content(
    model='gemini-2.0-flash',
    contents=prompt,
)

# Use the response as a JSON string.
print(response.text)

Output:

```json
[
  {
    "recipe_name": "Chocolate Chip Cookies",
    "ingredients": [
      "2 1/4 cups all-purpose flour",
      "1 teaspoon baking soda",
      "1 teaspoon salt",
      "1 cup (2 sticks) unsalted butter, softened",
      "3/4 cup granulated sugar",
      "3/4 cup packed brown sugar",
      "1 teaspoon vanilla extract",
      "2 large eggs",
      "2 cups chocolate chips"
    ]
  },
  {
    "recipe_name": "Peanut Butter Cookies",
    "ingredients": [
      "1 cup (2 sticks) unsalted butter, softened",
      "1 cup peanut butter",
      "1 cup granulated sugar",
      "1 cup packed brown sugar",
      "2 large eggs",
      "1 teaspoon vanilla extract",
      "2 1/2 cups all-purpose flour",
      "1 teaspoon baking soda",
      "1/2 teaspoon salt"
    ]
  },
  {
    "recipe_name": "Sugar Cookies",
    "ingredients": [
      "1 1/2 cups (3 sticks) unsalted butter, softened",
      "2 cups granulated sugar",
      "4 large eggs",
      "1 teaspoon vanilla extract",
      "5 cups all-purpose flour",
      "2 teaspoons baking powder",
      "1 teaspoon salt"
    ]
  },
  {
    "recipe_name": "Oatmeal Raisin Cookies",
    "ingredients": [
      "1 cup (2 sticks) unsalted butter, softened",
      "3/4 cup packed brown sugar",
      "1/2 cup granulated sugar",
      "2 large eggs",
      "1 teaspoon vanilla extract",
      "1 1/2 cups all-purpose flour",
      "1 teaspoon baking soda",
      "1 teaspoon ground cinnamon",
      "1/2 teaspoon salt",
      "3 cups rolled oats",
      "1 cup raisins"
    ]
  }
]

hmm i see, please try the following:

import os
import json
import google.generativeai as genai
from dotenv import load_dotenv

load_dotenv()
genai.configure(api_key=os.getenv(“GEMINI_API_KEY”))
model = genai.GenerativeModel(“gemini-2.5-pro-preview-03-25”)

:magnifying_glass_tilted_left: Extract structured parameters from a freeform user intent

def generate_search_queries(prompt: str) → dict:
system_prompt = (
“You are a backend JSON extractor for a vendor search system.\n”
“Extract:\n”
“- domain: type of vendors or businesses\n”
“- location: city/state/country\n”
“- quantity: number of vendors requested (default 3)\n”
“Return ONLY a JSON object:\n”
“{ "domain": "…", "location": "…", "quantity": 3 }”
)

try:
    response = model.generate_content([system_prompt, prompt])
    print("🧠 Gemini output:", response.text)
    json_data = json.loads(response.text.strip())

    return {
        "domain": json_data.get("domain", "software vendors"),
        "location": json_data.get("location", "California"),
        "quantity": int(json_data.get("quantity", 3))
    }

except Exception as e:
    print("⚠️ Failed to parse Gemini output:", e)
    return {
        "domain": "software vendors",
        "location": "California",
        "quantity": 3
    }

def generate_next_prompt_variation(intent_prompt: str, used_prompts: list[str]) → str:
try:
system_prompt = (
“You are an intelligent assistant that generates creative Google search queries "
“to help find software vendors for a specific business need.\n\n”
f"Original intent:\n"{intent_prompt}"\n\n”
f"Already used prompts:\n{used_prompts}\n\n"
"Your task is to generate one new and distinct Google search prompt that still aligns with the original intent "
“but explores a different angle (e.g., billing, cloud-based, practice management, portals, etc).\n”
“Return a clean, single-line search prompt without bullets or formatting.”
)

    response = model.generate_content(system_prompt)
    prompt = response.text.strip()
    prompt = prompt.strip("-•1234567890. ").strip()

    print("🧠 New prompt from Gemini:", prompt)
    return prompt

except Exception as e:
    print("❌ Error generating prompt variation:", e)
    return ""

hey @Siva_Sravana_Kumar_N did you try my prompt ? I am still facing the issues.