nic-6443 commented on code in PR #13231:
URL: https://github.com/apache/apisix/pull/13231#discussion_r3092000824


##########
apisix/plugins/ai-protocols/anthropic-messages.lua:
##########
@@ -110,8 +110,10 @@ function _M.parse_sse_event(event, ctx, state)
 
     elseif event.type == "error" then
         local err_data = core.json.decode(event.data)
-        local err_type = err_data and err_data.error and err_data.error.type 
or "unknown"
-        local err_msg = err_data and err_data.error and err_data.error.message 
or "unknown"
+        local err_type = err_data and type(err_data.error) == "table"
+                        and err_data.error.type or "unknown"
+        local err_msg = err_data and type(err_data.error) == "table"
+                        and err_data.error.message or "unknown"

Review Comment:
   Fixed in 4e6a317 — the `null_as_nil` option now also handles top-level 
`cjson.null` (returns `nil` instead). So if `event.data` decodes to 
`cjson.null`, `err_data` will be `nil` and the guard short-circuits safely.



##########
apisix/plugins/ai-protocols/converters/openai-embeddings-to-vertex-predict.lua:
##########
@@ -80,13 +80,13 @@ function _M.convert_response(body, ctx)
     local total_tokens = 0
 
     for i, pred in ipairs(predictions) do
-        local emb = pred.embeddings or {}
+        local emb = type(pred.embeddings) == "table" and pred.embeddings or {}
         local values = emb.values
         if type(values) ~= "table" then
             return nil, "invalid embedding at index " .. i
         end
 
-        if emb.statistics and emb.statistics.token_count then
+        if type(emb.statistics) == "table" and emb.statistics.token_count then
             total_tokens = total_tokens + emb.statistics.token_count
         end

Review Comment:
   Fixed in 4e6a317 — `null_as_nil` strips null values recursively within 
tables (array elements included), so `pred` elements that were `cjson.null` 
become `nil` and are skipped by `ipairs`.



##########
apisix/plugins/ai-protocols/openai-embeddings.lua:
##########
@@ -38,7 +38,7 @@ end
 
 
 function _M.extract_usage(res_body)
-    if not res_body or not res_body.usage then
+    if not res_body or type(res_body.usage) ~= "table" then

Review Comment:
   Fixed in 4e6a317 — `null_as_nil` now returns `nil` for top-level 
`cjson.null`, so `res_body` will be `nil` (not userdata) and callers guard 
correctly.



-- 
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]

Reply via email to