From a083888032c7453f272c85abd0ab2b289cb8c863 Mon Sep 17 00:00:00 2001 From: Aegis Dev Date: Sun, 31 May 2026 14:39:07 -0400 Subject: [PATCH] fix(config): guard against None when meta section lacks generated_by key When a config.toml [meta] section exists but omits the generated_by key, meta.get("generated_by") returns None, causing a TypeError on the None <= str comparison. Extract generated_by before the check so a missing key is treated as outdated rather than crashing. Fixes #2942 --- backend/chainlit/config.py | 3 ++- backend/tests/test_config.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/chainlit/config.py b/backend/chainlit/config.py index a540752b33..3e7931590e 100644 --- a/backend/chainlit/config.py +++ b/backend/chainlit/config.py @@ -633,7 +633,8 @@ def load_settings(): ui_settings = toml_dict.get("UI", {}) meta = toml_dict.get("meta") - if not meta or meta.get("generated_by") <= "0.3.0": + generated_by = meta.get("generated_by") if meta else None + if not generated_by or generated_by <= "0.3.0": raise ValueError( f"Your config file '{config_file}' is outdated. Please delete it and restart the app to regenerate it." ) diff --git a/backend/tests/test_config.py b/backend/tests/test_config.py index a1f9dff15f..324b77fe4a 100644 --- a/backend/tests/test_config.py +++ b/backend/tests/test_config.py @@ -7,6 +7,21 @@ from chainlit.config import ChainlitConfig +class TestLoadSettingsMetaGuard: + """Regression test: meta section with missing generated_by must not raise TypeError.""" + + def test_meta_section_without_generated_by_raises_value_error( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ): + """[meta] present but lacking 'generated_by' should raise ValueError, not TypeError.""" + toml_content = "[meta]\nsome_other_key = \"value\"\n" + cfg_file = tmp_path / "config.toml" + cfg_file.write_text(toml_content, encoding="utf-8") + monkeypatch.setattr(chainlit_config, "config_file", str(cfg_file)) + with pytest.raises(ValueError, match="outdated"): + chainlit_config.load_settings() + + @pytest.fixture def translation_dir(tmp_path: Path) -> Path: """Minimal translation directory with a controlled set of locale files."""