nic-6443 opened a new pull request, #13231:
URL: https://github.com/apache/apisix/pull/13231
## Problem
When LLM providers (e.g., vLLM) return JSON `null` for nullable object
fields like `prompt_tokens_details`, `cjson.decode` converts them to
`cjson.null` — a Lua `userdata` value that is **truthy** (only `nil` and
`false` are falsy in Lua). Code using `if x then x.field end` patterns passes
the guard but crashes when trying to index the userdata.
For example, OpenAI's API spec marks `prompt_tokens_details` as `nullable:
true`. When a provider returns `"prompt_tokens_details": null`, the existing
code:
```lua
if res_body.usage.prompt_tokens_details then -- cjson.null is truthy,
passes!
... = res_body.usage.prompt_tokens_details.cached_tokens -- crash:
indexing userdata
end
```
## Fix
Replace all unsafe truthiness-based guards with explicit `type(x) ==
"table"` checks across 4 AI protocol files:
| File | Fixes | Fields |
|------|-------|--------|
| `anthropic-messages-to-openai-chat.lua` | 6 | `choice.message`,
`tc["function"]`, `res_body.usage`, `prompt_tokens_details` |
| `openai-embeddings.lua` | 1 | `res_body.usage` |
| `openai-embeddings-to-vertex-predict.lua` | 2 | `pred.embeddings`,
`emb.statistics` |
| `anthropic-messages.lua` | 1 | `err_data.error` |
Also added test cases covering JSON null usage fields in the Anthropic
protocol conversion path.
--
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]