> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apiyi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Script Gets 502 but Nothing in the Call Logs?

> A 502 with an empty body and only Connection: close and Content-Length: 0 headers comes from the proxy software on your machine, not from APIYI. Make your script bypass the system proxy to fix it.

## Symptoms

A batch script (typically Windows + Python `requests`) intermittently receives 502 responses that print roughly like this:

```text theme={null}
HTTP 502: {'_non_json_response': '', '_status_code': 502,
           '_headers': {'Connection': 'close', 'Content-Length': '0'}}
```

You also notice the following:

* The response **body is empty**, and the **only** headers are `Connection` and `Content-Length`
* The web console keeps working the whole time
* The failed requests **do not appear at all** in your [call logs](/en/faq/call-logs)
* The script retries on 502, often failing several times in a row before one succeeds, with the backoff getting longer each time

## Short Answer

<Info>
  **This 502 does not come from APIYI. It is generated by the proxy software on your machine (Clash, v2rayN, and similar tools).**

  Make your script bypass the system proxy to fix it. With `requests`, set `session.trust_env = False`. Other options are covered below.
</Info>

## How to Tell Whether This Is Your Case

Every APIYI layer returns responses with recognizable traits, and none of them match the empty 502 above:

| Source                                | Response traits                                                                                       |
| ------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| APIYI access nodes (nginx)            | Always include `Server` and `Date` headers; 5xx errors generated by the nodes also carry an HTML body |
| APIYI gateway                         | JSON error body with `error.message`                                                                  |
| Proxy software that failed to forward | Empty body, usually with only `Connection: close` and `Content-Length: 0`                             |

If a 502 has **no `Server` header, no `Date` header, and an empty body**, it almost certainly came from proxy software, not from APIYI.

<Note>
  The platform can also return a real 502 when a service container briefly restarts. In that case the web console goes down at the same time, service recovers within about a minute, and the response carries full headers and a body. See [Website or API Returns 502](/en/faq/website-502-error).
</Note>

## Why the Proxy Gets Involved

1. **Python `requests` picks up the system proxy automatically.** On Windows it reads the system proxy settings from the registry. Once Clash, v2rayN, or a similar tool enables "system proxy", your script silently routes through it, and nothing in your code shows it.
2. **The proxy forwards plain-HTTP requests itself.** With a plain-HTTP address such as `http://api.apiyi.com:16888`, the proxy doesn't just open a transparent tunnel; it re-sends the request on your behalf. When a proxy node stalls, switches, or times out, the proxy answers your script with an empty 502.
3. **Large request bodies and high concurrency make it worse.** An image edit uploads several MB of image data, and a single generation takes 45 to 70 seconds. With dozens of concurrent requests sharing one proxy node, any hiccup fails a whole batch at once.

<Tip>
  With `https://`, the proxy only opens an encrypted tunnel, so failures usually surface as connection errors (such as `ProxyError`) rather than a fabricated 502. HTTPS traffic still goes through the proxy, though, so the real fix is still to keep your script off the proxy.
</Tip>

## Fix

