apisix icon indicating copy to clipboard operation
apisix copied to clipboard

bug: upstream error response body is discarded for 429/5xx responses

Open RGZingYang opened this issue 5 months ago • 1 comments

Current Behavior

When the upstream LLM provider returns HTTP 429 or 5xx with a JSON error body, ai-proxy currently returns only the status code and closes the HTTP connection before reading res.body_reader.

As a result, the provider-side error details are discarded and cannot be captured by logs or observability pipelines. This is especially problematic for monitoring upstream 5xx errors, because users need the actual error body returned by the LLM provider to investigate the root cause, distinguish between provider failures, quota/rate-limit conditions, request validation errors, and other provider-specific diagnostics.

Expected Behavior

When the upstream LLM provider returns HTTP 429 or 5xx with an error response body, ai-proxy should read the body up to a configurable maximum size before closing the connection. The captured body should either be exposed through logging/observability fields such as llm_response_text, or be preserved and returned to the client together with the upstream status code, while respecting response size limits and the upstream Content-Type.

Error Logs

No response

Steps to Reproduce

local res, err = httpc:request(params)
 if not res then
     core.log.warn("failed to send request to LLM server: ", err)
     return handle_error(err)
 end

 -- handling this error separately is needed for retries
 if res.status == 429 or (res.status >= 500 and res.status < 600 )then
     return res.status
 end

 local code, body = read_response(extra_opts.conf, ctx, res, self.response_filter)

 if conf.keepalive then
     local ok, err = httpc:set_keepalive(conf.keepalive_timeout, conf.keepalive_pool)
     if not ok then
         core.log.warn("failed to keepalive connection: ", err)
     end
 end

Environment

  • APISIX version (run apisix version):
  • Operating system (run uname -a):
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):

RGZingYang avatar Jun 10 '26 07:06 RGZingYang

Hi, @Baoyuantop ,I think the key branch is in apisix/plugins/ai-proxy/base.lua: after transport_http.request() returns, ai-proxy handles retryable responses with an early return:

if res.status == 429 or (res.status >= 500 and res.status < 600) then
    if res._httpc then
        res._httpc:close()
    end
    return res.status
end

That happens before ai_provider:parse_response(...), so res.body_reader is never consumed for 429/5xx. As a result, the upstream JSON error payload cannot be returned to the client or captured by the normal response/logging path.

hanzhenfang avatar Jun 15 '26 07:06 hanzhenfang