Skip to content

fix(ai-proxy): populate $upstream_response_length for cosocket transport#13323

Merged
nic-6443 merged 1 commit into
apache:masterfrom
nic-6443:fix/ai-proxy-upstream-response-length
Apr 30, 2026
Merged

fix(ai-proxy): populate $upstream_response_length for cosocket transport#13323
nic-6443 merged 1 commit into
apache:masterfrom
nic-6443:fix/ai-proxy-upstream-response-length

Conversation

@nic-6443

Copy link
Copy Markdown
Member

The previous commit (bda084d) added upstream nginx variable support for ai-proxy cosocket transport but missed passing response_length to update_upstream_state. This caused $upstream_response_length to always be 0 in access logs.

This tracks total bytes read in both parse_response and parse_streaming_response, then passes the value as response_length to update_upstream_state after body consumption is complete.

Changes:

  • ai-providers/base.lua: store res._upstream_bytes in all return paths of both parse methods
  • ai-proxy/base.lua: pass response_length = res._upstream_bytes or 0 in the final update_upstream_state call
  • Added tests (TEST 5-7 in ai-proxy-upstream-vars.t) verifying non-zero $upstream_response_length for both streaming and non-streaming
  • Updated docs to include upstream_response_length in the supported variables list

Copilot AI review requested due to automatic review settings April 30, 2026 08:11
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Apr 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes missing population of Nginx upstream variable $upstream_response_length when ai-proxy uses the cosocket transport by tracking bytes read from upstream responses and forwarding that to the upstream-state bookkeeping used for logging.

Changes:

  • Track total upstream response body bytes in parse_response and parse_streaming_response via res._upstream_bytes.
  • Pass response_length into the final update_upstream_state() call in ai-proxy after the body has been consumed.
  • Add tests to assert $upstream_response_length is non-zero for streaming and non-streaming requests; document the variable as supported.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
apisix/plugins/ai-providers/base.lua Tracks upstream body bytes for non-streaming and streaming parsing paths.
apisix/plugins/ai-proxy/base.lua Forwards tracked byte count into upstream-state (response_length) for log vars.
t/plugin/ai-proxy-upstream-vars.t Adds regression tests asserting $upstream_response_length becomes non-zero.
docs/en/latest/plugins/ai-proxy.md Documents upstream_response_length as a populated upstream variable.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apisix/plugins/ai-providers/base.lua
Comment thread apisix/plugins/ai-providers/base.lua
@nic-6443
nic-6443 force-pushed the fix/ai-proxy-upstream-response-length branch from e14442d to e54e3a5 Compare April 30, 2026 08:18
The previous commit (bda084d) added support for upstream nginx
variables when ai-proxy uses cosocket transport, but missed passing
response_length to update_upstream_state. This caused
$upstream_response_length to always be 0 in access logs.

Track total bytes read in both parse_response and
parse_streaming_response, then pass the value as response_length
to update_upstream_state after body consumption is complete.
@nic-6443
nic-6443 force-pushed the fix/ai-proxy-upstream-response-length branch from e54e3a5 to 3d810d0 Compare April 30, 2026 08:19
@nic-6443
nic-6443 merged commit 17b9b56 into apache:master Apr 30, 2026
27 of 29 checks passed
@nic-6443
nic-6443 deleted the fix/ai-proxy-upstream-response-length branch April 30, 2026 10:57
wistefan pushed a commit to wistefan/apisix that referenced this pull request Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants