Skip to content

bug: RailsConfig throws error when adding configs #1327

@JashG

Description

@JashG

Did you check docs and existing issues?

  • I have read all the NeMo-Guardrails docs
  • I have updated the package to the latest version before submitting this issue
  • (optional) I have used the develop branch
  • I have searched the existing issues of NeMo-Guardrails

Python version (python --version)

Python 3.10.2

Operating system/version

MacOS 15.5

NeMo-Guardrails version (if you must use a specific version and not the latest

0.15.0

Describe the bug

The RailsConfig class allows for adding two config objects. I encountered two cases where this function throws an error:

  1. prompts is None for at least one RailsConfig object:
config1 = RailsConfig(
  models=[
    Model(type="main", engine="openai", model="gpt-3.5-turbo")
  ],
)
config2 = RailsConfig(
  models=[
    Model(type="secondary", engine="anthropic", model="claude-3")
  ],
)

combined_config = config1 + config2

Error:

    @root_validator(pre=True, allow_reuse=True)
    def check_output_parser_exists(cls, values):
        tasks_requiring_output_parser = [
            "self_check_input",
            "self_check_facts",
            "self_check_output",
            # "content_safety_check input $model",
            # "content_safety_check output $model",
        ]
        prompts = values.get("prompts", [])
>       for prompt in prompts:
E       TypeError: 'NoneType' object is not iterable

nemoguardrails/rails/llm/config.py:1414: TypeError: 'NoneType' object is not iterable
  1. config_path is None for at least one RailsConfig object:
config1 = RailsConfig(
  models=[
    Model(type="main", engine="openai", model="gpt-3.5-turbo")
  ],
  prompts=[]
)
config2 = RailsConfig(
  models=[
    Model(type="secondary", engine="anthropic", model="claude-3")
  ],
  prompts=[]
)

combined_config = config1 + config2

Error:

def _join_rails_configs(
        base_rails_config: RailsConfig, updated_rails_config: RailsConfig
    ):
        """Helper to join two rails configuration."""
    
        config_old_types = {}
        for model_old in base_rails_config.models:
            config_old_types[model_old.type] = model_old
    
        for model_new in updated_rails_config.models:
            if model_new.type in config_old_types:
                if model_new.engine != config_old_types[model_new.type].engine:
                    raise ValueError(
                        "Both config files should have the same engine for the same model type"
                    )
                if model_new.model != config_old_types[model_new.type].model:
                    raise ValueError(
                        "Both config files should have the same model for the same model type"
                    )
    
        if base_rails_config.actions_server_url != updated_rails_config.actions_server_url:
            raise ValueError("Both config files should have the same actions_server_url")
    
        combined_rails_config_dict = _join_dict(
            base_rails_config.dict(), updated_rails_config.dict()
        )
>       combined_rails_config_dict["config_path"] = ",".join(
            [
                base_rails_config.dict()["config_path"],
                updated_rails_config.dict()["config_path"],
            ]
        )
E       TypeError: sequence item 0: expected str instance, NoneType found

Steps To Reproduce

I ran into this error upstream using the NeMo Guardrails service. Internally, the /v1/chat/completions endpoint invokes RailsConfig.__add__ given multiple config IDs, where I ran into the two errors above.

To reproduce, you can execute the code snippets shared above.

Expected Behavior

Adding two configs does not throw an error in the following cases:

  • prompts is None for at least one config
  • config_path is None for at least one config

Actual Behavior

Adding two configs does throw an error in the following cases:

  • prompts is None for at least one config
  • config_path is None for at least one config

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstatus: needs triageNew issues that have not yet been reviewed or categorized.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions