diff --git a/lua/gp/dispatcher.lua b/lua/gp/dispatcher.lua index c28caba9..343f3a74 100644 --- a/lua/gp/dispatcher.lua +++ b/lua/gp/dispatcher.lua @@ -320,7 +320,12 @@ local query = function(buf, provider, payload, handler, on_exit, callback) if qt.provider == "googleai" then if line:match('"text":') then - content = vim.json.decode("{" .. line .. "}").text + -- Escape raw newlines to prevent json decode from crashing + local sanitized = line:gsub("\r", ""):gsub("\n", "\\n") + local success, decoded = pcall(vim.json.decode, "{" .. sanitized .. "}") + if success and decoded and decoded.text then + content = decoded.text + end end end