Skip to content

fix(ai-proxy): keep empty anthropic tools array - #14999

Open
tonydzi wants to merge 1 commit into
Kong:masterfrom
tonydzi:fix/ai-proxy-anthropic-empty-tools
Open

tonydzi wants to merge 1 commit into
Kong:masterfrom
tonydzi:fix/ai-proxy-anthropic-empty-tools

Conversation

@tonydzi

@tonydzi tonydzi commented Sep 15, 2026

Copy link
Copy Markdown

Summary

An OpenAI-format chat request with tools: [], proxied by ai-proxy to the anthropic provider, leaves Kong as "tools": {}. Anthropic rejects that with 400 tools: Input should be a valid array — the same error reported in #14983.

Root cause: to_tools() in kong/llm/drivers/anthropic.lua builds the translated list as a plain {}. The request body itself is decoded with array_mt (kong.request.get_body → decode_with_array_mt), but this new table has no metatable, so when nothing is inserted cjson encodes it as an object in normalize-request → kong.service.request.set_body.

The fix creates that table with cjson.array_mt. Non-empty tools encode exactly as before.

This does not replace #14984. #14983 is about ai-prompt-decorator with the native Anthropic format, which is not in this repo's decorator schema (openai, bedrock, gemini). I found this separate path on master while reproducing it: no decorator needed, just ai-proxy + anthropic + tools: [].

Reproduction

resty in the kong:3.9 image with this repo's master sources mounted on package.path. Input decoded with kong.tools.cjson.decode_with_array_mt, then anthropic.to_format(..., "llm/v1/chat"), then cjson.safe.encode (what set_body uses):

in : {"messages":[{"role":"user","content":"ping"}],"tools":[]}
out: {"model":"claude-opus-4","max_tokens":8,"stream":false,"tools":{},"messages":[{"role":"user","content":"ping"}]}

Tests

New unit case in spec/03-plugins/38-ai-proxy/01-unit_spec.lua ("anthropic tools").

Run locally: busted 2.3.0 under resty in the kong:3.9 image (OpenResty LuaJIT + lua-cjson from that image), master sources from this branch. This is not Kong's make test dev environment.

  • without the driver change: not ok — Passed in: '{..."tools":{}}', Expected: '"tools":[]'
  • with the change: ok
  • whole 01-unit_spec.lua: 156/156 ok with the change, 155/156 without (only the new case fails)
  • luacheck on both touched Lua files: 0 warnings / 0 errors

Not run: integration specs (03-anthropic_integration_spec.lua) and the full CI matrix.

Changelog: changelog/unreleased/kong/fix-ai-proxy-anthropic-empty-tools.yml.

I built this with Claude Code as an implementation collaborator; I checked the reproduction and test runs above.

— Anton Dziatkovskii · github.com/tonydzi

🤖 Generated with Claude Code

An OpenAI-format chat request with `tools: []` routed to the
anthropic provider was sent upstream as `tools: {}`. `to_tools()`
builds a fresh Lua table without `array_mt`, so cjson encodes the
empty result as an object and Anthropic rejects the request with
`400 tools: Input should be a valid array`.

* create the translated tools table with `cjson.array_mt`
* add a unit test that encodes the translated request

Related Kong#14983

Assisted-by: Claude Code/claude-opus-5
Machine: A-Mac16-2019-PaloAlto
Account: tonydzi
Operator: anton
Co-Authored-By: Claude Opus 5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-15T12:27:04.388517Z a238bb2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@CLAassistant

CLAassistant commented Sep 15, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants