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
improvement: fallback to os.environ for AI API keys (#6016)
Myles Scolnick6024c50bf0c8
diff --git a/marimo/_server/ai/config.py b/marimo/_server/ai/config.pyindex 689f306ea0..93f5c3a48b 100644--- a/marimo/_server/ai/config.py+++ b/marimo/_server/ai/config.py@@ -37,11 +37,17 @@ class AnyProviderConfig:@classmethoddef for_openai(cls, config: AiConfig) -> AnyProviderConfig:- return cls._for_openai_like(config, "open_ai", "OpenAI")+ fallback_key = cls.os_key("OPENAI_API_KEY")+ return cls._for_openai_like(+ config, "open_ai", "OpenAI", fallback_key=fallback_key+ )@classmethoddef for_azure(cls, config: AiConfig) -> AnyProviderConfig:- return cls._for_openai_like(config, "azure", "Azure OpenAI")+ fallback_key = cls.os_key("AZURE_API_KEY")+ return cls._for_openai_like(+ config, "azure", "Azure OpenAI", fallback_key=fallback_key+ )@classmethoddef for_openai_compatible(cls, config: AiConfig) -> AnyProviderConfig:@@ -87,7 +93,11 @@ class AnyProviderConfig:@classmethoddef for_anthropic(cls, config: AiConfig) -> AnyProviderConfig:ai_config = _get_ai_config(config, "anthropic", "Anthropic")- key = _get_key(ai_config, "Anthropic")+ key = _get_key(+ ai_config,+ "Anthropic",+ fallback_key=cls.os_key("ANTHROPIC_API_KEY"),+ )return cls(base_url=_get_base_url(ai_config),api_key=key,@@ -96,8 +106,15 @@ class AnyProviderConfig:@classmethoddef for_google(cls, config: AiConfig) -> AnyProviderConfig:+ fallback_key = cls.os_key("GEMINI_API_KEY") or cls.os_key(+ "GOOGLE_API_KEY"+ )ai_config = _get_ai_config(config, "google", "Google AI")- key = _get_key(ai_config, "Google AI")+ key = _get_key(+ ai_config,+ "Google AI",+ fallback_key=fallback_key,+ )return cls(base_url=_get_base_url(ai_config),api_key=key,@@ -147,6 +164,12 @@ class AnyProviderConfig:except HTTPException:return cls.for_openai(config)+ @classmethod+ def os_key(cls, key: str) -> Optional[str]:+ import os++ return os.environ.get(key)+def _get_tools(mode: CopilotMode) -> list[Tool]:tool_manager = get_tool_manager()diff --git a/tests/_server/ai/test_ai_config.py b/tests/_server/ai/test_ai_config.pyindex 92351299d4..e379e49ed6 100644--- a/tests/_server/ai/test_ai_config.py+++ b/tests/_server/ai/test_ai_config.py@@ -1,6 +1,7 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations+import osfrom typing import Any, castfrom unittest.mock import patch@@ -279,6 +280,157 @@ class TestAnyProviderConfig:assert provider_config.tools == []+class TestOsKey:+ """Tests for os_key method."""++ @patch.dict(os.environ, {"OPENAI_API_KEY": "test-api-key"})+ def test_os_key_exists(self) -> None:+ """Test os_key returns value when environment variable exists."""+ result = AnyProviderConfig.os_key("OPENAI_API_KEY")+ assert result == "test-api-key"++ @patch.dict(os.environ, {}, clear=True)+ def test_os_key_not_exists(self) -> None:+ """Test os_key returns None when environment variable doesn't exist."""+ result = AnyProviderConfig.os_key("NONEXISTENT_KEY")+ assert result is None++ @patch.dict(os.environ, {"EMPTY_KEY": ""})+ def test_os_key_empty_string(self) -> None:+ """Test os_key returns empty string when environment variable is empty."""+ result = AnyProviderConfig.os_key("EMPTY_KEY")+ assert result == ""+++class TestProviderConfigWithFallback:+ """Tests for provider config methods with OS environment fallback."""++ @patch.dict(os.environ, {"OPENAI_API_KEY": "env-openai-key"})+ def test_for_openai_with_fallback_key(self) -> None:+ """Test OpenAI config uses fallback key when config is missing api_key."""+ config: AiConfig = {"open_ai": {}}++ provider_config = AnyProviderConfig.for_openai(config)++ assert provider_config.api_key == "env-openai-key"++ @patch.dict(os.environ, {"OPENAI_API_KEY": "env-openai-key"})+ def test_for_openai_config_key_takes_precedence(self) -> None:+ """Test OpenAI config key takes precedence over environment variable."""+ config: AiConfig = {"open_ai": {"api_key": "config-openai-key"}}++ provider_config = AnyProviderConfig.for_openai(config)++ assert provider_config.api_key == "config-openai-key"++ @patch.dict(os.environ, {}, clear=True)+ def test_for_openai_no_fallback_available(self) -> None:+ """Test OpenAI config fails when no config key and no env var."""+ config: AiConfig = {"open_ai": {}}++ with pytest.raises(HTTPException) as exc_info:+ AnyProviderConfig.for_openai(config)++ assert exc_info.value.status_code == HTTPStatus.BAD_REQUEST+ assert "OpenAI API key not configured" in str(exc_info.value.detail)++ @patch.dict(os.environ, {"AZURE_API_KEY": "env-azure-key"})+ def test_for_azure_with_fallback_key(self) -> None:+ """Test Azure config uses fallback key when config is missing api_key."""+ config: AiConfig = {+ "azure": {"base_url": "https://test.openai.azure.com"}+ }++ provider_config = AnyProviderConfig.for_azure(config)++ assert provider_config.api_key == "env-azure-key"++ @patch.dict(os.environ, {"AZURE_API_KEY": "env-azure-key"})+ def test_for_azure_config_key_takes_precedence(self) -> None:+ """Test Azure config key takes precedence over environment variable."""+ config: AiConfig = {+ "azure": {+ "api_key": "config-azure-key",+ "base_url": "https://test.openai.azure.com",+ }+ }++ provider_config = AnyProviderConfig.for_azure(config)++ assert provider_config.api_key == "config-azure-key"++ @patch.dict(os.environ, {"ANTHROPIC_API_KEY": "env-anthropic-key"})+ def test_for_anthropic_with_fallback_key(self) -> None:+ """Test Anthropic config uses fallback key when config is missing api_key."""+ config: AiConfig = {"anthropic": {}}++ provider_config = AnyProviderConfig.for_anthropic(config)++ assert provider_config.api_key == "env-anthropic-key"++ @patch.dict(os.environ, {"ANTHROPIC_API_KEY": "env-anthropic-key"})+ def test_for_anthropic_config_key_takes_precedence(self) -> None:+ """Test Anthropic config key takes precedence over environment variable."""+ config: AiConfig = {"anthropic": {"api_key": "config-anthropic-key"}}++ provider_config = AnyProviderConfig.for_anthropic(config)++ assert provider_config.api_key == "config-anthropic-key"++ @patch.dict(os.environ, {"GEMINI_API_KEY": "env-gemini-key"})+ def test_for_google_with_gemini_fallback_key(self) -> None:+ """Test Google config uses GEMINI_API_KEY fallback when config is missing api_key."""+ config: AiConfig = {"google": {}}++ provider_config = AnyProviderConfig.for_google(config)++ assert provider_config.api_key == "env-gemini-key"++ @patch.dict(os.environ, {"GOOGLE_API_KEY": "env-google-key"}, clear=True)+ def test_for_google_with_google_fallback_key(self) -> None:+ """Test Google config uses GOOGLE_API_KEY fallback when GEMINI_API_KEY is not available."""+ config: AiConfig = {"google": {}}++ provider_config = AnyProviderConfig.for_google(config)++ assert provider_config.api_key == "env-google-key"++ @patch.dict(+ os.environ,+ {+ "GEMINI_API_KEY": "env-gemini-key",+ "GOOGLE_API_KEY": "env-google-key",+ },+ )+ def test_for_google_gemini_takes_precedence_over_google(self) -> None:+ """Test Google config prefers GEMINI_API_KEY over GOOGLE_API_KEY."""+ config: AiConfig = {"google": {}}++ provider_config = AnyProviderConfig.for_google(config)++ assert provider_config.api_key == "env-gemini-key"++ @patch.dict(os.environ, {"GEMINI_API_KEY": "env-gemini-key"})+ def test_for_google_config_key_takes_precedence(self) -> None:+ """Test Google config key takes precedence over environment variables."""+ config: AiConfig = {"google": {"api_key": "config-google-key"}}++ provider_config = AnyProviderConfig.for_google(config)++ assert provider_config.api_key == "config-google-key"++ @patch.dict(os.environ, {}, clear=True)+ def test_for_google_no_fallback_available(self) -> None:+ """Test Google config fails when no config key and no env vars."""+ config: AiConfig = {"google": {}}++ with pytest.raises(HTTPException) as exc_info:+ AnyProviderConfig.for_google(config)++ assert exc_info.value.status_code == HTTPStatus.BAD_REQUEST+ assert "Google AI API key not configured" in str(exc_info.value.detail)++class TestGetKey:"""Tests for _get_key function."""@@ -369,6 +521,89 @@ class TestGetKey:exc_info.value.detail)+ def test_get_key_with_fallback_key(self):+ """Test using fallback key when api_key is missing."""+ config = {}++ result = _get_key(config, "Test Service", fallback_key="fallback-key")++ assert result == "fallback-key"++ def test_get_key_with_fallback_key_empty_api_key(self):+ """Test using fallback key when api_key is empty."""+ config = {"api_key": ""}++ result = _get_key(config, "Test Service", fallback_key="fallback-key")++ assert result == "fallback-key"++ def test_get_key_with_fallback_key_none_api_key(self):+ """Test using fallback key when api_key is None."""+ config = {"api_key": None}++ result = _get_key(config, "Test Service", fallback_key="fallback-key")++ assert result == "fallback-key"++ def test_get_key_config_takes_precedence_over_fallback(self):+ """Test that config api_key takes precedence over fallback_key."""+ config = {"api_key": "config-key"}++ result = _get_key(config, "Test Service", fallback_key="fallback-key")++ assert result == "config-key"++ def test_get_key_no_fallback_key_provided(self):+ """Test error when no fallback key provided and api_key missing."""+ config = {}++ with pytest.raises(HTTPException) as exc_info:+ _get_key(config, "Test Service", fallback_key=None)++ assert exc_info.value.status_code == HTTPStatus.BAD_REQUEST+ assert "Test Service API key not configured" in str(+ exc_info.value.detail+ )++ def test_get_key_empty_fallback_key(self):+ """Test error when fallback key is empty string."""+ config = {}++ with pytest.raises(HTTPException) as exc_info:+ _get_key(config, "Test Service", fallback_key="")++ assert exc_info.value.status_code == HTTPStatus.BAD_REQUEST+ assert "Test Service API key not configured" in str(+ exc_info.value.detail+ )++ def test_get_key_bedrock_profile_ignores_fallback(self):+ """Test that Bedrock profile handling ignores fallback key."""+ config = {"profile_name": "aws-profile"}++ result = _get_key(config, "Bedrock", fallback_key="fallback-key")++ assert result == "profile:aws-profile"++ def test_get_key_bedrock_credentials_ignores_fallback(self):+ """Test that Bedrock credentials handling ignores fallback key."""+ config = {+ "aws_access_key_id": "access-key",+ "aws_secret_access_key": "secret-key",+ }++ result = _get_key(config, "Bedrock", fallback_key="fallback-key")++ assert result == "access-key:secret-key"++ def test_get_key_ollama_placeholder_ignores_fallback(self):+ """Test that Ollama placeholder handling ignores fallback key."""+ config = {"base_url": "http://127.0.0.1:11434/"}++ result = _get_key(config, "Ollama", fallback_key="fallback-key")++ assert result == "ollama-placeholder"+class TestGetBaseUrl:"""Tests for _get_base_url function."""