Skip to content

Add handling for config directory with .yml/.yaml extension #1293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nemoguardrails/rails/llm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
29 changes: 29 additions & 0 deletions tests/test_rails_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import logging
import os
import tempfile
from pathlib import Path
from unittest import mock

import pytest
Expand Down Expand Up @@ -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."""

Expand Down