Skip to content

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

Accept nested tool arguments when replaying Metabot tool calls (#83511)

Andrei Simionescu8e8712ac80dd
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index d3bd274659f..c2053c233d6 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -1493,7 +1493,6 @@
query-permissions
query-processor
request
- request.schema
search
search.entity-retrieval
server
diff --git a/src/metabase/metabot/self/core.clj b/src/metabase/metabot/self/core.clj
index 37189252fd4..4efb30c52f7 100644
--- a/src/metabase/metabot/self/core.clj
+++ b/src/metabase/metabot/self/core.clj
@@ -11,7 +11,6 @@
[metabase.llm.settings :as llm]
[metabase.metabot.schema.v2 :as schema.v2]
[metabase.premium-features.core :as premium-features]
- [metabase.request.schema :as request.schema]
[metabase.settings.core :as setting]
[metabase.util :as u]
[metabase.util.http :as u.http]
@@ -115,11 +114,23 @@
[:message {:optional true} [:maybe :string]]
[:type {:optional true} [:maybe :string]]]]]]])
+(mr/def ::decoded-json
+ "A value decoded from JSON.
+ Object keys are strings from `json/decode` (replayed history) or keywords from `json/decode+kw` (the stream)."
+ [:or
+ :string
+ :keyword
+ number?
+ :boolean
+ :nil
+ [:sequential [:ref ::decoded-json]]
+ [:map-of [:or :string :keyword] [:ref ::decoded-json]]])
+
(def ^:private ToolCallArguments
"A tool call's arguments as the LLM wrote them against the tool's own schema, keyed by that tool's argument names:
string keys off the wire, keyword keys when built in Clojure."
[:map-of {::mr/deliberately-open true, :description "tool call arguments"}
- [:or :string :keyword] ::request.schema/json-value])
+ [:or :string :keyword] ::decoded-json])
(def ^:private AISDKPart
"One element of the `:input` sequence passed to a provider adapter: an AISDK part keyed by
diff --git a/src/metabase/request/schema.clj b/src/metabase/request/schema.clj
index ad69f30ecfe..5d6f9e4e713 100644
--- a/src/metabase/request/schema.clj
+++ b/src/metabase/request/schema.clj
@@ -20,17 +20,6 @@
[:data-app-scoped? {:optional true} :boolean]
[:authenticated-via-oauth? {:optional true} :boolean]])
-(mr/def ::json-value
- "A JSON-shaped value: a scalar, a sequence of JSON values, or a string-keyed JSON object."
- [:or
- :string
- :keyword
- number?
- :boolean
- :nil
- [:sequential [:ref ::json-value]]
- [:map-of :string [:ref ::json-value]]])
-
(mr/def ::multipart-file
"One `:multipart-params` entry for an uploaded file, as `ring.middleware.multipart-params` builds it."
[:map {:closed true}
diff --git a/test/metabase/metabot/self/openai/chat_completions_test.clj b/test/metabase/metabot/self/openai/chat_completions_test.clj
index f3829a74e4c..8d3a4a3518a 100644
--- a/test/metabase/metabot/self/openai/chat_completions_test.clj
+++ b/test/metabase/metabot/self/openai/chat_completions_test.clj
@@ -4,7 +4,8 @@
[metabase.metabot.self.core :as self.core]
[metabase.metabot.self.mistral :as mistral]
[metabase.metabot.self.openai.chat-completions :as chat-completions]
- [metabase.metabot.test-util :as metabot.tu]))
+ [metabase.metabot.test-util :as metabot.tu]
+ [metabase.util.json :as json]))
(set! *warn-on-reflection* true)
@@ -233,6 +234,42 @@
:temperature 0.2
:max-tokens 128})))))
+(deftest ^:parallel request-body-replays-nested-tool-arguments-test
+ (testing "a streamed tool call whose arguments nest objects replays into the next request"
+ (let [query {:lib/type "mbql/query"
+ :stages [{:lib/type "mbql.stage/mbql", :source-table ["Sample Database" nil "ORDERS"]}]}
+ call {:type :tool-input
+ :id "call-1"
+ :function "construct_notebook_query"
+ :arguments {:query query}}
+ ;; the stream parser decodes the arguments with keyword keys at every depth
+ parts (into [] (self.core/aisdk-xf) (metabot.tu/parts->aisdk-chunks [call]))]
+ (is (=? {:messages [{:role "assistant"
+ :tool_calls [{:function {:name "construct_notebook_query"
+ :arguments #(= {:query query} (json/decode+kw %))}}]}]}
+ (chat-completions/request-body {:model "some/model" :input parts}))))))
+
+(deftest ^:parallel request-body-tool-arguments-schema-test
+ (let [replay (fn [arguments]
+ (chat-completions/request-body
+ {:model "some/model"
+ :input [{:type :tool-input
+ :id "call-1"
+ :function "f"
+ :arguments arguments}]}))]
+ (testing "decoded JSON replays at any depth, keyed by strings, keywords or both"
+ (are [arguments] (=? {:messages [{:tool_calls [{:function {:arguments string?}}]}]}
+ (replay arguments))
+ ;; keyword keys, as the stream decodes them
+ {:a {:b {:c [{:d [1 nil true "x" :kw]}]}}}
+ ;; string keys, as replayed history decodes them
+ {"a" {"b" {"c" [{"d" [1.5 [[]] {}]}]}}}
+ ;; both
+ {:a {"b" [{:c {"d" [{:e 1}]}}]}}))
+ (testing "a value JSON cannot hold is rejected, however deep"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Invalid input"
+ (replay {:a {"b" [{:c (java.time.Instant/now)}]}}))))))
+
;;; ──────────────────────────────────────────────────────────────────
;;; Streaming chunk conversion tests
;;;