"Retrieve file content" API always fails with "502 Bad gateway"

I’ve previously successfully completed a few batch jobs of ~50k embeddings and I am trying to download the resulting files (10 batches with ~2GB files as results), but all of the files fail to download through the /files/{file_id}/content endpoint (https://platform.openai.com/docs/api-reference/files/retrieve-contents).

Smaller files work (like any “error” file of the batches), but the ~2GB batch output themselves always fail. All I get when trying to fetch those is 20 seconds of nothing, then a Cloudflare error page (502 Bad gateway).

I’ve confirmed it’s not an issue with my connection. It has the same behaviour on multiple machines colocated all over Europe and US. OpenAI Support claims there are no current outages, and send me for an endless loop trying to provide support for ChatGPT…

Steps to repro (using openai-node, but I get the same result using curl exactly like the example in the docs):

// batch_686cfa016d188190b4c59f087634a269_output.jsonl (1841.05 MB)
const content = await openai.files.content('file-K6cKA4LNQy6LLoQKamSFhW');

More file-id’s that fail: file-2k1YfYkzYDKpEFip8pBbjA (2GB), file-2uudc3nffig6QChLSVY5nD (2GB), file-AXQ471Yymu5J1cwMUighTJ (~1GB)
A smaller file from a different batch that works fine: file-Dy444dJcxaw3XzmXbc8tXR (45MB)

Result:

node_modules/openai/src/error.ts:98
      return new InternalServerError(status, error, message, headers);
             ^


InternalServerError: 502 <!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>


<title>api.openai.com | 502: Bad gateway</title>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" id="cf_styles-css" href="/cdn-cgi/styles/main.css" />


</head>
<body>
<div id="cf-wrapper">
    <div id="cf-error-details" class="p-0">
        <header class="mx-auto pt-10 lg:pt-6 lg:px-8 w-240 lg:w-full mb-8">
            <h1 class="inline-block sm:block sm:mb-2 font-light text-60 lg:text-4xl text-black-dark leading-tight mr-2">
              <span class="inline-block">Bad gateway</span>
              <span class="code-label">Error code 502</span>
            </h1>
            <div>
               Visit <a href="https://www.cloudflare.com/5xx-error-landing?utm_source=errorcode_502&utm_campaign=api.openai.com" target="_blank" rel="noopener noreferrer">cloudflare.com</a> for more information.
            </div>
            <div class="mt-3">2025-07-09 16:11:00 UTC</div>
        </header>
        <div class="my-8 bg-gradient-gray">
            <div class="w-240 lg:w-full mx-auto">
                <div class="clearfix md:px-8">
                  
<div id="cf-browser-status" class=" relative w-1/3 md:w-full py-15 md:p-0 md:py-8 md:text-left md:border-solid md:border-0 md:border-b md:border-gray-400 overflow-hidden float-left md:float-none text-center">
  <div class="relative mb-10 md:m-0">
    
    <span class="cf-icon-browser block md:hidden h-20 bg-center bg-no-repeat"></span>
    <span class="cf-icon-ok w-12 h-12 absolute left-1/2 md:left-auto md:right-0 md:top-0 -ml-6 -bottom-4"></span>
    
  </div>
  <span class="md:block w-full truncate">You</span>
  <h3 class="md:inline-block mt-3 md:mt-0 text-2xl text-gray-600 font-light leading-1.3">
    
    Browser
    
  </h3>
  <span class="leading-1.3 text-2xl text-green-success">Working</span>
</div>

<div id="cf-cloudflare-status" class=" relative w-1/3 md:w-full py-15 md:p-0 md:py-8 md:text-left md:border-solid md:border-0 md:border-b md:border-gray-400 overflow-hidden float-left md:float-none text-center">
  <div class="relative mb-10 md:m-0">
    <a href="https://www.cloudflare.com/5xx-error-landing?utm_source=errorcode_502&utm_campaign=api.openai.com" target="_blank" rel="noopener noreferrer">
    <span class="cf-icon-cloud block md:hidden h-20 bg-center bg-no-repeat"></span>
    <span class="cf-icon-ok w-12 h-12 absolute left-1/2 md:left-auto md:right-0 md:top-0 -ml-6 -bottom-4"></span>
    </a>
  </div>
  <span class="md:block w-full truncate">Stockholm</span>
  <h3 class="md:inline-block mt-3 md:mt-0 text-2xl text-gray-600 font-light leading-1.3">
    <a href="https://www.cloudflare.com/5xx-error-landing?utm_source=errorcode_502&utm_campaign=api.openai.com" target="_blank" rel="noopener noreferrer">
    Cloudflare
    </a>
  </h3>
  <span class="leading-1.3 text-2xl text-green-success">Working</span>
</div>

<div id="cf-host-status" class="cf-error-source relative w-1/3 md:w-full py-15 md:p-0 md:py-8 md:text-left md:border-solid md:border-0 md:border-b md:border-gray-400 overflow-hidden float-left md:float-none text-center">
  <div class="relative mb-10 md:m-0">
    
    <span class="cf-icon-server block md:hidden h-20 bg-center bg-no-repeat"></span>
    <span class="cf-icon-error w-12 h-12 absolute left-1/2 md:left-auto md:right-0 md:top-0 -ml-6 -bottom-4"></span>
    
  </div>
  <span class="md:block w-full truncate">api.openai.com</span>
  <h3 class="md:inline-block mt-3 md:mt-0 text-2xl text-gray-600 font-light leading-1.3">
    
    Host
    
  </h3>
  <span class="leading-1.3 text-2xl text-red-error">Error</span>
</div>

                </div>
            </div>
        </div>

        <div class="w-240 lg:w-full mx-auto mb-8 lg:px-8">
            <div class="clearfix">
                <div class="w-1/2 md:w-full float-left pr-6 md:pb-10 md:pr-0 leading-relaxed">
                    <h2 class="text-3xl font-normal leading-1.3 mb-4">What happened?</h2>
                    <p>The web server reported a bad gateway error.</p>
                </div>
                <div class="w-1/2 md:w-full float-left leading-relaxed">
                    <h2 class="text-3xl font-normal leading-1.3 mb-4">What can I do?</h2>
                    <p class="mb-6">Please try again in a few minutes.</p>
                </div>
            </div>
        </div>

        <div class="cf-error-footer cf-wrapper w-240 lg:w-full py-10 sm:py-4 sm:px-8 mx-auto text-center sm:text-left border-solid border-0 border-t border-gray-300">
  <p class="text-13">
    <span class="cf-footer-item sm:block sm:mb-1">Cloudflare Ray ID: <strong class="font-semibold">95c917514af9a68a</strong></span>
    <span class="cf-footer-separator sm:hidden">&bull;</span>
    <span id="cf-footer-item-ip" class="cf-footer-item hidden sm:block sm:mb-1">
      Your IP:
      <button type="button" id="cf-footer-ip-reveal" class="cf-footer-ip-reveal-btn">Click to reveal</button>
      <span class="hidden" id="cf-footer-ip">37.123.153.252</span>
      <span class="cf-footer-separator sm:hidden">&bull;</span>
    </span>
    <span class="cf-footer-item sm:block sm:mb-1"><span>Performance &amp; security by</span> <a rel="noopener noreferrer" href="https://www.cloudflare.com/5xx-error-landing?utm_source=errorcode_502&utm_campaign=api.openai.com" id="brand_link" target="_blank">Cloudflare</a></span>
    
  </p>
  <script>(function(){function d(){var b=a.getElementById("cf-footer-item-ip"),c=a.getElementById("cf-footer-ip-reveal");b&&"classList"in b&&(b.classList.remove("hidden"),c.addEventListener("click",function(){c.classList.add("hidden");a.getElementById("cf-footer-ip").classList.remove("hidden")}))}var a=document;document.addEventListener&&a.addEventListener("DOMContentLoaded",d)})();</script>
</div><!-- /.error-footer -->


    </div>
</div>
</body>
</html>

    at Function.generate (node_modules/openai/src/error.ts:98:14)
    at OpenAI.makeStatusError (node_modules/openai/src/core.ts:435:21)
    at OpenAI.makeRequest (node_modules/openai/src/core.ts:499:24)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async fetchAndInsertBatches (api/vector/knowledge/advertisers/advertisersIndex.mts:148:19)
    at async <anonymous> (api/vector/knowledge/advertisers/advertisersIndex.mts:225:2) {
  status: 502,
  headers: {
    'alt-svc': 'h3=":443"; ma=86400',
    'cache-control': 'private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
    'cf-ray': '95c917514af9a68a-ARN',
    connection: 'keep-alive',
    'content-length': '6327',
    'content-type': 'text/html; charset=UTF-8',
    date: 'Wed, 09 Jul 2025 16:11:00 GMT',
    expires: 'Thu, 01 Jan 1970 00:00:01 GMT',
    'referrer-policy': 'same-origin',
    server: 'cloudflare',
    'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
    'x-content-type-options': 'nosniff',
    'x-frame-options': 'SAMEORIGIN'
  },
  request_id: undefined,
  error: undefined,
  code: undefined,
  param: undefined,
  type: undefined
}

Node.js v23.11.0
3 Likes

I also encountered exact same issue…

Please check the urls in the configs of your clients. Never got this error beside a mistyped urls…

I’m also having the same issue with big files. ~2GB fails but a ~450MB output file works fine.
Im using the OpenAI Python library.

I also faced similar issue multiple time , especially for big files more than 1GB

Your 502 error is not coming from the OpenAI API itself, but from the Cloudflare gateway that fired the request before it arrived. This is typical when transferring larger outputs (tens of MB+), or when streaming limits are not respected. I recommend:

  1. Reduce batches to units or tens of items

  2. Ensure that results do not exceed units of MB

  3. Use asynchronous export or redirection to your own S3 endpoint if you really need 2GB results

OpenAI API is not a streaming engine - it is not built to bulk transfer 2GB JSON. Above 100MB you are in the danger zone, above 1GB in the suicide zone.

1 Like

I encounter the same problems, a few months ago this worked without problems.

I am receiving “502 Bad Gateway” or “504 Gateway Timeout” erros when trying to download the output files created by batch jobs via the Files api endpoint.

The batches are in a completed state and not expired. The output file is processed, exists and also not expired.

Steps I tried to resolve this issue:

  • Downloading via curl returns the same error codes.

  • Different batches from different dates are affected.

  • Different batch sizes are affected 1-3GB.

  • As far as I can tell, all my new batches are affected.

  • My internet connection is stable (no packet loss and low latency).

  • I also tried from different networks.

  • Interestingly, downloading manually via the web-dashboard works, which I currently use as a workaround.

Example code, output indicated with #:

batch = client.batches.retrieve("batch_689b77a3e38c8190b59fac55de0e89b6")
# Batch(id='batch_689b77a3e38c8190b59fac55de0e89b6', completion_window='24h', created_at=1755019171, endpoint='/v1/chat/completions', input_file_id='file-BvGpWKhkXDmYuZEngpH7MF', object='batch', status='completed', cancelled_at=None, cancelling_at=None, completed_at=1755020917, error_file_id=None, errors=None, expired_at=None, expires_at=1755105571, failed_at=None, finalizing_at=1755020516, in_progress_at=1755019175, metadata={'description': 'batch_1755018965_5_7030_test_CoT_prompt_None_None'}, output_file_id='file-YbAi7cxvJn5dwS2RbK4s5g', request_counts=BatchRequestCounts(completed=6942, failed=0, total=6942))
file_object = client.files.retrieve(batch.output_file_id)
# FileObject(id='file-YbAi7cxvJn5dwS2RbK4s5g', bytes=3575579066, created_at=1755020879, filename='batch_689b77a3e38c8190b59fac55de0e89b6_output.jsonl', object='file', purpose='batch_output', status='processed', status_details=None, expires_at=1757612879)
file_content = client.files.content(batch.output_file_id)
#
raise self._make_status_error_from_response(err.response) from None
openai.InternalServerError: <!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<title>api.openai.com | 502: Bad gateway</title>
....