From d3ec7c7320151244f07252a73628b1417f694b06 Mon Sep 17 00:00:00 2001 From: Peter Cardenas <16930781+PeterCardenas@users.noreply.github.com> Date: Sun, 30 Nov 2025 19:07:33 -0800 Subject: [PATCH] fix: gracefully handle utf-8 errors (#2834) --- lua/avante/tokenizers.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lua/avante/tokenizers.lua b/lua/avante/tokenizers.lua index 9e86f01..36879d8 100644 --- a/lua/avante/tokenizers.lua +++ b/lua/avante/tokenizers.lua @@ -52,7 +52,13 @@ function M.encode(prompt) if not prompt or prompt == "" then return nil end if type(prompt) ~= "string" then error("Prompt is not type string", 2) end - return tokenizers.encode(prompt) + local success, result = pcall(tokenizers.encode, prompt) + -- Some output like terminal command output might not be utf-8 encoded, which will cause an error here + if not success then + Utils.warn("Failed to encode prompt: " .. result) + return nil + end + return result end ---@param prompt string