# 504 Server Error: Gateway Time-out for url

**URL:** <https://discuss.huggingface.co/t/504-server-error-gateway-time-out-for-url/170008>\
**Category:** 🤗Hub\
**Created:** [November 6, 2025, 9:13am UTC](https://discuss.huggingface.co/t/504-server-error-gateway-time-out-for-url/170008 "2025-11-06T09:13:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eduardo7890](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@eduardo7890](https://discuss.huggingface.co/u/eduardo7890)\
**Post date:** [November 6, 2025, 9:13am UTC](https://discuss.huggingface.co/t/504-server-error-gateway-time-out-for-url/170008/1 "2025-11-06T09:13:37Z")

</div>

Hi everyone,

My script is having the following error:

504 Server Error: Gateway Time-out for url: **[https://router.huggingface.co/hf-inference/models/facebook/bart-large-mnli](https://router.huggingface.co/hf-inference/models/facebook/bart-large-mnli)**

there’s something not working properly

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [November 6, 2025, 9:13am UTC](https://discuss.huggingface.co/t/504-server-error-gateway-time-out-for-url/170008/2 "2025-11-06T09:13:38Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [November 6, 2025, 1:14pm UTC](https://discuss.huggingface.co/t/504-server-error-gateway-time-out-for-url/170008/3 "2025-11-06T13:14:15Z")

</div>



---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [November 6, 2025, 3:55pm UTC](https://discuss.huggingface.co/t/504-server-error-gateway-time-out-for-url/170008/4 "2025-11-06T15:55:46Z")

</div>

Seems working now?

```py
import os, json, time, random, requests

API_URL = "https://router.huggingface.co/hf-inference/models/facebook/bart-large-mnli"
HF_TOKEN = os.getenv("HF_TOKEN") # set: export HF_TOKEN=hf_...
TEXT = "I bought a device from your company. It does not work and I want a refund."
LABELS = ["refund", "legal", "faq"]

TIMEOUT = float(os.getenv("HF_TIMEOUT", "90"))
RETRIES = int(os.getenv("HF_RETRIES", "3"))
BASE = float(os.getenv("HF_BACKOFF_BASE", "1.5"))

def backoff(i): return BASE * (2 ** (i-1)) + random.uniform(0, 0.3)

headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
payload = {"inputs": TEXT, "parameters": {"candidate_labels": LABELS}}

for attempt in range(1, RETRIES+1):
    try:
        r = requests.post(API_URL, headers=headers, json=payload, timeout=TIMEOUT)
        meta = {
            "attempt": attempt,
            "status": r.status_code,
            "elapsed_s": r.elapsed.total_seconds(),
            "date": r.headers.get("date"),
            "x-request-id": r.headers.get("x-request-id") or r.headers.get("x-amzn-trace-id"),
            "server": r.headers.get("server"),
        }
        if r.ok:
            data = r.json()
            print("[hf-debug] success", json.dumps(meta, indent=2))
            print(json.dumps(data, indent=2))
            break
        print("[hf-debug] http_error", json.dumps({**meta, "body": r.text[:500]}, indent=2))
        # Retry only on transient statuses
        if r.status_code not in (429, 503, 504) or attempt == RETRIES:
            break
    except requests.Timeout as e:
        print("[hf-debug] timeout", {"attempt": attempt, "error": str(e)})
        if attempt == RETRIES:
            break
    time.sleep(backoff(attempt))

"""
[hf-debug] success {
  "attempt": 1,
  "status": 200,
  "elapsed_s": 5.310831,
  "date": "Thu, 06 Nov 2025 15:53:39 GMT",
  "x-request-id": "Root=1-690cc47e-339aa87e2f2b99bb582b1c8b",
  "server": "uvicorn"
}
[
  {
    "label": "refund",
    "score": 0.9196358323097229
  },
  {
    "label": "faq",
    "score": 0.06989368051290512
  },
  {
    "label": "legal",
    "score": 0.010470455512404442
  }
]
"""

```
