sagrawal1993 opened a new pull request, #14221:
URL: https://github.com/apache/apisix/pull/14221
Title: fix(ai-proxy): keep the client's key order in the upstream request
body
### Description
Since 3.18, a request body that `ai-proxy` changes before sending
(`options.model`, `stream_options` on a streamed OpenAI request, `model`
removed for Azure/Bedrock, a protocol conversion) is encoded by the transport
with `core.json.canonical_encode`, which sorts every object's keys. Providers
generate structured output in the order of a JSON Schema's `properties`, so a
schema written as `reasoning, answer` is answered as `answer, reasoning`; tool
`parameters` are reordered the same way. Only a body nothing touched keeps the
client's bytes.
This keeps the client's key order for reshaped bodies:
- **`apisix/utils/json-order.lua`** (new)
- `shape(raw)`: one pass over a JSON text, recording each object's keys in
order (and which values were arrays). Returns `nil` for anything that is not a
valid object or array, so callers fall back.
- `encode(value, shape)`: writes the current table with the original keys
first, in their order, then any new keys sorted — the same input always gives
the same bytes, so provider-side prompt caching still sees a stable prefix.
Values always come from the table; the raw bytes only supply the order. An
empty table is `[]` when it carries `core.json.array_mt` or the original had an
array there. `null`, strings, numbers (exact integers up to 2^53, otherwise the
shortest round-trip form) and the 1000-level nesting limit match `cjson`.
- `child(shape, ...)` and an optional `shapes` map for a table moved to
another path (e.g. a schema a converter relocates), so it keeps the order of
where it came from.
- **`apisix/plugins/ai-proxy/base.lua`**, Step 3: when the body was
reshaped, it is encoded with `json-order` in the order of
`core.request.get_body()` — the client's bytes, or a rewriting plugin's — and
handed to `build_request` as a string, as the passthrough path already does
(SigV4 signs a string body as is). The shape is cached on `ctx` across retries.
If encoding fails, the table goes to the transport as before. `llm_tool_count`
and `llm_end_user_id` are computed from the table rather than the encoded
string.
- `core.json.canonical_encode` is unchanged, and `ai-cache` keeps using it
for its key.
#### Which issue(s) this PR fixes:
Fixes #
### 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/utils/json-order.t`: order kept and new keys sorted, empty
arrays/objects (decoded or built in Lua), escaped keys, strings, numbers and
`null` round-trip, invalid JSON gives no shape, a moved table keeps its order,
unencodable values are an error.
- `t/plugin/ai-proxy-key-order.t`: a mock upstream records the exact bytes
it receives, one test per way Step 3 is reached —
- a body reshaped by `options.model` arrives in the client's order,
identical except for the replaced model;
- a streamed request keeps the client's keys, and `stream_options` (added
by APISIX) follows them;
- `azure-openai` (`model` removed) keeps the order of what remains;
- a body `ai-request-rewrite` replaced (`ai_request_body_changed`) is sent
in the rewritten body's order;
- an Anthropic `/v1/messages` request converted to OpenAI keeps the order
of the keys that carry over, the converter's new keys after;
- an untouched body is still forwarded byte for byte (the existing
passthrough).
On master the first five fail with every key sorted (e.g. `properties`
arrive as `alpha, mid, zeta`); the passthrough test passes on both.
Both pass locally on master with `prove` (OpenResty 1.31.8.1); `luacheck`
and `lj-releng` report nothing on the changed files.
Docs: no configuration changes; the behaviour change is that upstream bodies
keep the client's key order instead of being sorted. Happy to add a note to the
`ai-proxy` docs if you prefer.
Backward compatibility: the upstream receives the same JSON values; only the
key order of reshaped bodies differs, as for an untouched body today.
--
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]