diff --git a/nemoguardrails/rails/llm/config.py b/nemoguardrails/rails/llm/config.py index d0e0cf03e..1004adef7 100644 --- a/nemoguardrails/rails/llm/config.py +++ b/nemoguardrails/rails/llm/config.py @@ -1473,7 +1473,7 @@ def from_path( """ # If the config path is a file, we load the YAML content. # Otherwise, if it's a folder, we iterate through all files. - if config_path.endswith(".yaml") or config_path.endswith(".yml"): + if os.path.isfile(config_path) and config_path.endswith((".yaml", ".yml")): with open(config_path) as f: raw_config = yaml.safe_load(f.read()) diff --git a/tests/test_rails_config.py b/tests/test_rails_config.py index 8ccbb9497..6fe54f487 100644 --- a/tests/test_rails_config.py +++ b/tests/test_rails_config.py @@ -15,6 +15,8 @@ import logging import os +import tempfile +from pathlib import Path from unittest import mock import pytest @@ -117,6 +119,33 @@ def test_rails_config_from_path(): assert config.sample_conversation is not None +def test_rails_config_from_path_yml_extension(): + """Test loading RailsConfig when the config directory ends with a .yml suffix. + + Ensures a directory mistakenly named with a YAML extension is treated as a directory, + not a file, and its internal YAML config is loaded properly. + """ + + with tempfile.TemporaryDirectory(suffix=".yml") as temp_dir: + temp_path = Path(temp_dir) + + minimal_yaml = ( + "models: []\n" + "instructions:\n" + " - type: general\n" + " content: Test instruction\n" + "sample_conversation: Test conversation\n" + ) + + # place a config file inside the directory-with-.yml suffix + (temp_path / "config.yml").write_text(minimal_yaml) + + config = RailsConfig.from_path(str(temp_path)) + assert config is not None + assert len(config.instructions) > 0 + assert config.sample_conversation is not None + + def test_rails_config_parse_obj(): """Test parsing RailsConfig from object."""