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: only use max_completion_tokens on reasoning models (#6082)
Myles Scolnick282dd7125f98
diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.pyindex 89f9bcb4c6..443971b085 100644--- a/marimo/_server/ai/providers.py+++ b/marimo/_server/ai/providers.py@@ -327,7 +327,7 @@ class OpenAIProvider(def _is_reasoning_model(self, model: str) -> bool:# only o-series models support reasoning- return model.startswith("o")+ return model.startswith("o") or model.startswith("gpt-5")def get_client(self, config: AnyProviderConfig) -> AsyncOpenAI:DependencyManager.openai.require(why="for AI assistance with OpenAI")@@ -421,13 +421,15 @@ class OpenAIProvider(+ messages),),- "max_completion_tokens": max_tokens,"stream": True,"timeout": 15,"tools": convert_to_openai_tools(self.config.tools),}if self._is_reasoning_model(self.model):create_params["reasoning_effort"] = self.DEFAULT_REASONING_EFFORT+ create_params["max_completion_tokens"] = max_tokens+ else:+ create_params["max_tokens"] = max_tokensreturn cast("OpenAiStream[ChatCompletionChunk]",await client.chat.completions.create(**create_params),diff --git a/tests/_server/ai/test_providers.py b/tests/_server/ai/test_providers.pyindex 9381db056b..cca1ded852 100644--- a/tests/_server/ai/test_providers.py+++ b/tests/_server/ai/test_providers.py@@ -1,7 +1,10 @@"""Tests for the LLM providers in marimo._server.ai.providers."""+from unittest.mock import AsyncMock, patch+import pytest+from marimo._ai._types import ChatMessagefrom marimo._config.config import AiConfigfrom marimo._server.ai.providers import (AnthropicProvider,@@ -72,3 +75,71 @@ def test_get_completion_provider(model_name: str, provider_type: type) -> None:config = AnyProviderConfig(api_key="test-key", base_url="http://test-url")provider = get_completion_provider(config, model_name)assert isinstance(provider, provider_type)+++ ("model_name", "expected_params"),+ [+ pytest.param(+ "o1-mini",+ {"max_completion_tokens": 1000, "reasoning_effort": "medium"},+ id="reasoning_model_o1_mini",+ ),+ pytest.param(+ "o1-preview",+ {"max_completion_tokens": 1000, "reasoning_effort": "medium"},+ id="reasoning_model_o1_preview",+ ),+ pytest.param(+ "gpt-4",+ {"max_tokens": 1000},+ id="non_reasoning_model_gpt4",+ ),+ pytest.param(+ "gpt-3.5-turbo",+ {"max_tokens": 1000},+ id="non_reasoning_model_gpt35",+ ),+ ],+)+@patch("openai.AsyncOpenAI")+async def test_openai_provider_max_tokens_parameter(+ mock_openai_class, model_name: str, expected_params: dict+) -> None:+ """Test that OpenAI provider uses correct token parameter for reasoning vs non-reasoning models."""+ # Setup mock+ mock_client = AsyncMock()+ mock_openai_class.return_value = mock_client+ mock_stream = AsyncMock()+ mock_client.chat.completions.create.return_value = mock_stream++ # Create provider+ config = AnyProviderConfig(+ api_key="test-key", base_url="https://api.openai.com/v1"+ )+ provider = OpenAIProvider(model_name, config)++ # Call stream_completion+ messages = [ChatMessage(role="user", content="test message")]+ await provider.stream_completion(messages, "system prompt", 1000)++ # Verify the correct parameters were passed+ mock_client.chat.completions.create.assert_called_once()+ call_kwargs = mock_client.chat.completions.create.call_args[1]++ # Check that the expected parameters are present+ for param_name, param_value in expected_params.items():+ assert param_name in call_kwargs, (+ f"Expected parameter {param_name} not found"+ )+ assert call_kwargs[param_name] == param_value++ # Ensure mutually exclusive parameters are not both present+ if "max_completion_tokens" in expected_params:+ assert "max_tokens" not in call_kwargs, (+ "max_tokens should not be present for reasoning models"+ )+ else:+ assert "max_completion_tokens" not in call_kwargs, (+ "max_completion_tokens should not be present for non-reasoning models"+ )