branch: elpa/gptel
commit 59c00487975385a9a4b358c06da8d11daf724c91
Author: Karthik Chikmagalur <[email protected]>
Commit: Karthik Chikmagalur <[email protected]>
gptel: Handle case of missing tool-id
* gptel-openai.el (gptel--openai-format-tool-id): When a tool
result in the buffer is missing a tool-id, generate one for OpenAI
requests. This can happen when the tool call is from an API that
does not use tool-ids, like Gemini.
* gptel-anthropic.el (gptel--anthropic-format-tool-id): Ditto.
---
gptel-anthropic.el | 4 ++++
gptel-openai.el | 4 ++++
2 files changed, 8 insertions(+)
diff --git a/gptel-anthropic.el b/gptel-anthropic.el
index 42caca5670..7ec53be442 100644
--- a/gptel-anthropic.el
+++ b/gptel-anthropic.el
@@ -300,6 +300,10 @@ TOOL-USE is a list of plists containing tool names,
arguments and call results."
;; this is handled by its defgeneric implementation
(defun gptel--anthropic-format-tool-id (tool-id)
+ (unless tool-id
+ (setq tool-id (substring
+ (md5 (format "%s%s" (random) (float-time)))
+ nil 24)))
(if (string-prefix-p "toolu_" tool-id)
tool-id
(format "toolu_%s" tool-id)))
diff --git a/gptel-openai.el b/gptel-openai.el
index 9a49ba5542..cd3fb850f6 100644
--- a/gptel-openai.el
+++ b/gptel-openai.el
@@ -331,6 +331,10 @@ Mutate state INFO with response metadata."
"Format TOOL-ID for OpenAI.
If the ID has the format used by a different backend, use as-is."
+ (unless tool-id
+ (setq tool-id (substring
+ (md5 (format "%s%s" (random) (float-time)))
+ nil 24)))
(if (or (string-prefix-p "toolu_" tool-id) ;#747
(string-prefix-p "call_" tool-id))
tool-id