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: prevent hard failure on malformed ai config (#9960)

Dylan Madisetti2e6baa579668
diff --git a/marimo/_config/secrets.py b/marimo/_config/secrets.py
index e104025940..ea46531e5d 100644
--- a/marimo/_config/secrets.py
+++ b/marimo/_config/secrets.py
@@ -16,8 +16,10 @@ def mask_secrets_partial(config: PartialMarimoConfig) -> PartialMarimoConfig:
def mask_secrets(config: MarimoConfig) -> MarimoConfig:
- def deep_remove_from_path(path: list[str], obj: dict[str, Any]) -> None:
- if not path:
+ def deep_remove_from_path(
+ path: list[str], obj: dict[str, Any] | Any
+ ) -> None:
+ if not path or not isinstance(obj, dict):
return
key = path[0]
@@ -52,12 +54,11 @@ def mask_secrets(config: MarimoConfig) -> MarimoConfig:
)
new_config = deep_copy(config)
- config_dict = cast(dict[str, Any], new_config)
for secret in secrets:
- deep_remove_from_path(list(secret), config_dict)
+ deep_remove_from_path(list(secret), new_config)
- return new_config # type: ignore
+ return cast(MarimoConfig, new_config)
T = TypeVar("T")
diff --git a/tests/_config/test_secrets_config.py b/tests/_config/test_secrets_config.py
index fa2dbd098b..ab47da1cac 100644
--- a/tests/_config/test_secrets_config.py
+++ b/tests/_config/test_secrets_config.py
@@ -120,6 +120,27 @@ def test_mask_secrets_empty() -> None:
assert config["ai"]["openrouter"]["api_key"] == ""
+def test_mask_secrets_non_dict_values() -> None:
+ """mask_secrets must not crash when a secret path hits a non-dict value.
+
+ A malformed/partial config can place a non-dict where mask_secrets expects
+ to recurse into a dict. `bedrock=None` previously raised TypeError and
+ `custom_providers="oops"` raised AttributeError; valid providers must still
+ be masked.
+ """
+ config = PartialMarimoConfig(
+ ai={
+ "open_ai": {"api_key": "super_secret"},
+ "bedrock": None,
+ "custom_providers": "oops",
+ },
+ )
+ new_config = mask_secrets(config)
+ assert new_config["ai"]["open_ai"]["api_key"] == SECRET_PLACEHOLDER
+ assert new_config["ai"]["bedrock"] is None
+ assert new_config["ai"]["custom_providers"] == "oops"
+
+
def test_remove_secret_placeholders() -> None:
config = PartialMarimoConfig(
ai={