<Steps>
  <Step title="Check whether your script uses a proxy">
    Run this in the same environment as your script:

    ```python theme={null}
    import urllib.request
    print(urllib.request.getproxies())
    ```

    If the output contains an `http` or `https` entry (for example `127.0.0.1:7890`), `requests` routes through that proxy by default.
  </Step>

  <Step title="Bypass the proxy (pick one)">
    <Tabs>
      <Tab title="requests">
        ```python theme={null}
        import requests

        session = requests.Session()
        session.trust_env = False   # ignore system and environment proxy settings

        resp = session.post(
            "https://api.apiyi.com/v1/images/edits",
            headers={"Authorization": "Bearer YOUR_API_KEY"},
            data={"model": "gpt-image-2-vip", "prompt": "...", "size": "1024x1536"},
            files=[("image[]", open("a.jpg", "rb"))],
            timeout=(10, 600),
        )
        ```

        To change a single request only: `requests.post(..., proxies={"http": None, "https": None})`.
      </Tab>

      <Tab title="OpenAI SDK">
        ```python theme={null}
        import httpx
        from openai import OpenAI

        client = OpenAI(
            api_key="YOUR_API_KEY",
            base_url="https://api.apiyi.com/v1",
            http_client=httpx.Client(trust_env=False, timeout=600),
        )
        ```
      </Tab>

      <Tab title="Environment variable">
        If you'd rather not change code, set `NO_PROXY` before running the script so APIYI domains skip the proxy:

        ```bash theme={null}
        # Windows PowerShell
        $env:NO_PROXY="api.apiyi.com,.apiyi.com"

        # macOS / Linux
        export NO_PROXY="api.apiyi.com,.apiyi.com"
        ```
      </Tab>

      <Tab title="Proxy software">
        In Clash, v2rayN, or similar tools, add a DIRECT rule for `apiyi.com`, or turn off "system proxy" while batch jobs run.
      </Tab>
    </Tabs>
  </Step>

  <Step title="Verify with a small batch">
    Run a few dozen requests at 5 to 10 concurrent workers. If the empty 502s are gone, the proxy was the cause. APIYI is reachable directly and doesn't require a proxy; see [Do I Need a Proxy to Use the API?](/en/faq/network-proxy)
  </Step>
</Steps>

<Note>
  `http://api.apiyi.com:16888` is an officially provided plain-HTTP endpoint that lowers latency for image workloads (see [How Can I Reduce Image API Latency?](/en/faq/image-api-network-latency-optimization)). **You can keep using it**, as long as you bypass the proxy as described above.
</Note>

## FAQ

<AccordionGroup>
  <Accordion title="Am I charged for these failed requests?">
    It depends on when the proxy failed:

    * If the proxy failed **before the request reached APIYI**, APIYI never received it: no charge and no call log entry.
    * If the proxy dropped the connection **after the request was sent, while waiting for the result**, APIYI may already be generating the image and bills it as usual, but the result never reaches your script.

    So don't rely on your script's error output alone. Check the actual number of charged calls in your [call logs](/en/faq/call-logs).
  </Accordion>

  <Accordion title="Why does it succeed after a few retries?">
    Proxy node hiccups are usually intermittent, so a retry that lands after the node recovers goes through. But each retry uploads the full image again, and backoff grows each time, so batch jobs take much longer overall. Bypassing the proxy is the real fix.
  </Accordion>

  <Accordion title="Can APIYI fix this on the server side?">
    No. The failure happens in the proxy between your machine and APIYI. Either the request never reaches APIYI or the proxy closes the connection itself, so there is nothing the server can do.
  </Accordion>

  <Accordion title="I still get 502 after bypassing the proxy. What now?">
    Check the headers first. If the response includes `Server` and `Date` and has an HTML or JSON body, it is a platform-side 502; see [Website or API Returns 502](/en/faq/website-502-error). If it keeps happening, send support the failure time (with time zone, for example `14:30 (UTC+8)`) and the full response.
  </Accordion>
</AccordionGroup>

## Related Docs

<CardGroup cols={2}>
  <Card title="Website or API Returns 502 — What Should I Do?" icon="refresh-cw" href="/en/faq/website-502-error">
    Platform-side 502: brief container restart, recovers in about a minute
  </Card>

  <Card title="Do I Need a Proxy to Use the API?" icon="wifi" href="/en/faq/network-proxy">
    Direct connection works; no proxy or VPN needed
  </Card>

  <Card title="How Can I Reduce Image API Latency?" icon="gauge" href="/en/faq/image-api-network-latency-optimization">
    HTTP endpoint, connection reuse, and timeout settings
  </Card>

  <Card title="How do I avoid API timeouts?" icon="timer" href="/en/faq/timeout-configuration">
    Timeout settings and layer-by-layer troubleshooting
  </Card>
</CardGroup>
