fix: gracefully handle utf-8 errors (#2834)

This commit is contained in:
Peter Cardenas
2025-11-30 19:07:33 -08:00
committed by GitHub
parent 4d9d2de6fe
commit d3ec7c7320

View File

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