A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: anthropic reasoning models for temperature fixes (#7310)
Myles Scolnickb991241c3bcb
diff --git a/examples/ai/chat/anthropic_example.py b/examples/ai/chat/anthropic_example.pyindex 0d6b66da21..1287d8a626 100644--- a/examples/ai/chat/anthropic_example.py+++ b/examples/ai/chat/anthropic_example.py@@ -8,7 +8,7 @@import marimo-__generated_with = "0.17.8"+__generated_with = "0.18.0"app = marimo.App(width="medium")@@ -53,7 +53,7 @@ def _(input_key, mo, os_key):def _(key, mo):chatbot = mo.ui.chat(mo.ai.llm.anthropic(- "claude-3-7-sonnet-20250219",+ "claude-sonnet-4-5",system_message="You are a helpful assistant.",api_key=key,),diff --git a/marimo/_ai/_convert.py b/marimo/_ai/_convert.pyindex 6cc341326c..fbc0e85067 100644--- a/marimo/_ai/_convert.py+++ b/marimo/_ai/_convert.py@@ -203,6 +203,9 @@ def convert_to_anthropic_messages()if not message.parts:+ if not message.content:+ continue+# Convert content to stringtext_block: TextBlockParam = {"type": "text",@@ -222,12 +225,18 @@ def convert_to_anthropic_messages(for part in message.parts:if isinstance(part, TextPart):+ if not part.text:+ continue+text_part: TextBlockParam = {"type": "text","text": part.text,}current_parts.append(text_part)elif isinstance(part, ReasoningPart):+ if not part.text:+ continue+signature = ""if part.details and len(part.details) > 0:signature = part.details[0].signature or ""diff --git a/marimo/_ai/llm/_impl.py b/marimo/_ai/llm/_impl.pyindex 8eff9bdced..c70e1c4a32 100644--- a/marimo/_ai/llm/_impl.py+++ b/marimo/_ai/llm/_impl.py@@ -208,6 +208,10 @@ class anthropic(ChatModel):base_url: The base URL to use"""+ def supports_temperature(self, model: str) -> bool:+ # Reasoning models (>4.0) don't support temperature+ return model.startswith("claude-3")+def __init__(self,model: str,@@ -282,7 +286,9 @@ class anthropic(ChatModel):params["top_p"] = config.top_pif config.top_k is not None:params["top_k"] = config.top_k- if config.temperature is not None:+ if config.temperature is not None and self.supports_temperature(+ self.model+ ):params["temperature"] = config.temperature# Try streaming first, fall back to non-streaming on errordiff --git a/tests/_ai/llm/test_impl.py b/tests/_ai/llm/test_impl.pyindex 60b4da9c17..40cfd23081 100644--- a/tests/_ai/llm/test_impl.py+++ b/tests/_ai/llm/test_impl.py@@ -943,6 +943,57 @@ class TestAnthropic:result = result_list[-1] if result_list else ""assert result == ""+ def test_supports_temperature(self) -> None:+ """Test supports_temperature method."""+ model = anthropic("claude-3-opus-20240229")+ assert model.supports_temperature("claude-3-opus-20240229") is True+ assert model.supports_temperature("claude-3-sonnet-20240229") is True+ assert model.supports_temperature("claude-3-haiku-20240307") is True++ # Reasoning models (>4.0) don't support temperature+ assert model.supports_temperature("claude-sonnet-4-5") is False+ assert model.supports_temperature("claude-opus-4-5") is False+ assert model.supports_temperature("claude-4-opus") is False++ @patch.object(anthropic, "_require_api_key")+ @patch("anthropic.Anthropic")+ def test_call_without_temperature_for_reasoning_model(+ self, mock_anthropic_class: MagicMock, mock_require_api_key: MagicMock+ ) -> None:+ """Test that temperature is not included for reasoning models."""+ mock_require_api_key.return_value = "test-key"+ mock_client = MagicMock()+ mock_anthropic_class.return_value = mock_client++ # Setup streaming response+ mock_stream = MagicMock()+ mock_stream.__enter__ = MagicMock(return_value=mock_stream)+ mock_stream.__exit__ = MagicMock(return_value=None)+ mock_stream.text_stream = ["Test response"]+ mock_client.messages.stream.return_value = mock_stream++ model = anthropic("claude-sonnet-4-5")+ messages = [ChatMessage(role="user", content="Test prompt")]+ config = ChatModelConfig(+ max_tokens=100,+ temperature=0.7,+ top_p=0.9,+ top_k=10,+ )++ result_gen = model(messages, config)+ list(result_gen) # Consume the generator++ mock_client.messages.stream.assert_called_once()+ call_args = mock_client.messages.stream.call_args[1]+ assert call_args["model"] == "claude-sonnet-4-5"+ assert call_args["system"] == DEFAULT_SYSTEM_MESSAGE+ assert call_args["max_tokens"] == 100+ # Temperature should not be included for reasoning models+ assert "temperature" not in call_args+ assert call_args["top_p"] == 0.9+ assert call_args["top_k"] == 10+@patch.dict(os.environ, {"ANTHROPIC_API_KEY": "env-key"})def test_require_api_key_env(self) -> None:"""Test _require_api_key with environment variable."""diff --git a/tests/_ai/test_chat_convert.py b/tests/_ai/test_chat_convert.pyindex 1662a3f0d2..f1822dff87 100644--- a/tests/_ai/test_chat_convert.py+++ b/tests/_ai/test_chat_convert.py@@ -693,6 +693,93 @@ class TestAnthropic:"content": [{"type": "text", "text": "Hi there!"}],}+ def test_convert_to_anthropic_messages_empty_content(self):+ """Test converting messages with empty content."""+ messages = [+ ChatMessage(+ role="user",+ content="",+ parts=None,+ ),+ ChatMessage(+ role="assistant",+ content="Hi there!",+ parts=None,+ ),+ ]++ result = convert_to_anthropic_messages(messages)++ # Empty content message should be skipped+ assert len(result) == 1+ assert result[0] == {+ "role": "assistant",+ "content": [{"type": "text", "text": "Hi there!"}],+ }++ def test_convert_to_anthropic_messages_empty_text_parts(self):+ """Test converting messages with empty text parts."""+ messages = [+ ChatMessage(+ role="user",+ content="Hello",+ parts=[+ TextPart(type="text", text="Hello"),+ TextPart(type="text", text=""), # Empty text part+ TextPart(type="text", text="World"),+ TextPart(type="text", text=""), # Another empty text part+ ],+ ),+ ]++ result = convert_to_anthropic_messages(messages)++ # Empty text parts should be skipped+ assert result == [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "Hello"},+ {"type": "text", "text": "World"},+ ],+ },+ ]++ def test_convert_to_anthropic_messages_empty_reasoning_parts(self):+ """Test converting messages with empty reasoning parts."""+ messages = [+ ChatMessage(+ role="assistant",+ content="",+ parts=[+ ReasoningPart(+ type="reasoning",+ text="Valid reasoning",+ details=[],+ ),+ ReasoningPart(+ type="reasoning",+ text="", # Empty reasoning part+ details=[],+ ),+ TextPart(type="text", text="Response"),+ ],+ ),+ ]++ result = convert_to_anthropic_messages(messages)++ # Empty reasoning parts should be skipped+ assert len(result) == 1+ assert result[0]["role"] == "assistant"+ assert len(result[0]["content"]) == 2+ assert result[0]["content"][0] == {+ "type": "thinking",+ "thinking": "Valid reasoning",+ "signature": "",+ }+ assert result[0]["content"][1] == {"type": "text", "text": "Response"}+def test_convert_to_anthropic_messages_file_part(self):"""Test converting messages with file parts."""messages = [