# nvidia/nemo-guardrails architecture

> Architecture diagram of the GitHub repository nvidia/nemo-guardrails, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nvidia/nemo-guardrails
- Repository: https://github.com/nvidia/nemo-guardrails

## Overview

##### Main Components to Illustrate
- Core Guardrails Engine – This is the backbone of NeMo Guardrails. It includes:
- LLMRails (the public API for invoking the guardrails)
- RailsConfig (configuration loader that parses the guardrails configuration folder with its YAML files, .co files for Colang definitions, actions.py for custom logic, etc.)
- Colang parser and runtime (contained within the “nemoguardrails/colang” directories for versions 1.0 and 2.x, plus transformer and grammar components)
- Modules handling input, dialog, output, retrieval, and execution rails (with each type of rail functioning as a pipeline layer to modify, filter, or enhance the LLM requests/responses)

- LLM Connector and Provider Abstraction:
- LLM modules under “nemoguardrails/llm” that manage provider-specific integrations (OpenAI, Llama, etc.)
- Embedding providers and the integration of external vector search libraries (e.g., through “nemoguardrails/embeddings”)

- Actions and Custom Processing:
- The “actions” modules (found both in nemoguardrails/actions and within the rails, library subdirectories) that represent custom code executing validation, summarization, math operations, or integration with external APIs
- The guardrails library components (such as for jailbreak detection, self-check, fact-checking, etc.) that allow rapid extension of functionality

- Server and CLI Layers:
- The built-in CLI (nemoguardrails/cli) to trigger chat sessions, run evaluation tasks or launch an actions server
- The Server API (nemoguardrails/server) which exposes HTTP endpoints; includes datastore integrations (memory, redis) and asynchronous processing of LLM calls

- Integration with Third-Party Tools:
- Connectors for LangChain (found under “nemoguardrails/integrations/langchain”) which wrap guardrails around external chains
- Docker and devcontainer configurations (e.g., in the Dockerfile, .devcontainer folder) that indicate containerized deployment

##### Relationships and Interactions
- Input Flow:
- User input (either via chat-ui or an API call) is received and then passed to the guardrails engine.
- The input rail layer (with its Colang-based definitions from .co files) processes the received message.

- Dialog Processing:
- After pre-checks, the dialog rails guide the LLM’s behavior (by combining the loaded configurations and dynamic factors, e.g., as defined in the Colang scripts).
- The server’s actions (custom functions from actions.py) may be triggered, modifying or rejecting requested flows.

- Output Flow:
- The processed output then moves through the output rails which apply self-checks (fact-checking, moderation etc.) before returning the final content.
- The evaluation layer logs and monitors responses and flows (using modules in nemoguardrails/evaluate and nemoguardrails/logging).

- Asynchronous and API Communication:
- The core methods (synchronous and async versions of LLMRails.generate) illustrate the async-first design.

##### Important Architectural Patterns and Design Principles
- Modularity: Each rail type (input, dialog, output, retrieval, execution) is designed as an independent layer that can be combined or replaced.
- Configuration-driven behavior: The extensive use of configuration folders (YAML, .co Colang files, custom actions) means that the behavior is largely externalized.
- Asynchronous Processing: Emphasize async APIs in the core LLM interactions and guardrails processing.

##### Relevant Technologies, Frameworks, or Libraries
- Python (3.8+), Docker, and containerization (via Dockerfile and devcontainer)
- Annoy (for vector-based indexing in embeddings)
- External services for LLM providers, and integration with LangChain for chains
- Testing and CI/CD pipelines (evidenced by GitHub workflows, GitLab CI, pytest etc.)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Layer
    U["User Input"]:::ui

    subgraph "Interface Layer"
        CLI["CLI Interface"]:::infrastructure
        API["Server API"]:::infrastructure
    end

    U --> CLI
    U --> API

    %% Deployment Node connected to Interface Layer
    DPT["Docker/Devcontainer"]:::infrastructure
    DPT --- CLI
    DPT --- API

    %% Guardrails Engine Subgraph
    subgraph "Guardrails Engine"
        RC["RailsConfig"]:::engine
        LRM["LLMRails"]:::engine
        CP["Colang Parser"]:::engine
        RP["Rail Pipelines"]:::engine
        AM["Actions Modules"]:::engine
    end

    %% Connections from Interface to Engine
    CLI --> RC
    API --> RC
    RC --> LRM
    LRM --> RP
    RP --> CP
    CP --> AM

    %% LLM Integration Subgraph
    subgraph "LLM Integration"
        LP["LLM Providers"]:::integration
        EP["Embedding Providers"]:::integration
    end

    %% Connection from Guardrails Engine to LLM Integration and back
    AM --> LP
    LP --> EP
    EP --> RP

    %% External Integrations Subgraph
    subgraph "External Integrations"
        LC["LangChain Connectors"]:::integration
    end

    %% Connection from LLM Integration to External Integrations and feedback loop
    EP --> LC
    LC --> RP

    %% Click Events
    click LRM "https://github.com/nvidia/nemo-guardrails/blob/develop/nemoguardrails/rails/llm/llmrails.py"
    click RC "https://github.com/nvidia/nemo-guardrails/blob/develop/nemoguardrails/rails/llm/default_config.yml"
    click AM "https://github.com/nvidia/nemo-guardrails/blob/develop/nemoguardrails/actions/actions.py"
    click CP "https://github.com/nvidia/nemo-guardrails/tree/develop/nemoguardrails/colang/"
    click RP "https://github.com/nvidia/nemo-guardrails/tree/develop/nemoguardrails/rails/"
    click LP "https://github.com/nvidia/nemo-guardrails/tree/develop/nemoguardrails/llm/providers/"
    click EP "https://github.com/nvidia/nemo-guardrails/tree/develop/nemoguardrails/embeddings/"
    click CLI "https://github.com/nvidia/nemo-guardrails/tree/develop/nemoguardrails/cli/"
    click API "https://github.com/nvidia/nemo-guardrails/tree/develop/nemoguardrails/server/"
    click LC "https://github.com/nvidia/nemo-guardrails/tree/develop/nemoguardrails/integrations/langchain/"
    click DPT "https://github.com/nvidia/nemo-guardrails/tree/develop/.devcontainer/"

    %% Styles
    classDef ui fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef infrastructure fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef engine fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef integration fill:#d4edda,stroke:#155724,stroke-width:2px;
```
