dhivakar020 opened a new pull request, #14153:
URL: https://github.com/apache/apisix/pull/14153

   ### Description
   
   When a request falls through to the `passthrough` protocol (a body that is 
not OpenAI chat/embeddings/responses, Anthropic or Bedrock) and 
`override.endpoint` has no path, `ai-proxy` builds the upstream path from 
`ctx.var.uri` (`apisix/plugins/ai-proxy/base.lua`).
   
   `ctx.var.uri` is read and cached when the route is matched, before any 
plugin runs. When `proxy-rewrite` changes the path, it calls `ngx.req.set_uri` 
and sets `$upstream_uri`, but the cached `ctx.var.uri` keeps the original path. 
So `ai-proxy` sends the **original client path** to the LLM upstream, and the 
rewrite is silently ignored. Header rewrites from `proxy-rewrite` do apply, and 
the `ai-proxy` docs suggest combining the two plugins, so a path rewrite being 
dropped is surprising.
   
   Reproduction (`override.endpoint: https://httpbin.org`, `proxy-rewrite` 
`regex_uri: ["^/anything/original/(.*)", "/anything/rewritten/$1"]`, `POST 
/anything/original/x` with body `{"foo":"bar"}`):
   
   | | path received by the upstream |
   |---|---|
   | expected | `/anything/rewritten/x` |
   | before this PR | `/anything/original/x` |
   | after this PR | `/anything/rewritten/x` |
   
   This PR makes passthrough use the path part of `$upstream_uri` when it is 
set, the same path a plain `proxy_pass` would send, and falls back to 
`ctx.var.uri` otherwise. The query string is still forwarded separately from 
the client's args, as before.
   
   Side effect with `normalize_uri_like_servlet: true`: `$upstream_uri` holds 
the original path, so passthrough now forwards `/a;jsessionid=1/b` like a plain 
route does (#13914), instead of the normalized `/a/b`. Checked locally with an 
`ai-proxy` route and a plain route side by side; both now send the same request 
URI.
   
   Core already works around the same cached value in `proxy-mirror` (`-- read 
the var directly as ctx.var.uri may be cached before the rewrite`).
   
   #### Which issue(s) this PR fixes:
   
   Fixes #14129
   
   ### Checklist
   
   - [x] I have explained the need for this PR and the problem it solves
   - [x] I have explained the changes or the new features added to this PR
   - [x] I have added tests corresponding to this change
   - [ ] I have updated the documentation to reflect this change
   - [x] I have verified that this change is backward compatible (If not, 
please discuss on the [APISIX mailing 
list](https://github.com/apache/apisix/tree/master#community) first)
   
   Tests: `t/plugin/ai-proxy-passthrough.t` TEST 18/19 (route with 
`proxy-rewrite` before `ai-proxy`, upstream must receive the rewritten path and 
the client query). `ai-proxy-passthrough.t`, `ai-proxy-anthropic.t` and 
`ai-proxy-multi3.t` pass locally.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to