Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

BOT-1223 Fix zero-arity tool call schemas for anthropic provider (#71618)

appleby4ff8b2722e6d
diff --git a/src/metabase/metabot/self/claude.clj b/src/metabase/metabot/self/claude.clj
index 764e1c6e04b..ef3284b5b58 100644
--- a/src/metabase/metabot/self/claude.clj
+++ b/src/metabase/metabot/self/claude.clj
@@ -154,7 +154,7 @@
:content [{:type "tool_use"
:id (:id part)
:name (:function part)
- :input (:arguments part)}]}
+ :input (or (:arguments part) {})}]}
:tool-output {:role "user"
:content [{:type "tool_result"
:tool_use_id (:id part)
diff --git a/src/metabase/metabot/self/core.clj b/src/metabase/metabot/self/core.clj
index ca40a88da92..bc3e4b254a3 100644
--- a/src/metabase/metabot/self/core.clj
+++ b/src/metabase/metabot/self/core.clj
@@ -416,7 +416,7 @@
(= (:type chunk) :tool-output-available) (assoc ::duration-ms duration-ms))))
results (try
(let [{:keys [arguments]} (into {} (aisdk-xf) chunks)
- arguments (coerce-stringified-json arguments)
+ arguments (or (coerce-stringified-json arguments) {})
decode (tool-decode-fn tool)
arguments (cond-> arguments decode decode)]
(log/debug "Executing tool" {:tool-name tool-name :arguments arguments})
diff --git a/src/metabase/metabot/self/openrouter.clj b/src/metabase/metabot/self/openrouter.clj
index ef53aac8d14..e248ff17a76 100644
--- a/src/metabase/metabot/self/openrouter.clj
+++ b/src/metabase/metabot/self/openrouter.clj
@@ -63,7 +63,7 @@
:type "function"
:function {:name (:function part)
:arguments (let [args (:arguments part)]
- (if (string? args) args (json/encode args)))}}]}
+ (if (string? args) args (json/encode (or args {}))))}}]}
:tool-output {:role "tool"
:tool_call_id (:id part)
:content (or (get-in part [:result :output])
diff --git a/src/metabase/metabot/tools/document.clj b/src/metabase/metabot/tools/document.clj
index 62efa06f42f..1844b25072c 100644
--- a/src/metabase/metabot/tools/document.clj
+++ b/src/metabase/metabot/tools/document.clj
@@ -91,7 +91,7 @@
- Use this tool when an existing model or metric does not answer the user's question.
- Do NOT use this tool if there is a model or metric that can answer the user's question
unless the user is requesting SQL."
- [_args :- [:maybe [:map {:closed true}]]]
+ [_args :- [:map {:closed true}]]
(try
(let [refs (context-references)
database-ids (referenced-database-ids refs)]
diff --git a/src/metabase/metabot/tools/metadata.clj b/src/metabase/metabot/tools/metadata.clj
index 66ce1fc1c84..0e698627c00 100644
--- a/src/metabase/metabot/tools/metadata.clj
+++ b/src/metabase/metabot/tools/metadata.clj
@@ -136,7 +136,7 @@
(mu/defn ^{:tool-name "list_available_data_sources"}
list-available-data-sources-tool
"List all data sources (metrics and models) available to the metabot instance."
- [_args :- [:maybe [:map {:closed true}]]]
+ [_args :- [:map {:closed true}]]
(add-output
(entity-details-tools/answer-sources {:metabot-id shared/*metabot-id*
:with-field-values? false
diff --git a/src/metabase/metabot/tools/snippets.clj b/src/metabase/metabot/tools/snippets.clj
index 99188ecce11..d1daf33f366 100644
--- a/src/metabase/metabot/tools/snippets.clj
+++ b/src/metabase/metabot/tools/snippets.clj
@@ -62,7 +62,7 @@
Use this tool before editing or creating SQL transforms to understand what
snippets are available. If any snippets may be relevant to the user's request,
fetch their content using the get_snippet_details tool."
- [_args :- [:maybe [:map {:closed true}]]]
+ [_args :- [:map {:closed true}]]
(add-output (get-snippets {}) format-snippet-list-output))
(mu/defn ^{:tool-name "get_snippet_details"
diff --git a/src/metabase/metabot/tools/todo.clj b/src/metabase/metabot/tools/todo.clj
index b380b906ef3..c27e1091a56 100644
--- a/src/metabase/metabot/tools/todo.clj
+++ b/src/metabase/metabot/tools/todo.clj
@@ -133,7 +133,7 @@
todo-read-tool
"Read the current todo list from memory.
Returns the list of todos that have been created during this conversation."
- [_args :- [:maybe [:map {:closed true}]]]
+ [_args :- [:map {:closed true}]]
(try
(add-output
(todo-read {:memory-atom shared/*memory-atom*})
diff --git a/test/metabase/metabot/self/claude_test.clj b/test/metabase/metabot/self/claude_test.clj
index 9876020972d..5cfac52eae1 100644
--- a/test/metabase/metabot/self/claude_test.clj
+++ b/test/metabase/metabot/self/claude_test.clj
@@ -121,6 +121,16 @@
{:type :tool-output :id "call-1" :result {:output "2025-02-13T14:00:00+02:00"}}
{:type :text :text "It's 2:00 PM in Kyiv."}])))))
+(deftest ^:parallel parts->claude-messages-nil-arguments-test
+ (testing "tool call with nil arguments defaults to empty object"
+ (is (=? [{:role "assistant"
+ :content [{:type "tool_use"
+ :id "call-1"
+ :name "todo_read"
+ :input {}}]}]
+ (claude/parts->claude-messages
+ [{:type :tool-input :id "call-1" :function "todo_read" :arguments nil}])))))
+
(deftest ^:parallel parts->claude-messages-error-result-test
(testing "tool error is formatted as error string"
(is (=? [{:role "user"
diff --git a/test/metabase/metabot/self/openrouter_test.clj b/test/metabase/metabot/self/openrouter_test.clj
index a1e8e460e55..82eb1a8fbfc 100644
--- a/test/metabase/metabot/self/openrouter_test.clj
+++ b/test/metabase/metabot/self/openrouter_test.clj
@@ -60,6 +60,17 @@
[{:type :tool-output :id "call-1" :result {:output "Result 1"}}
{:type :tool-output :id "call-2" :result {:output "Result 2"}}])))))
+(deftest ^:parallel parts->cc-messages-nil-arguments-test
+ (testing "tool call with nil arguments defaults to empty object JSON string"
+ (is (=? [{:role "assistant"
+ :content nil
+ :tool_calls [{:id "call-1"
+ :type "function"
+ :function {:name "todo_read"
+ :arguments "{}"}}]}]
+ (openrouter/parts->cc-messages
+ [{:type :tool-input :id "call-1" :function "todo_read" :arguments nil}])))))
+
(deftest ^:parallel parts->cc-messages-full-conversation-test
(testing "full conversation with tool round-trip"
(is (=? [{:role "user" :content "What time is it in Kyiv?"}
diff --git a/test/metabase/metabot/self_test.clj b/test/metabase/metabot/self_test.clj
index 1105df18d34..06ed431b004 100644
--- a/test/metabase/metabot/self_test.clj
+++ b/test/metabase/metabot/self_test.clj
@@ -235,13 +235,23 @@
{:type :tool-input :id "call-err" :function "get-time" :arguments {:tz "Invalid/Timezone"}}])
result (into [] (self.core/tool-executor-xf test-util/TOOLS) chunks)]
(is (= (count chunks) (dec (count result))))
- (let [tool-result (last result)]
- (is (=? {:type :tool-output-available
- :toolCallId "call-err"
- :toolName "get-time"
- :error {:message string?
- :type string?}}
- tool-result)))))
+ (is (=? {:type :tool-output-available
+ :toolCallId "call-err"
+ :toolName "get-time"
+ :error {:message string?
+ :type string?}}
+ (last result)))))
+
+ (testing "tool-executor-xf handles nil arguments for no-arg tools"
+ (let [chunks (test-util/parts->aisdk-chunks
+ [{:type :start :id "msg-nil"}
+ {:type :tool-input :id "call-nil" :function "no-arg" :arguments nil}])
+ result (into [] (self.core/tool-executor-xf test-util/TOOLS) chunks)]
+ (is (=? {:type :tool-output-available
+ :toolCallId "call-nil"
+ :toolName "no-arg"
+ :result {:output "ok"}}
+ (last result)))))
(testing "tool-executor-xf ignores unknown tool names"
(let [chunks (test-util/parts->aisdk-chunks
diff --git a/test/metabase/metabot/test_util.clj b/test/metabase/metabot/test_util.clj
index 3d6fead7e3e..1e37bb6f977 100644
--- a/test/metabase/metabot/test_util.clj
+++ b/test/metabase/metabot/test_util.clj
@@ -145,7 +145,13 @@
(reduce [_ rf init]
(reduce rf init chunks)))))})
+(defn no-arg-tool []
+ {:tool-name "no-arg"
+ :doc "A tool that takes no arguments."
+ :schema [:=> [:cat [:map {:closed true}]] :any]
+ :fn (fn [_] {:output "ok"})})
+
(def TOOLS
"Tool map for tests — keyed by tool name string."
- (let [tool-defs (map #(%) [get-time-tool convert-currency-tool mock-llm-tool])]
+ (let [tool-defs (map #(%) [get-time-tool convert-currency-tool mock-llm-tool no-arg-tool])]
(into {} (map (juxt :tool-name identity)) tool-defs)))