Skip to content

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.

fix: remove tools if empty (#6109)

Myles Scolnick11310f6461eb
diff --git a/marimo/_server/ai/config.py b/marimo/_server/ai/config.py
index b621907394..b01a9ef4f0 100644
--- a/marimo/_server/ai/config.py
+++ b/marimo/_server/ai/config.py
@@ -1,7 +1,7 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from dataclasses import dataclass, field
+from dataclasses import dataclass
from typing import (
Any,
Optional,
@@ -32,7 +32,14 @@ class AnyProviderConfig:
ssl_verify: Optional[bool] = None
ca_bundle_path: Optional[str] = None
client_pem: Optional[str] = None
- tools: list[Tool] = field(default_factory=list)
+ tools: Optional[list[Tool]] = None
+
+ def __post_init__(self) -> None:
+ # Only include tools if they are available
+ # Empty tools list causes an error with deepseek
+ # https://discord.com/channels/1059888774789730424/1387766267792068821
+ if not self.tools:
+ self.tools = None
@classmethod
def for_openai(cls, config: AiConfig) -> AnyProviderConfig:
@@ -96,15 +103,9 @@ class AnyProviderConfig:
"ssl_verify": ai_config.get("ssl_verify", True),
"ca_bundle_path": ai_config.get("ca_bundle_path", None),
"client_pem": ai_config.get("client_pem", None),
+ "tools": _get_tools(config.get("mode", "manual")),
}
- # Only include tools if they are available
- # Empty tools list causes an error with deepseek
- # https://discord.com/channels/1059888774789730424/1387766267792068821
- tools = _get_tools(config.get("mode", "manual"))
- if len(tools) > 0:
- kwargs["tools"] = tools
-
return AnyProviderConfig(**kwargs)
@classmethod
diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.py
index 443971b085..f720c50719 100644
--- a/marimo/_server/ai/providers.py
+++ b/marimo/_server/ai/providers.py
@@ -410,6 +410,7 @@ class OpenAIProvider(
max_tokens: int,
) -> OpenAiStream[ChatCompletionChunk]:
client = self.get_client(self.config)
+ tools = self.config.tools
create_params = {
"model": self.model,
"messages": cast(
@@ -423,7 +424,7 @@ class OpenAIProvider(
),
"stream": True,
"timeout": 15,
- "tools": convert_to_openai_tools(self.config.tools),
+ "tools": convert_to_openai_tools(tools) if tools else None,
}
if self._is_reasoning_model(self.model):
create_params["reasoning_effort"] = self.DEFAULT_REASONING_EFFORT
@@ -580,6 +581,7 @@ class AnthropicProvider(
max_tokens: int,
) -> AnthropicStream[RawMessageStreamEvent]:
client = self.get_client(self.config)
+ tools = self.config.tools
create_params = {
"model": self.model,
"max_tokens": max_tokens,
@@ -587,7 +589,7 @@ class AnthropicProvider(
Any,
convert_to_anthropic_messages(messages),
),
- "tools": convert_to_anthropic_tools(self.config.tools),
+ "tools": convert_to_anthropic_tools(tools) if tools else None,
"system": system_prompt,
"stream": True,
"temperature": self.get_temperature(),
@@ -682,11 +684,12 @@ class GoogleProvider(
def get_config(
self, system_prompt: str, max_tokens: int
) -> GenerateContentConfig:
+ tools = self.config.tools
config = {
"system_instruction": system_prompt,
"temperature": 0,
"max_output_tokens": max_tokens,
- "tools": convert_to_google_tools(self.config.tools),
+ "tools": convert_to_google_tools(tools) if tools else None,
}
if self.is_thinking_model(self.model):
config["thinking_config"] = {
@@ -822,6 +825,7 @@ class BedrockProvider(
from litellm import acompletion as litellm_completion
self.setup_credentials(self.config)
+ tools = self.config.tools
return await litellm_completion(
model=self.model,
@@ -835,7 +839,7 @@ class BedrockProvider(
max_completion_tokens=max_tokens,
stream=True,
timeout=15,
- tools=convert_to_openai_tools(self.config.tools),
+ tools=convert_to_openai_tools(tools) if tools else None,
)
def extract_content(
diff --git a/tests/_server/ai/test_ai_config.py b/tests/_server/ai/test_ai_config.py
index 4c86b85818..0960ba8670 100644
--- a/tests/_server/ai/test_ai_config.py
+++ b/tests/_server/ai/test_ai_config.py
@@ -314,7 +314,7 @@ class TestAnyProviderConfig:
provider_config = AnyProviderConfig.for_openai(config)
- assert provider_config.tools == []
+ assert provider_config.tools is None
class TestOsKey:
@@ -749,6 +749,17 @@ class TestGetAiConfig:
assert result == {}
+ def test_get_ai_config_empty_tools(self):
+ """Test that _get_ai_config returns empty dict when AI config key is missing."""
+ config: AiConfig = {
+ "open_ai": {"api_key": "test-key"},
+ "mode": "manual",
+ }
+
+ result = _get_ai_config(config, "open_ai")
+
+ assert result == {"api_key": "test-key"}
+
class TestUtilityFunctions:
"""Tests for utility functions."""
@@ -919,7 +930,7 @@ class TestUtilityFunctions:
)
assert provider_config.api_key == "test-key"
- assert provider_config.tools == [] # Autocomplete should have no tools
+ assert provider_config.tools is None
class TestEdgeCases:
@@ -1009,3 +1020,12 @@ class TestEdgeCases:
assert exc_info.value.status_code == HTTPStatus.BAD_REQUEST
assert "GitHub API key not configured" in str(exc_info.value.detail)
+
+ def test_tools_empty_list(self):
+ """Test that tools are not included when empty list."""
+ provider_config = AnyProviderConfig(
+ tools=[],
+ api_key="test-key",
+ base_url="test-base-url",
+ )
+ assert provider_config.tools is None