Skip to content

feat(ai): support configurable request JSON library#13386

Open
nic-6443 wants to merge 41 commits into
apache:masterfrom
nic-6443:ai-json-lib-20260519-apisix
Open

feat(ai): support configurable request JSON library#13386
nic-6443 wants to merge 41 commits into
apache:masterfrom
nic-6443:ai-json-lib-20260519-apisix

Conversation

@nic-6443
Copy link
Copy Markdown
Member

@nic-6443 nic-6443 commented May 19, 2026

This adds an APISIX-level JSON library selector for request body parsing used by core.request.get_request_body_table, and reuses the same selector for AI upstream request body encoding.

The supported values are cjson, simdjson, and qjson. The default is simdjson. qjson is available as an experimental option for users who explicitly opt in after evaluating compatibility. The config schema owns the enum/default validation, and request_json caches the validated value from local_conf. The selected decode/encode behavior is fixed by the configured value: cjson uses cjson decode and encode, simdjson uses simdjson decode and cjson encode, and qjson uses qjson decode and encode. qjson decode results are materialized before returning from core.request so existing table consumers such as JSONPath matching can keep using normal Lua table traversal.

lua-qjson and api7-lua-resty-simdjson are installed through LuaRocks dependencies. The simdjson package keeps cjson array metadata for decoded arrays, so empty arrays still round-trip as arrays when encoded by cjson. The Rust toolchain bootstrap logic is shared by CI and install-dependencies to keep the minimum rustc/rustup setup in one place. Docker build paths that run make deps also install or carry the Rust/helper prerequisites needed to build simdjson.

Docs include benchmark data for cjson, simdjson, and qjson on large AI request bodies.

Tests added/updated:

  • t/core/request.t covers selector behavior, qjson materialization, simdjson empty-array round-trip behavior, and AI transport encoding
  • t/core/config-default.t covers the default value

Checks run:

  • luacheck apisix/core/request_json.lua apisix/core/request.lua apisix/plugins/ai-transport/http.lua apisix/plugins/ai-transport/auth-aws.lua
  • perl -I. -c t/core/request.t
  • bash -n ci/common.sh utils/install-dependencies.sh utils/install-rust-toolchain.sh ci/linux_apisix_current_luarocks_runner.sh
  • git diff --check
  • request_json qjson smoke test via resty
  • qjson LuaRocks install smoke test
  • lua-qjson LuaRocks install with Makefile Rust/Cargo home selection
  • api7-lua-resty-simdjson LuaRocks install smoke test
  • request_json simdjson empty-array round-trip smoke test via resty

Copilot AI review requested due to automatic review settings May 19, 2026 02:05
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels May 19, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds an APISIX-level setting apisix.request_body_json_lib (default qjson, options cjson/simdjson/qjson) controlling the JSON library used by core.request.get_request_body_table and by AI transport upstream body encoding. Introduces a new module apisix.core.request_json that lazily loads the selected library, plus rockspec dependencies on lua-qjson and lua-resty-simdjson.

Changes:

  • New module apisix/core/request_json.lua selecting between cjson/qjson/simdjson based on local config, wired into apisix/core/request.lua and AI transport encoders (ai-transport/http.lua, ai-transport/auth-aws.lua).
  • Schema + default config additions for request_body_json_lib, and new rockspec deps lua-qjson and lua-resty-simdjson.
  • Docs (en/zh) and tests covering selector behavior, AI encoder routing, and the new default in config-default.t.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
apisix/core/request_json.lua New selector module with lazy load of qjson/simdjson and cjson fallback.
apisix/core/request.lua Replaces core.json.decode with request_json.decode for request body parsing.
apisix/plugins/ai-transport/http.lua Switches upstream body encode to request_json.encode.
apisix/plugins/ai-transport/auth-aws.lua Switches SigV4 body encode to request_json.encode; drops unused core import.
apisix/cli/schema.lua Adds enum schema for request_body_json_lib.
apisix/cli/config.lua Adds default request_body_json_lib = "qjson".
conf/config.yaml.example Documents new option in example config.
apisix-master-0.rockspec Adds lua-qjson and lua-resty-simdjson dependencies.
docs/en/latest/plugins/ai-proxy.md English docs for the new option.
docs/zh/latest/plugins/ai-proxy.md Chinese docs for the new option.
t/core/request.t Adds tests for selector behavior and AI transport encoder routing.
t/core/config-default.t Adds assertion on the new default value.

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

Comment thread apisix/core/request_json.lua Outdated
Comment thread apisix/core/request_json.lua Outdated
Comment thread apisix/core/request_json.lua Outdated
Comment thread docs/en/latest/plugins/ai-proxy.md Outdated
Comment thread apisix/core/request_json.lua
Comment thread apisix/core/request_json.lua Outdated
Comment thread t/core/request.t Outdated
Comment thread t/core/request.t
Comment thread apisix/cli/config.lua Outdated
Comment thread apisix/plugins/ai-transport/http.lua
@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels May 19, 2026
Comment thread apisix/cli/config.lua Outdated
AlinsRan
AlinsRan previously approved these changes May 20, 2026
membphis
membphis previously approved these changes May 20, 2026
@nic-6443 nic-6443 dismissed stale reviews from membphis, shreemaan-abhishek, and AlinsRan via 8219798 May 20, 2026 06:21
Comment thread t/chaos/utils/Dockerfile
git \
openldap-dev \
pcre-dev \
rust \
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.

why we need rust?

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

Labels

enhancement New feature or request size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants