From c2ad1c004d7f5406f5eb302c533e7c7c14ea2ce0 Mon Sep 17 00:00:00 2001 From: algal Date: Thu, 30 Mar 2023 17:39:58 -0700 Subject: [PATCH] Decode response body as utf-8 and then parse as json This changes response parsing so that the response body is decoded as UTF-8 and then parsed as JSON, rather than the other way around. This fixes the handling of responses that contain Unicode characters which are encoded with multiple bytes in UTF-8, such as emojis. --- gptel.el | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/gptel.el b/gptel.el index bdbd6c0..8993f24 100644 --- a/gptel.el +++ b/gptel.el @@ -417,16 +417,15 @@ INFO is a plist with the following keys: (if-let* ((status (buffer-substring (line-beginning-position) (line-end-position))) (json-object-type 'plist) (response (progn (forward-paragraph) - (condition-case nil - (json-read) - (json-readtable-error 'json-read-error))))) + (let ((json-str (decode-coding-string + (buffer-substring-no-properties (point) (point-max)) + 'utf-8))) + (condition-case nil + (json-read-from-string json-str) + (json-readtable-error 'json-read-error)))))) (cond ((string-match-p "200 OK" status) - (list :content (string-trim - (decode-coding-string - (map-nested-elt - response '(:choices 0 :message :content)) - 'utf-8)) + (list :content (string-trim (map-nested-elt response '(:choices 0 :message :content))) :status status)) ((plist-get response :error) (let* ((error-plist (plist-get response :error))