Skip to content

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

Claude Sonnet 5 support (#76756)

applebyd3ee87fab5e5
diff --git a/src/metabase/metabot/self/claude.clj b/src/metabase/metabot/self/claude.clj
index aa596230a14..54401743e25 100644
--- a/src/metabase/metabot/self/claude.clj
+++ b/src/metabase/metabot/self/claude.clj
@@ -51,6 +51,12 @@
:cacheCreationTokens (:cache_creation_input_tokens u 0)
:cacheReadTokens (:cache_read_input_tokens u 0)})
+(def ^:private translated-chunk-type?
+ "Claude content-block types we translate into AI SDK chunks. Other block types
+ (e.g. `thinking`/`redacted_thinking` from extended or adaptive thinking) are
+ ignored, mirroring the OpenAI adapter's handling of reasoning output."
+ #{:text :tool_use})
+
(defn claude->aisdk-chunks-xf
"Translates Claude /v1/messages streaming events into AI SDK v5 protocol chunks.
@@ -82,10 +88,14 @@
;; usage in the completion arity so we don't lose data entirely.
last-usage (volatile! nil)
close! (fn [result]
- (u/prog1 (rf result (merge {:type (case @current-type
- :text :text-end
- :tool_use :tool-input-available)}
- @payload))
+ ;; only emit an end marker for block types we translate; thinking and
+ ;; other untranslated blocks are ignored.
+ (u/prog1 (if-let [end-type (case @current-type
+ :text :text-end
+ :tool_use :tool-input-available
+ nil)]
+ (rf result (merge {:type end-type} @payload))
+ result)
(vreset! current-type nil)
(vreset! current-id nil)
(vreset! payload {})))]
@@ -121,19 +131,24 @@
:tool_use {:toolCallId chunk-id
:toolName (:name content_block)}
nil)))
- (rf (merge (case block-type
- :text {:type :text-start}
- :tool_use {:type :tool-input-start})
- @payload)))
-
- ;; content block delta
- (= t "content_block_delta") (rf (case (:type delta)
- "text_delta" {:type :text-delta
- :id (:id @payload)
- :delta (:text delta)}
- "input_json_delta" {:type :tool-input-delta
- :toolCallId (:toolCallId @payload)
- :inputTextDelta (:partial_json delta)}))
+ (cond->
+ (translated-chunk-type? block-type)
+ (rf (merge (case block-type
+ :text {:type :text-start}
+ :tool_use {:type :tool-input-start})
+ @payload))))
+
+ ;; content block delta — ignore deltas for blocks we don't translate
+ ;; (e.g. thinking_delta / signature_delta from extended thinking)
+ (and (= t "content_block_delta")
+ (contains? #{"text_delta" "input_json_delta"} (:type delta)))
+ (rf (case (:type delta)
+ "text_delta" {:type :text-delta
+ :id (:id @payload)
+ :delta (:text delta)}
+ "input_json_delta" {:type :tool-input-delta
+ :toolCallId (:toolCallId @payload)
+ :inputTextDelta (:partial_json delta)}))
;; end of content block
(= t "content_block_stop") (close!)
@@ -300,16 +315,18 @@
(defn- model-supports-temperature?
"Whether `model` accepts an explicit `temperature` parameter.
- Sampling parameters (`temperature`, `top_p`, `top_k`) were removed starting with Claude Opus 4.7. Strips an
- optional vendor prefix (e.g. Bedrock's `anthropic.`) before checking."
+ Sampling parameters (`temperature`, `top_p`, `top_k`) were removed starting with Claude Opus 4.7 and Sonnet 5.
+ Strips an optional vendor prefix (e.g. Bedrock's `anthropic.`) before checking."
[model]
(let [model (str/replace-first (str model) #"^anthropic\." "")]
(not (or (str/starts-with? model "claude-fable")
- (when-let [[_ major minor] (re-find #"^claude-opus-(\d+)(?:-(\d+))?" model)]
+ (when-let [[_ family major minor] (re-find #"^claude-(opus|sonnet)-(\d+)(?:-(\d+))?" model)]
(let [major (parse-long major)
minor (or (some-> minor parse-long) 0)]
- (or (> major 4)
- (and (= major 4) (>= minor 7)))))))))
+ (case family
+ "opus" (or (> major 4)
+ (and (= major 4) (>= minor 7)))
+ "sonnet" (>= major 5))))))))
(mu/defn claude-request-body
"Build the Anthropic Messages API request body for an LLM request."
diff --git a/test/metabase/metabot/self/claude_test.clj b/test/metabase/metabot/self/claude_test.clj
index d23cc76939c..602cd000d9c 100644
--- a/test/metabase/metabot/self/claude_test.clj
+++ b/test/metabase/metabot/self/claude_test.clj
@@ -149,6 +149,46 @@
(is (= {:cacheCreationTokens 0 :cacheReadTokens 0}
(select-keys (:usage usage) [:cacheCreationTokens :cacheReadTokens]))))))
+(deftest ^:parallel claude-thinking-blocks-ignored-test
+ (testing "thinking content blocks (extended/adaptive thinking, e.g. Claude Sonnet 5) are ignored"
+ (let [events [{:type "message_start"
+ :message {:id "msg-1" :model "claude-sonnet-5"
+ :usage {:input_tokens 10 :output_tokens 0}}}
+ ;; a thinking block arrives before the text
+ {:type "content_block_start" :index 0 :content_block {:type "thinking"}}
+ {:type "content_block_delta" :index 0 :delta {:type "thinking_delta" :thinking "let me think"}}
+ {:type "content_block_delta" :index 0 :delta {:type "signature_delta" :signature "abc"}}
+ {:type "content_block_stop" :index 0}
+ {:type "content_block_start" :index 1 :content_block {:type "text" :id "text-1"}}
+ {:type "content_block_delta" :index 1 :delta {:type "text_delta" :text "hi"}}
+ {:type "content_block_stop" :index 1}
+ {:type "message_delta" :delta {:stop_reason "end_turn"}
+ :usage {:input_tokens 10 :output_tokens 5}}
+ {:type "message_stop"}]]
+ (testing "no thinking/reasoning chunks are emitted; only text + usage survive"
+ (is (=? [{:type :start} {:type :text-start} {:type :text-delta} {:type :text-end} {:type :usage}]
+ (into []
+ (comp (claude/claude->aisdk-chunks-xf)
+ (m/distinct-by :type))
+ events))))
+ (testing "through the full pipeline produces text + usage"
+ (is (=? [{:type :start} {:type :text :text "hi"} {:type :usage}]
+ (into []
+ (comp (claude/claude->aisdk-chunks-xf)
+ (self.core/aisdk-xf))
+ events))))))
+ (testing "a stream that ends mid-thinking flushes usage without emitting a thinking part"
+ (let [events [{:type "message_start"
+ :message {:id "msg-2" :model "claude-sonnet-5"
+ :usage {:input_tokens 10 :output_tokens 0}}}
+ {:type "content_block_start" :index 0 :content_block {:type "thinking"}}
+ {:type "content_block_delta" :index 0 :delta {:type "thinking_delta" :thinking "partial"}}]]
+ (is (=? [{:type :start} {:type :usage}]
+ (into []
+ (comp (claude/claude->aisdk-chunks-xf)
+ (m/distinct-by :type))
+ events))))))
+
;;; ──────────────────────────────────────────────────────────────────
;;; parts->claude-messages tests
;;; ──────────────────────────────────────────────────────────────────
@@ -419,9 +459,11 @@
"claude-opus-4-5" "claude-opus-4-6" "claude-opus-4-1"]]
(is (true? (#'claude/model-supports-temperature? model))
model)))
- (testing "sampling parameters were removed starting with Opus 4.7 and on Fable models"
+ (testing "sampling parameters were removed starting with Opus 4.7, Sonnet 5, and on Fable models"
(doseq [model ["claude-opus-4-7" "claude-opus-4-8" "claude-opus-4-8-20260415"
- "claude-opus-5" "claude-opus-5-0" "claude-fable-5"]]
+ "claude-opus-5" "claude-opus-5-0"
+ "claude-sonnet-5" "claude-sonnet-5-0" "claude-sonnet-6"
+ "claude-fable-5"]]
(is (false? (#'claude/model-supports-temperature? model))
model))))
@@ -440,4 +482,5 @@
(is (= 0.3 (:temperature (request-body "claude-haiku-4-5")))))
(testing "temperature is omitted for models that reject sampling parameters"
(is (not (contains? (request-body "claude-opus-4-8") :temperature)))
+ (is (not (contains? (request-body "claude-sonnet-5") :temperature)))
(is (not (contains? (request-body "anthropic.claude-opus-4-8") :temperature))))))