Skip to content

fix(ai-cache): key the passthrough protocol on the client method, path and query - #13887

Open
shreemaan-abhishek wants to merge 2 commits into
apache:masterfrom
shreemaan-abhishek:fix/ai-cache-passthrough-key
Open

fix(ai-cache): key the passthrough protocol on the client method, path and query#13887
shreemaan-abhishek wants to merge 2 commits into
apache:masterfrom
shreemaan-abhishek:fix/ai-cache-passthrough-key

Conversation

@shreemaan-abhishek

Copy link
Copy Markdown
Contributor

Description

For the passthrough protocol, ai-proxy forwards the client's request method, path and query string verbatim (ai-proxy/base.lua), so on a wildcard route they select which upstream endpoint answers. The ai-cache fingerprint covered only the client body and the instance config, so the same body sent to /v1/images/generations and /v1/chat/completions (or to one path with different query parameters) collided on one cache entry and the second request was served the first one's response.

This folds the client method, uri and args into build_repr under a passthrough gate. Other protocols build a fixed upstream request from the body alone, so their fingerprints are unchanged; an existing entry keeps its key.

Tests: a key.lua unit block (path, query and method each flip the passthrough fingerprint; the openai-chat fingerprint still ignores the client path) plus an end-to-end sequence on a /v1/* route: same body to two upstream paths is MISS/MISS, a query-string variant is a MISS, and an exact repeat is a HIT. Without the fix TESTs 58, 61 and 62 fail.

Docs: the cache_key note in ai-cache.md (en/zh) now states that passthrough keys include the method, path and query.

Which issue(s) this PR fixes:

Fixes #

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

…h and query

For the passthrough protocol ai-proxy forwards the client's request method,
path and query string verbatim, so they select the upstream endpoint. The
cache fingerprint only covered the body and instance config, so the same body
sent to two different upstream paths (or with different query parameters)
shared one cache entry. Fold the three into the fingerprint under the
passthrough gate; other protocols are unchanged.
ctx.var.request_method is cached at route match, before a proxy-rewrite
method change, while ai-proxy forwards the live method. Use
core.request.get_method() so the fingerprint matches the upstream request,
and cover a proxy-rewrite method rewrite in the passthrough cache tests.
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Aug 27, 2026

@membphis membphis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants