Skip to content

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

[BOT-1855] Assemble streamed tool calls by index for OpenAI-compatible servers (#83728)

Voytek Pitułac6b18e378cd9
diff --git a/src/metabase/metabot/self/core.clj b/src/metabase/metabot/self/core.clj
index edd2d967962..66e7b0609a2 100644
--- a/src/metabase/metabot/self/core.clj
+++ b/src/metabase/metabot/self/core.clj
@@ -422,10 +422,19 @@
:function (:toolName chunk)
:result (:result chunk)
:error (:error chunk)
- :duration-ms (::duration-ms chunk)}))
+ :duration-ms (::duration-ms chunk)}
+ ;; A group opening with a continuation chunk (:tool-input-delta, :text-delta, ...) has lost its start chunk,
+ ;; and with it the data the part needs (a tool call's name), so it cannot be assembled.
+ (let [id (or (:id chunk) (:toolCallId chunk))]
+ (throw (ex-info (format "Model stream sent a %s chunk for %s without its start chunk" (:type chunk) (pr-str id))
+ {:chunk-type (:type chunk)
+ :id id})))))
(defn aisdk-xf
- "Collect a stream of AI SDK v5 chunks into a list of parts (joins by id)."
+ "Collect a stream of AI SDK v5 chunks into a list of parts (joins by id).
+
+ Producers must emit the chunks of each id contiguously, the start chunk first. A group that does not open
+ with its start chunk throws."
([] (aisdk-xf nil))
([{:keys [stream-text?]}]
(fn [rf]
diff --git a/src/metabase/metabot/self/openai/chat_completions.clj b/src/metabase/metabot/self/openai/chat_completions.clj
index 08c299cfcb7..6efa97d6db7 100644
--- a/src/metabase/metabot/self/openai/chat_completions.clj
+++ b/src/metabase/metabot/self/openai/chat_completions.clj
@@ -14,6 +14,7 @@
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
[metabase.util.json :as json]
+ [metabase.util.log :as log]
[metabase.util.malli :as mu]))
(set! *warn-on-reflection* true)
@@ -156,6 +157,65 @@
(or (not-empty (:reasoning m))
(not-empty (:reasoning_content m))))
+(defn- conj-tool-call-delta
+ "Fold one `delta.tool_calls` entry into `state`, the tool calls a response has streamed so far:
+ `:calls`, a vector of `{:id :name :args}` in arrival order, `:args` holding the argument fragments;
+ `:by-key`, the position of the call each `index` and each `id` last addressed; and
+ `:previous`, the position the previous entry addressed.
+
+ An entry joins the call its `index` addresses, falling back to its `id`, then to the previous entry's
+ call. A blank `id` counts as no `id`. It opens a new call when nothing is addressed yet, or when it
+ carries an `id` that differs from the addressed call's — a server reusing one `index` for several
+ calls. A call adopts the first `id` and `name` any of its entries carry. The `index` outranks the `id`, so
+ two calls that have the same `id` at different indexes stay two calls, and [[with-ids]] rejects them."
+ [{:keys [calls by-key previous] :as _state} {:keys [index id function] :as _tool-call}]
+ (let [id (when-not (str/blank? id) id)
+ pos (cond
+ (some? index) (get by-key index)
+ id (get by-key id)
+ :else previous)
+ existing (some->> pos (nth calls))
+ new? (or (nil? existing)
+ (and id (:id existing) (not= id (:id existing))))
+ pos (if new? (count calls) pos)
+ base (if new? {:args []} existing)
+ call (cond-> base
+ (and id (nil? (:id base))) (assoc :id id)
+ (and (not-empty (:name function))
+ (nil? (:name base))) (assoc :name (:name function))
+ (not-empty (:arguments function)) (update :args conj (:arguments function)))]
+ {:calls (assoc calls pos call)
+ :by-key (cond-> by-key
+ (some? index) (assoc index pos)
+ id (assoc id pos))
+ :previous pos}))
+
+(defn- with-ids
+ "Give each call in `calls` that has no `id` a minted one. Downstream, chunks and tool results join by
+ `toolCallId`, so the server must send a different `id` for each call. If two calls have the same `id`, this
+ throws: no known server does this, so we do not guess how to separate them."
+ [calls]
+ (doseq [[id same-id-calls] (group-by :id (filter :id calls))
+ :when (> (count same-id-calls) 1)]
+ (throw (ex-info (format "Model stream sent the tool call id %s for %d different calls"
+ (pr-str id) (count same-id-calls))
+ {:id id
+ :tool-names (mapv :name same-id-calls)})))
+ (mapv #(update % :id (fn [id] (or id (core/mkid)))) calls))
+
+(defn- tool-call-chunks
+ "The AI SDK chunks for one assembled tool call: its start, one delta that holds all its arguments, and
+ its availability. The call is emitted whole, so one delta is sufficient. A call that never received a
+ `name` cannot be run and yields no chunks."
+ [{:keys [id name args]}]
+ (if (str/blank? name)
+ (do (log/warnf "Dropping a streamed tool call that carried no function name (id %s)" (pr-str id))
+ [])
+ (let [arguments (str/join args)]
+ (cond-> [{:type :tool-input-start :toolCallId id :toolName name}]
+ (not-empty arguments) (conj {:type :tool-input-delta :toolCallId id :inputTextDelta arguments})
+ true (conj {:type :tool-input-available :toolCallId id :toolName name})))))
+
(defn chat-completions->aisdk-chunks-xf
"Translates Chat Completions streaming chunks into AI SDK v5 protocol chunks.
@@ -176,12 +236,12 @@
Chat Completions has no explicit start/stop events per content block like
Claude or OpenAI Responses do — we infer transitions from the delta shape.
- Parallel tool calls are tracked by tool-call `id`, not by `index`, which is
- never read: a tool-call delta whose `id` differs from the open one closes the
- previous block and opens a new one. That relies on providers sending `id` only
- on a tool call's opening chunk — one that repeated it on continuation chunks
- would lose their arguments, since neither the start branch (needs `:name`) nor
- the argument-delta branch (needs no `:id`) would fire.
+ Tool calls are assembled per call — keyed by `index`, falling back to `id` (see
+ [[conj-tool-call-delta]]) — and emitted once the response finishes, each call's
+ chunks contiguous and in arrival order. That tolerates servers that send a call's
+ `id` late, repeat it on every chunk, put several calls in one delta, or interleave
+ the deltas of parallel calls; the cost is that no tool call is emitted before the
+ response's `finish_reason` (or the end of the stream).
Takes the dialect's `finish_reason` table, defaulting to OpenAI's [[stop-reasons]]. A reason the table maps to
\"error\" (Mistral's `error`, Z.AI's `network_error`) fails the response the same way an error object does.
@@ -197,28 +257,35 @@
(chat-completions->aisdk-chunks-xf stop-reasons nil))
([stop-reasons {:keys [forward-reasoning?]}]
(fn [rf]
- (let [current-type (volatile! nil) ;; :text | :reasoning | :function_call | nil
- current-id (volatile! nil) ;; active chunk id (text-id, reasoning-id, or tool call_id)
+ (let [current-type (volatile! nil) ;; :text | :reasoning | nil
message-id (volatile! nil)
model-name (volatile! nil)
- payload (volatile! {}) ;; carried across start/delta/end, same as openai.clj
+ ;; carried across start/delta/end, same as openai.clj; :id is the open block's id
+ payload (volatile! {})
stop-reason (volatile! nil)
- clear! (fn []
- (vreset! current-type nil)
- (vreset! current-id nil)
- (vreset! payload {}))
+ no-calls {:calls [] :by-key {} :previous nil}
+ tool-calls (volatile! no-calls)
+ ;; Close the open text or reasoning block, if there is one
close! (fn [result]
- (u/prog1 (rf result (merge {:type (case @current-type
- :text :text-end
- :reasoning :reasoning-end
- :function_call :tool-input-available)}
- @payload))
- (clear!)))]
+ (if-let [block-type @current-type]
+ (u/prog1 (rf result (merge {:type (case block-type
+ :text :text-end
+ :reasoning :reasoning-end)}
+ @payload))
+ (vreset! current-type nil)
+ (vreset! payload {}))
+ result))
+ ;; Close the open block, then emit the assembled tool calls. A `reduced` result stops the
+ ;; emission and stays wrapped, so the consumer's early termination reaches the caller.
+ finish! (fn [result]
+ (u/prog1 (u/reduce-preserving-reduced
+ rf
+ (close! result)
+ (mapcat tool-call-chunks (with-ids (:calls @tool-calls))))
+ (vreset! tool-calls no-calls)))]
(fn
([result]
- (cond-> result
- @current-type (close!)
- true (rf)))
+ (rf (unreduced (finish! result))))
([result {:keys [id model choices usage error] :as _chunk}]
(let [choice (first choices)
@@ -229,64 +296,52 @@
(or (:message error)
(some-> error pr-str)
(tru "The model provider failed to complete the response")))
- tool-call (first (:tool_calls delta))
+ call-deltas (:tool_calls delta)
reasoning-md (:reasoning_metadata delta)
;; Determine what kind of content this chunk carries.
;; Empty-string content (common between tool calls) is ignored
- ;; to avoid spurious text blocks that would close open tools.
+ ;; to avoid spurious text blocks.
chunk-type (cond
(not-empty (:content delta)) :text
- ;; tool_calls outrank reasoning: a delta carrying both would
- ;; otherwise classify as :reasoning, and the tool call's opening
- ;; chunk — the only one carrying its id and name — would be
- ;; lost, breaking the tool loop. Ranked this way, such a delta
- ;; loses its reasoning fragment instead: display text,
- ;; recoverable. No probed provider combines the two in one
- ;; delta today.
- (some? tool-call) :function_call
+ ;; tool_calls outrank reasoning, so a delta carrying both loses its
+ ;; reasoning fragment: display text, recoverable. No probed provider
+ ;; combines the two in one delta today.
+ (seq call-deltas) :function_call
(and forward-reasoning?
(delta-reasoning delta)) :reasoning
- :else nil)
- ;; For new tool calls, the id comes from the chunk; for deltas
- ;; on the same tool, we keep current-id.
- chunk-id (or (:id tool-call) @current-id (core/mkid))]
+ :else nil)]
(cond-> result
;; Emit :start on first chunk
(and id (not @message-id)) (-> (rf {:type :start :messageId id})
(u/prog1
(vreset! message-id id)
(vreset! model-name model)))
- ;; Close previous block when type changes, or when a new tool
- ;; call arrives (different id = different tool in parallel)
+ ;; Close the open text or reasoning block when the content type changes
(and @current-type
- (or (and chunk-type
- (not= chunk-type @current-type))
- (and (= chunk-type :function_call)
- (not= chunk-id @current-id)))) (close!)
+ chunk-type
+ (not= chunk-type @current-type)) (close!)
;; Start a new text block
(and (= chunk-type :text)
(not= @current-type :text)) (-> (u/prog1
(let [tid (core/mkid)]
(vreset! current-type :text)
- (vreset! current-id tid)
(vreset! payload {:id tid})))
(rf (merge {:type :text-start} @payload)))
;; Text delta
(and (= chunk-type :text)
(some? (:content delta))) (rf {:type :text-delta
- :id @current-id
+ :id (:id @payload)
:delta (:content delta)})
;; Start a new reasoning block
(and (= chunk-type :reasoning)
(not= @current-type :reasoning)) (-> (u/prog1
(let [rid (core/mkid)]
(vreset! current-type :reasoning)
- (vreset! current-id rid)
(vreset! payload {:id rid})))
(rf (merge {:type :reasoning-start} @payload)))
;; Reasoning delta
(= chunk-type :reasoning) (rf {:type :reasoning-delta
- :id @current-id
+ :id (:id @payload)
:delta (delta-reasoning delta)})
;; A delta may carry ready-namespaced provider metadata for the
;; open reasoning block, ridden out on its end chunk the way
@@ -300,34 +355,17 @@
(= @current-type :reasoning)) (u/prog1
(vswap! payload assoc
:providerMetadata reasoning-md))
- ;; Start a new tool call block
- (and (= chunk-type :function_call)
- (:id tool-call)
- (:name (:function tool-call))) (-> (u/prog1
- (vreset! current-type :function_call)
- (vreset! current-id (:id tool-call))
- (vreset! payload {:toolCallId (:id tool-call)
- :toolName (:name (:function tool-call))}))
- (rf (merge {:type :tool-input-start} @payload))
- ;; Emit initial arguments if present
- (cond-> (not (str/blank? (:arguments (:function tool-call))))
- (rf {:type :tool-input-delta
- :toolCallId (:id tool-call)
- :inputTextDelta (:arguments (:function tool-call))})))
- ;; Tool argument delta (continuation of existing tool call)
- (and (= chunk-type :function_call)
- (not (:id tool-call))
- (some? (:arguments (:function tool-call)))) (rf {:type :tool-input-delta
- :toolCallId (:toolCallId @payload)
- :inputTextDelta (:arguments (:function tool-call))})
- ;; Closing a tool call runs it, so drop one that an error cuts off
- (and error-text
- (= @current-type :function_call)) (u/prog1 (clear!))
- ;; Finish reason — close whatever is open
+ ;; Every tool_calls entry joins its call, whatever the delta's content type
+ (seq call-deltas) (u/prog1
+ (vswap! tool-calls
+ #(reduce conj-tool-call-delta % call-deltas)))
+ ;; Emitting a tool call runs it, so drop the calls an error cuts off
+ error-text (u/prog1
+ (vreset! tool-calls no-calls))
+ ;; Finish reason — close whatever is open, then emit the assembled tool calls
(some? finish-reason) (-> (u/prog1
(vreset! stop-reason finish-reason))
- (cond->
- @current-type (close!)))
+ (finish!))
;; Usage (often on a separate final chunk with empty choices)
(some? usage) (rf (cond-> {:type :usage
:usage (usage->aisdk-usage usage)
@@ -337,7 +375,7 @@
(assoc :finish-reason (core/stop-reason->finish-reason stop-reasons @stop-reason)
:raw-finish-reason @stop-reason)))
;; An error in the stream, e.g. a failure partway through generation
- error-text (-> (cond-> @current-type (close!))
+ error-text (-> (close!)
(rf {:type :error :errorText error-text}))))))))))
;;; Request body
diff --git a/test/metabase/metabot/self/openai/chat_completions_test.clj b/test/metabase/metabot/self/openai/chat_completions_test.clj
index 1d5b752f26b..615cac6d12e 100644
--- a/test/metabase/metabot/self/openai/chat_completions_test.clj
+++ b/test/metabase/metabot/self/openai/chat_completions_test.clj
@@ -3,8 +3,10 @@
[clojure.test :refer :all]
[metabase.metabot.self.core :as self.core]
[metabase.metabot.self.mistral :as mistral]
+ [metabase.metabot.self.moonshot :as moonshot]
[metabase.metabot.self.openai.chat-completions :as chat-completions]
[metabase.metabot.self.openrouter :as openrouter]
+ [metabase.metabot.self.vllm :as vllm]
[metabase.metabot.self.zai :as zai]
[metabase.metabot.test-util :as metabot.tu]
[metabase.util.json :as json]))
@@ -351,10 +353,9 @@
{:choices []
:usage {:prompt_tokens 100 :completion_tokens 5 :cached_tokens 64}}]))))))
-(deftest ^:parallel chunks-xf-parallel-tool-calls-are-tracked-by-id-test
- (testing "tool calls serialized on one choice are split into separate blocks by tool-call id"
- ;; Both calls arrive on `choices[0]`; only the `tool_calls` entry's `id` distinguishes them.
- ;; The `index` field is present in the wire data and deliberately unread.
+(deftest ^:parallel chunks-xf-parallel-tool-calls-are-split-by-index-test
+ (testing "tool calls serialized on one choice are split into separate blocks by tool-call index"
+ ;; Both calls arrive on `choices[0]`; the `tool_calls` entry's `index` distinguishes them.
(is (=? [{:type :start}
{:type :tool-input
:id "get_weather_0"
@@ -387,15 +388,225 @@
{:choices [{:index 0 :delta {} :finish_reason "tool_calls"}]}
{:choices [] :usage {:prompt_tokens 138 :completion_tokens 36}}])))))
+;;; ──────────────────────────────────────────────────────────────────
+;;; Tool-call assembly fixtures
+;;;
+;;; SYNTHETIC: hand-written from the documented Chat Completions streaming
+;;; format, not captured from the named providers. Each fixture models one
+;;; delta shape a model family is reported to send; none has been recorded
+;;; from a live stream yet.
+;;; ──────────────────────────────────────────────────────────────────
+
+(defn- synthetic-stream
+ "Wrap `deltas` (each a Chat Completions `delta` map) in a stream envelope: an opening role chunk, one chunk
+ per delta on `choices[0]`, a `tool_calls` finish, and a final usage chunk."
+ [model deltas]
+ (concat [{:id "chatcmpl-synthetic"
+ :model model
+ :choices [{:index 0 :delta {:role "assistant" :content ""} :finish_reason nil}]}]
+ (for [delta deltas]
+ {:choices [{:index 0 :delta delta :finish_reason nil}]})
+ [{:choices [{:index 0 :delta {} :finish_reason "tool_calls"}]}
+ {:choices [] :usage {:prompt_tokens 100 :completion_tokens 20}}]))
+
+(def ^:private search-call
+ {:function "search" :arguments {:query "revenue"}})
+
+(def ^:private get-time-call
+ {:function "get_time" :arguments {:tz "UTC"}})
+
+(def ^:private tool-call-fixtures
+ "Synthetic streams keyed by the delta shape they model, each with the tool calls it must assemble into."
+ {"vLLM / Qwen: index-keyed, id never sent"
+ {:stream (synthetic-stream
+ "Qwen/Qwen3-32B"
+ [{:tool_calls [{:index 0 :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:index 0 :function {:arguments "{\"query\":"}}]}
+ {:tool_calls [{:index 0 :function {:arguments " \"revenue\"}"}}]}])
+ :expected [(assoc search-call :id string?)]}
+
+ "vLLM / Qwen: two index-keyed calls, id never sent"
+ {:stream (synthetic-stream
+ "Qwen/Qwen3-32B"
+ [{:tool_calls [{:index 0 :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:index 0 :function {:arguments "{\"query\": \"revenue\"}"}}]}
+ {:tool_calls [{:index 1 :type "function" :function {:name "get_time" :arguments ""}}]}
+ {:tool_calls [{:index 1 :function {:arguments "{\"tz\": \"UTC\"}"}}]}])
+ :expected [(assoc search-call :id string?) (assoc get-time-call :id string?)]}
+
+ "vLLM / Qwen: id arrives after the opening delta"
+ {:stream (synthetic-stream
+ "Qwen/Qwen3-32B"
+ [{:tool_calls [{:index 0 :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:index 0 :id "chatcmpl-tool-7f3a" :function {:arguments "{\"query\":"}}]}
+ {:tool_calls [{:index 0 :function {:arguments " \"revenue\"}"}}]}])
+ :expected [(assoc search-call :id "chatcmpl-tool-7f3a")]}
+
+ "GLM: id repeated on every delta"
+ {:stream (synthetic-stream
+ "glm-4.6"
+ [{:tool_calls [{:index 0 :id "call_9a1" :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:index 0 :id "call_9a1" :type "function" :function {:arguments "{\"query\":"}}]}
+ {:tool_calls [{:index 0 :id "call_9a1" :type "function" :function {:arguments " \"revenue\"}"}}]}])
+ :expected [(assoc search-call :id "call_9a1")]}
+
+ "GLM: two whole calls in one delta"
+ {:stream (synthetic-stream
+ "glm-4.6"
+ [{:tool_calls [{:index 0
+ :id "call_a"
+ :type "function"
+ :function {:name "search" :arguments "{\"query\": \"revenue\"}"}}
+ {:index 1
+ :id "call_b"
+ :type "function"
+ :function {:name "get_time" :arguments "{\"tz\": \"UTC\"}"}}]}])
+ :expected [(assoc search-call :id "call_a") (assoc get-time-call :id "call_b")]}
+
+ "DeepSeek via OpenRouter: deltas of two calls interleaved"
+ {:stream (synthetic-stream
+ "deepseek/deepseek-v4-pro"
+ [{:tool_calls [{:index 0 :id "call_00_a" :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:index 1 :id "call_01_b" :type "function" :function {:name "get_time" :arguments ""}}]}
+ {:tool_calls [{:index 0 :function {:arguments "{\"query\":"}}]}
+ {:tool_calls [{:index 1 :function {:arguments "{\"tz\":"}}]}
+ {:tool_calls [{:index 0 :function {:arguments " \"revenue\"}"}}]}
+ {:tool_calls [{:index 1 :function {:arguments " \"UTC\"}"}}]}])
+ :expected [(assoc search-call :id "call_00_a") (assoc get-time-call :id "call_01_b")]}
+
+ "DeepSeek via OpenRouter: reasoning between a call's deltas"
+ {:stream (synthetic-stream
+ "deepseek/deepseek-v4-pro"
+ [{:reasoning "I should search."}
+ {:tool_calls [{:index 0 :id "call_00_a" :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:index 0 :function {:arguments "{\"query\":"}}]}
+ {:reasoning " Revenue it is."}
+ {:content "\n"}
+ {:tool_calls [{:index 0 :function {:arguments " \"revenue\"}"}}]}])
+ :expected [(assoc search-call :id "call_00_a")]}
+
+ "one index reused for calls with distinct ids"
+ {:stream (synthetic-stream
+ "some-model"
+ [{:tool_calls [{:index 0
+ :id "call_a"
+ :type "function"
+ :function {:name "search" :arguments "{\"query\": \"revenue\"}"}}]}
+ {:tool_calls [{:index 0
+ :id "call_b"
+ :type "function"
+ :function {:name "get_time" :arguments "{\"tz\": \"UTC\"}"}}]}])
+ :expected [(assoc search-call :id "call_a") (assoc get-time-call :id "call_b")]}
+
+ "id-keyed, no index"
+ {:stream (synthetic-stream
+ "some-model"
+ [{:tool_calls [{:id "call_a" :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:function {:arguments "{\"query\": \"revenue\"}"}}]}
+ {:tool_calls [{:id "call_b" :type "function" :function {:name "get_time" :arguments ""}}]}
+ {:tool_calls [{:function {:arguments "{\"tz\": \"UTC\"}"}}]}])
+ :expected [(assoc search-call :id "call_a") (assoc get-time-call :id "call_b")]}
+
+ "blank id on continuation deltas"
+ {:stream (synthetic-stream
+ "some-model"
+ [{:tool_calls [{:index 0 :id "call_a" :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:index 0 :id "" :function {:arguments "{\"query\":"}}]}
+ {:tool_calls [{:index 0 :id "" :function {:arguments " \"revenue\"}"}}]}])
+ :expected [(assoc search-call :id "call_a")]}
+
+ "index on the opening delta only, id on every delta"
+ {:stream (synthetic-stream
+ "some-model"
+ [{:tool_calls [{:index 0 :id "call_a" :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:id "call_a" :function {:arguments "{\"query\": \"revenue\"}"}}]}])
+ :expected [(assoc search-call :id "call_a")]}})
+
+(defn- assembled-tool-calls
+ [chunks-xf parts-xf stream]
+ (into []
+ (comp chunks-xf
+ parts-xf
+ (filter #(= :tool-input (:type %)))
+ (map #(select-keys % [:id :function :arguments])))
+ stream))
+
+(deftest ^:parallel chunks-xf-assembles-tool-calls-test
+ (doseq [[shape {:keys [stream expected]}] tool-call-fixtures
+ [adapter chunks-xf] [["openrouter" (openrouter/openrouter->aisdk-chunks-xf)]
+ ["vllm" (vllm/vllm->aisdk-chunks-xf)]
+ ["zai" (zai/zai->aisdk-chunks-xf)]
+ ["moonshot" (moonshot/moonshot->aisdk-chunks-xf)]
+ ["mistral" (mistral/mistral->aisdk-chunks-xf)]]
+ [mode parts-xf] [["aisdk-xf" (self.core/aisdk-xf)]
+ ["lite-aisdk-xf" (self.core/lite-aisdk-xf)]]]
+ (testing (str shape " through " adapter " and " mode)
+ (is (=? expected (assembled-tool-calls chunks-xf parts-xf stream))))))
+
+(deftest ^:parallel chunks-xf-tool-call-chunks-are-not-interleaved-test
+ (testing "each tool call's chunks are emitted together, in arrival order, with joined arguments, at finish"
+ (is (= [{:type :tool-input-start :toolCallId "call_00_a" :toolName "search"}
+ {:type :tool-input-delta :toolCallId "call_00_a" :inputTextDelta "{\"query\": \"revenue\"}"}
+ {:type :tool-input-available :toolCallId "call_00_a" :toolName "search"}
+ {:type :tool-input-start :toolCallId "call_01_b" :toolName "get_time"}
+ {:type :tool-input-delta :toolCallId "call_01_b" :inputTextDelta "{\"tz\": \"UTC\"}"}
+ {:type :tool-input-available :toolCallId "call_01_b" :toolName "get_time"}]
+ (into []
+ (comp (chat-completions/chat-completions->aisdk-chunks-xf)
+ (filter #(#{:tool-input-start :tool-input-delta :tool-input-available} (:type %))))
+ (:stream (tool-call-fixtures "DeepSeek via OpenRouter: deltas of two calls interleaved")))))))
+
+(deftest ^:parallel chunks-xf-tool-call-emission-stops-when-reduced-test
+ (testing "a consumer that stops early receives no more tool-call chunks, and the result stays reduced"
+ (let [seen (atom [])]
+ (is (= [:start :tool-input-start]
+ (into []
+ (comp (chat-completions/chat-completions->aisdk-chunks-xf)
+ (map (fn [chunk] (swap! seen conj (:type chunk)) chunk))
+ (take 2)
+ (map :type))
+ (:stream (tool-call-fixtures "GLM: two whole calls in one delta")))))
+ (is (= [:start :tool-input-start] @seen)))))
+
+(deftest ^:parallel chunks-xf-duplicate-tool-call-id-fails-test
+ (testing "two calls with one id fail with an error naming the id, and emit no tool call"
+ (let [emitted (atom [])]
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Model stream sent the tool call id \"call_0\" for 2 different calls"
+ (into []
+ (comp (chat-completions/chat-completions->aisdk-chunks-xf)
+ (map (fn [chunk] (swap! emitted conj (:type chunk)) chunk)))
+ (synthetic-stream
+ "some-model"
+ [{:tool_calls [{:index 0
+ :id "call_0"
+ :type "function"
+ :function {:name "search" :arguments "{\"query\": \"revenue\"}"}}]}
+ {:tool_calls [{:index 1
+ :id "call_0"
+ :type "function"
+ :function {:name "get_time" :arguments "{\"tz\": \"UTC\"}"}}]}]))))
+ (is (= [:start] @emitted)))))
+
+(deftest ^:parallel chunks-xf-nameless-tool-call-is-dropped-test
+ (testing "a tool call whose name never arrives emits no chunks, and the named call beside it survives"
+ (is (=? [(assoc search-call :id "call_a")]
+ (assembled-tool-calls
+ (chat-completions/chat-completions->aisdk-chunks-xf)
+ (self.core/aisdk-xf)
+ (synthetic-stream
+ "some-model"
+ [{:tool_calls [{:index 0 :id "call_a" :type "function" :function {:name "search" :arguments ""}}]}
+ {:tool_calls [{:index 1 :id "call_b" :type "function" :function {:arguments "{}"}}]}
+ {:tool_calls [{:index 0 :function {:arguments "{\"query\": \"revenue\"}"}}]}]))))))
+
(deftest ^:parallel chunks-xf-combined-reasoning-and-tool-call-delta-test
(testing "a delta carrying both reasoning and a tool call keeps the tool call"
- ;; The tool call's opening chunk is the only one carrying its id and name — classifying the
- ;; delta as :reasoning would lose the call entirely and break the tool loop. The delta's
- ;; reasoning fragment is dropped instead: display text, recoverable.
+ ;; tool_calls outrank reasoning, so the delta's reasoning fragment is dropped: display text, recoverable.
(is (= [{:type :start :messageId "chatcmpl-9"}
{:type :tool-input-start :toolCallId "call-1" :toolName "get_weather"}
- {:type :tool-input-delta :toolCallId "call-1" :inputTextDelta "{\"city\""}
- {:type :tool-input-delta :toolCallId "call-1" :inputTextDelta ": \"Berlin\"}"}
+ {:type :tool-input-delta :toolCallId "call-1" :inputTextDelta "{\"city\": \"Berlin\"}"}
{:type :tool-input-available :toolCallId "call-1" :toolName "get_weather"}]
(into [] (chat-completions/chat-completions->aisdk-chunks-xf
chat-completions/stop-reasons
@@ -416,8 +627,7 @@
(deftest ^:parallel chunks-xf-reasoning-deltas-open-no-text-block-test
(testing "reasoning_content deltas and empty-string content produce no chunks"
;; Without `:forward-reasoning?` reasoning deltas are dropped rather than surfaced as text.
- ;; An empty-string `content` between blocks must not open a text block either — that would close
- ;; the tool call that follows it.
+ ;; An empty-string `content` between blocks must not open a spurious text block either.
(is (= [:start :tool-input-start :tool-input-delta :tool-input-available :usage]
(chunk-types
[{:id "chatcmpl-4"
@@ -475,10 +685,8 @@
:model "kimi-k2.6"
:choices [{:index 0 :delta {:role "assistant" :content "Hel"} :finish_reason nil}]}
error-chunk]))))
- (testing "without making a half-streamed tool call available to run"
+ (testing "without emitting any chunk of a half-streamed tool call"
(is (= [{:type :start :messageId "chatcmpl-5"}
- {:type :tool-input-start :toolCallId "call-1" :toolName "search"}
- {:type :tool-input-delta :toolCallId "call-1" :inputTextDelta "{\"query\": \"rev"}
{:type :error :errorText message}]
(into [] xf
[{:id "chatcmpl-5"
diff --git a/test/metabase/metabot/self_test.clj b/test/metabase/metabot/self_test.clj
index ab79a191dbb..5e7d756ab7a 100644
--- a/test/metabase/metabot/self_test.clj
+++ b/test/metabase/metabot/self_test.clj
@@ -525,6 +525,19 @@
{:type :reasoning-delta :id "r1" :delta "c"}
{:type :reasoning-end :id "r1" :providerMetadata {:anthropic {:signature "sig"}}}])))))
+(deftest ^:parallel aisdk-xf-orphan-chunk-test
+ (testing "a group that does not open with its start chunk fails with an error naming the chunk"
+ (doseq [[mode xf] [["aisdk-xf" (self.core/aisdk-xf)]
+ ["lite-aisdk-xf" (self.core/lite-aisdk-xf)]]]
+ (testing mode
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Model stream sent a :tool-input-delta chunk for \"call-1\" without its start chunk"
+ (into [] xf
+ [{:type :start :messageId "msg-1"}
+ {:type :tool-input-delta :toolCallId "call-1" :inputTextDelta "{}"}
+ {:type :tool-input-available :toolCallId "call-1" :toolName "search"}])))))))
+
;;; tool executor
(deftest ^:parallel tool-executor-xf-test