Copilot commented on code in PR #13231:
URL: https://github.com/apache/apisix/pull/13231#discussion_r3090760376


##########
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:
   In the SSE `error` event path, `err_data` can be `cjson.null` (userdata) 
when `event.data` is JSON `null`. The current guard `err_data and 
type(err_data.error) == "table" ...` will still try to index `err_data.error` 
(crashes on userdata). Please guard `type(err_data) == "table"` before 
accessing `.error`, and then separately check `type(err_data.error) == "table"`.
   ```suggestion
           local error_obj = type(err_data) == "table" and type(err_data.error) 
== "table"
                           and err_data.error or nil
           local err_type = error_obj and error_obj.type or "unknown"
           local err_msg = error_obj and error_obj.message or "unknown"
   ```



##########
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:
   `extract_usage` still assumes `res_body` is indexable: if the decoded 
top-level response is JSON `null`, `res_body` becomes `cjson.null` (userdata) 
and `type(res_body.usage)` will attempt to index userdata and crash. Consider 
changing the guard to require `type(res_body) == "table"` before looking at 
`.usage`.
   ```suggestion
       if type(res_body) ~= "table" or type(res_body.usage) ~= "table" then
   ```



##########
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:
   In `convert_response`, `pred` elements inside `predictions` can be JSON 
`null` -> `cjson.null` (userdata). The expression `type(pred.embeddings) == 
"table"` will still index `pred.embeddings` and crash on userdata. Add a 
`type(pred) == "table"` check before accessing fields. Also, 
`emb.statistics.token_count` can be `cjson.null`; adding it to `total_tokens` 
will error unless you verify it is a number.



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