Skip to content

Commit dfd8879

Browse files
committed
gptel-openai: Adapt OpenAI tool-ids for Openrouter+Anthropic
* gptel-openai.el (gptel--openai-format-tool-id, gptel--openai-unformat-tool-id): It is possible to use the gptel-openai to access Anthropic backends with Openrouter. In this case, the tool call ids are not in the call_* format, and tool calls fail (#747). Try to work around this by registering tool call ids as received when they are not in the call_* format. This will make the chat log not work when switching backends when both of these conditions are met: (i) tool results are included in the buffer and (ii) a non-standard tool call id is in effect. But there's not much we can do about it since we no longer tell if the gptel-openai backend in effect wants call_*, toolu_* or some other format. * gptel-anthropic.el (gptel--anthropic-unformat-tool-id): Don't issue a message if the tool call id is not in the expected form.
1 parent 35d53ca commit dfd8879

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

gptel-anthropic.el

+1-3
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,7 @@ TOOL-USE is a list of plists containing tool names, arguments and call results."
300300
(defun gptel--anthropic-unformat-tool-id (tool-id)
301301
(or (and (string-match "toolu_\\(.+\\)" tool-id)
302302
(match-string 1 tool-id))
303-
(progn
304-
(message "Unexpected tool_call_id format: %s" tool-id)
305-
tool-id)))
303+
tool-id))
306304

307305
(cl-defmethod gptel--parse-list ((_backend gptel-anthropic) prompt-list)
308306
(cl-loop for text in prompt-list

gptel-openai.el

+8-4
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,18 @@ Mutate state INFO with response metadata."
328328
tool-use))
329329

330330
(defun gptel--openai-format-tool-id (tool-id)
331-
(format "call_%s" tool-id))
331+
"Format TOOL-ID for OpenAI.
332+
333+
If the ID has the format used by a different backend, use as-is."
334+
(if (or (string-prefix-p "toolu_" tool-id) ;#747
335+
(string-prefix-p "call_" tool-id))
336+
tool-id
337+
(format "call_%s" tool-id)))
332338

333339
(defun gptel--openai-unformat-tool-id (tool-id)
334340
(or (and (string-match "call_\\(.+\\)" tool-id)
335341
(match-string 1 tool-id))
336-
(progn
337-
(message "Unexpected tool_call_id format: %s" tool-id)
338-
tool-id)))
342+
tool-id))
339343

340344
;; NOTE: No `gptel--inject-prompt' method required for gptel-openai, since this
341345
;; is handled by its defgeneric implementation

0 commit comments

Comments
 (0)