Symptoms
A batch script (typically Windows + Pythonrequests) intermittently receives 502 responses that print roughly like this:
- The response body is empty, and the only headers are
ConnectionandContent-Length - The web console keeps working the whole time
- The failed requests do not appear at all in your 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
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.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:
If a 502 has no
Server header, no Date header, and an empty body, it almost certainly came from proxy software, not from APIYI.
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.
Why the Proxy Gets Involved
- Python
requestspicks 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. - 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. - 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.
Fix
1
Check whether your script uses a proxy
Run this in the same environment as your script:If the output contains an
http or https entry (for example 127.0.0.1:7890), requests routes through that proxy by default.2
Bypass the proxy (pick one)
- requests
- OpenAI SDK
- Environment variable
- Proxy software
requests.post(..., proxies={"http": None, "https": None}).3
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?
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?). You can keep using it, as long as you bypass the proxy as described above.FAQ
Am I charged for these failed requests?
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.
Why does it succeed after a few retries?
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.
Can APIYI fix this on the server side?
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.
I still get 502 after bypassing the proxy. What now?
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. If it keeps happening, send support the failure time (with time zone, for example 14:30 (UTC+8)) and the full response.Related Docs
Website or API Returns 502 — What Should I Do?
Platform-side 502: brief container restart, recovers in about a minute
Do I Need a Proxy to Use the API?
Direct connection works; no proxy or VPN needed
How Can I Reduce Image API Latency?
HTTP endpoint, connection reuse, and timeout settings
How do I avoid API timeouts?
Timeout settings and layer-by-layer troubleshooting