Skip to content

Commit 52ac7ed

Browse files
authored
fix(config): add handling for config directory with .yml/.yaml extension (#1293)
1 parent 3018024 commit 52ac7ed

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

nemoguardrails/rails/llm/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ def from_path(
14791479
"""
14801480
# If the config path is a file, we load the YAML content.
14811481
# Otherwise, if it's a folder, we iterate through all files.
1482-
if config_path.endswith(".yaml") or config_path.endswith(".yml"):
1482+
if os.path.isfile(config_path) and config_path.endswith((".yaml", ".yml")):
14831483
with open(config_path) as f:
14841484
raw_config = yaml.safe_load(f.read())
14851485

tests/test_rails_config.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import logging
1717
import os
18+
import tempfile
19+
from pathlib import Path
1820
from unittest import mock
1921

2022
import pytest
@@ -117,6 +119,33 @@ def test_rails_config_from_path():
117119
assert config.sample_conversation is not None
118120

119121

122+
def test_rails_config_from_path_yml_extension():
123+
"""Test loading RailsConfig when the config directory ends with a .yml suffix.
124+
125+
Ensures a directory mistakenly named with a YAML extension is treated as a directory,
126+
not a file, and its internal YAML config is loaded properly.
127+
"""
128+
129+
with tempfile.TemporaryDirectory(suffix=".yml") as temp_dir:
130+
temp_path = Path(temp_dir)
131+
132+
minimal_yaml = (
133+
"models: []\n"
134+
"instructions:\n"
135+
" - type: general\n"
136+
" content: Test instruction\n"
137+
"sample_conversation: Test conversation\n"
138+
)
139+
140+
# place a config file inside the directory-with-.yml suffix
141+
(temp_path / "config.yml").write_text(minimal_yaml)
142+
143+
config = RailsConfig.from_path(str(temp_path))
144+
assert config is not None
145+
assert len(config.instructions) > 0
146+
assert config.sample_conversation is not None
147+
148+
120149
def test_rails_config_parse_obj():
121150
"""Test parsing RailsConfig from object."""
122151

0 commit comments

Comments
 (0)