Skip to content

perf(ai): reuse raw request body when unchanged#13406

Merged
nic-6443 merged 1 commit into
apache:masterfrom
nic-6443:ai-proxy-raw-body-apisix-20260521
May 21, 2026
Merged

perf(ai): reuse raw request body when unchanged#13406
nic-6443 merged 1 commit into
apache:masterfrom
nic-6443:ai-proxy-raw-body-apisix-20260521

Conversation

@nic-6443

Copy link
Copy Markdown
Member

This avoids re-encoding the ai-proxy request body when the outgoing body is unchanged. If protocol conversion, provider preparation, model options, request body overrides, or provider cleanup changes the body, the existing encoded table path is still used.

It also covers ai-proxy-multi through the shared provider build path and keeps retry behavior from falling back to the original raw body after an earlier attempt has rewritten the parsed body. The raw body is read lazily in the unchanged-body branch and cached on the request context, so retries do not read it again.

The sidecar LLM request path used by ai-request-rewrite is explicitly marked as generated, so it continues to send the JSON-encoded LLM request instead of reusing the original client body.

Tests run:

luacheck apisix/plugins/ai-proxy/base.lua apisix/plugins/ai-providers/base.lua apisix/plugins/ai-protocols/openai-chat.lua apisix/plugins/ai-protocols/bedrock-converse.lua apisix/plugins/ai-protocols/passthrough.lua apisix/plugins/ai-request-rewrite.lua
perl -I. -c t/plugin/ai-proxy-request-body-override.t
git diff --check
prove -I. t/plugin/ai-proxy-request-body-override.t

Copilot AI review requested due to automatic review settings May 21, 2026 06:06
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. performance generate flamegraph for the current PR labels May 21, 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

This PR optimizes the AI proxy request pipeline by reusing the original raw client request body when the outgoing body is unchanged, avoiding unnecessary JSON re-encoding and preventing retry attempts from accidentally reverting to the original raw body after a prior rewrite.

Changes:

  • Track whether the request body was modified during provider request building, and reuse the original raw body when unchanged.
  • Update AI protocol adapters’ prepare_outgoing_request to return a boolean indicating whether they mutated the body.
  • Add tests to ensure ai-proxy / ai-proxy-multi forward the original body when not rewritten, and that retries don’t reuse raw after a rewrite.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
t/plugin/ai-proxy-request-body-override.t Adds coverage for the unchanged-body forward path and retry behavior.
apisix/plugins/ai-request-rewrite.lua Marks sidecar-generated requests as “body changed” to prevent raw-body reuse.
apisix/plugins/ai-providers/base.lua Implements change tracking and raw-body reuse/caching logic in build_request.
apisix/plugins/ai-protocols/passthrough.lua Makes prepare_outgoing_request explicitly report “no mutation”.
apisix/plugins/ai-protocols/openai-chat.lua Returns whether stream-related injection mutated the body.
apisix/plugins/ai-protocols/bedrock-converse.lua Returns whether stripping the stream field mutated the body.

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




=== TEST 4b: ai-proxy-multi forwards the original body when it is not rewritten

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think lint will fail here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

strange that lint didn't fail here, I remember doing something similar and lint failed, lol.

@AlinsRan AlinsRan May 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

===TEST 4a
...
===TEST 4b 

At this point, Lint will fail.

@nic-6443
nic-6443 merged commit 6dd6a34 into apache:master May 21, 2026
28 checks passed
@nic-6443
nic-6443 deleted the ai-proxy-raw-body-apisix-20260521 branch May 21, 2026 08:19
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

performance generate flamegraph for the current PR size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants