Getting RuntimeError: API error: 504 Server Error: GATEWAY_TIMEOUT for url and cant solve it #9342
Replies: 1 comment
-
Hi there! A So even though you're running code locally, your How to fixIf you want to run 100% locally, change these URLs:
And make sure:
TL;DR Debug checklist
Optional: If you must call the cloud endpointThen a
Try shortening your flow or increasing the timeout in your Hope this helps! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am making a project which uses a Multi agent AI to ask questions based on a profile and get AI generated answers.



The error I am facing:
RuntimeError: API error: 504 Server Error: GATEWAY_TIMEOUT for url: https://api.langflow.astra.datastax.com/lf/076b1e4d-92ab-4b5a-a006-566774e08b62/api/v1/run/63d6d7e9-89ed-45e9-b3c6-657741ac6c65 Response:
Traceback:
File "C:\Users\DELL\Desktop\Study\Projects\Python AI Multi-Agent Tutorial (RAG, Streamlit, Langflow & More!)\langflow_env\lib\site-packages\streamlit\runtime\fragment.py", line 245, in wrapped_fragment
result = non_optional_func(*args, **kwargs)
File "C:\Users\DELL\Desktop\Study\Projects\Python AI Multi-Agent Tutorial (RAG, Streamlit, Langflow & More!)\main.py", line 164, in ask_ai_func
result = ask_ai(st.session_state.profile, user_question)
File "C:\Users\DELL\Desktop\Study\Projects\Python AI Multi-Agent Tutorial (RAG, Streamlit, Langflow & More!).\ai.py", line 69, in ask_ai
result = run_flow(
File "C:\Users\DELL\Desktop\Study\Projects\Python AI Multi-Agent Tutorial (RAG, Streamlit, Langflow & More!).\ai.py", line 57, in run_flow
raise RuntimeError(f"API error: {e}\nResponse: {getattr(e.response, 'text', None)}")
The code I have is:
APPLICATION_TOKEN = os.getenv("LANGFLOW_TOKEN")
def dict_to_string(obj, level=0):
strings = []
indent = " " * level
if isinstance(obj, dict):
for key, value in obj.items():
if isinstance(value, (dict, list)):
nested_string = dict_to_string(value, level + 1)
strings.append(f"{indent}{key}: {nested_string}")
else:
strings.append(f"{indent}{key}: {value}")
elif isinstance(obj, list):
for idx, item in enumerate(obj):
nested_string = dict_to_string(item, level + 1)
strings.append(f"{indent}Item {idx + 1}: {nested_string}")
else:
strings.append(f"{indent}{obj}")
return ", ".join(strings)
def run_flow(
api_url: str,
tweaks: dict = None,
application_token: str = None,
timeout: int = 240
) -> dict:
# Langflow Cloud payload format: "input_value", "output_type", "input_type", "tweaks"
payload = {
"input_value": "",
"output_type": "chat",
"input_type": "chat",
"tweaks": tweaks if tweaks else {}
}
headers = {
"Content-Type": "application/json"
}
if application_token:
headers["Authorization"] = "Bearer " + application_token
try:
response = requests.post(api_url, json=payload, headers=headers, timeout=timeout)
response.raise_for_status()
except requests.exceptions.Timeout:
raise RuntimeError(f"API request timed out after {timeout} seconds.")
except requests.exceptions.RequestException as e:
raise RuntimeError(f"API error: {e}\nResponse: {getattr(e.response, 'text', None)}")
try:
resp_json = response.json()
except Exception as e:
raise RuntimeError(f"Bad or empty JSON in response: {response.text}") from e
return resp_json
def ask_ai(profile, question):
tweaks = {
"TextInput-72gju": {"input_value": question},
"TextInput-kcMpN": {"input_value": dict_to_string(profile)}
}
result = run_flow(
ASK_AI_API_URL,
tweaks=tweaks,
application_token=APPLICATION_TOKEN
)
print(json.dumps(result, indent=2)) # Print full response for debugging
# Adapt output extraction path if your API response structure differs:
try:
return result["outputs"][0]["outputs"][0]["results"]["text"]["data"]["text"]
except Exception:
print("Raw result for debugging:", json.dumps(result, indent=2))
raise
def get_macros(profile, goals):
tweaks = {
"TextInput-7K8yN": {"input_value": ", ".join(goals)},
"TextInput-Y8cHw": {"input_value": dict_to_string(profile)}
}
result = run_flow(
MACROS_API_URL,
tweaks=tweaks,
application_token=APPLICATION_TOKEN
)
try:
text_res = result["outputs"][0]["outputs"][0]["results"]["text"]["data"]["text"]
return json.loads(text_res)
except Exception:
print("Raw result for debugging:", json.dumps(result, indent=2))
raise
Beta Was this translation helpful? Give feedback.
All reactions