Skip to content

Constants

gllm_guardrail.constants

Guardrail configuration constants.

Authors

Kevin Yauris (kevin.yauris@gdplabs.id) Moch. Nauval Rizaldi Nasril (moch.n.r.nasril@gdplabs.id)

References

https://github.com/NVIDIA/NeMo-Guardrails

logger = manager.get_logger(__name__) module-attribute

manager = LoggerManager() module-attribute

GuardrailConstants

Constants for the GuardrailManager component.

ANSWER_KEY = 'answer' class-attribute instance-attribute

CONTENT_KEY = 'content' class-attribute instance-attribute

CONTEXT_KEY = 'context' class-attribute instance-attribute

DEFAULT_EMPTY_CONTENT_SAFE = True class-attribute instance-attribute

DEFAULT_ERROR_CONSERVATIVE = True class-attribute instance-attribute

ENGINE_KWARGS_KEY = 'engine_kwargs' class-attribute instance-attribute

FILTERED_CONTENT_KEY = 'filtered_content' class-attribute instance-attribute

IS_SAFE_KEY = 'is_safe' class-attribute instance-attribute

QUERY_KEY = 'query' class-attribute instance-attribute

REASON_KEY = 'reason' class-attribute instance-attribute

GuardrailMode

Bases: StrEnum

Guardrail mode enumeration for guardrail configuration.

BOTH = 'both' class-attribute instance-attribute

DISABLED = 'disabled' class-attribute instance-attribute

INPUT_ONLY = 'input_only' class-attribute instance-attribute

OUTPUT_ONLY = 'output_only' class-attribute instance-attribute

LoggerManager

A singleton class to manage logging configuration.

This class ensures that the root logger is initialized only once and is used across the application.

Basic usage

The LoggerManager can be used to get a logger instance as follows:

logger = LoggerManager().get_logger()
logger.info("This is an info message")
Set logging configuration

The LoggerManager also provides capabilities to set the logging configuration:

manager = LoggerManager()
manager.set_level(logging.DEBUG)
manager.set_log_format(custom_log_format)
manager.set_date_format(custom_date_format)
Add custom handlers

The LoggerManager also provides capabilities to add custom handlers to the root logger:

manager = LoggerManager()
handler = logging.FileHandler("app.log")
manager.add_handler(handler)
Extra error information

When logging errors, extra error information can be added as follows:

logger.error("I am dead!", extra={"error_code": "ERR_CONN_REFUSED"})
Logging modes

The LoggerManager supports three logging modes:

  1. Text: Logs in a human-readable format with RichHandler column-based formatting. Used when the LOG_FORMAT environment variable is set to "text". Output example: log 2025-10-08T09:26:16 DEBUG [LoggerName] This is a debug message. 2025-10-08T09:26:17 INFO [LoggerName] This is an info message. 2025-10-08T09:26:18 WARNING [LoggerName] This is a warning message. 2025-10-08T09:26:19 ERROR [LoggerName] This is an error message. 2025-10-08T09:26:20 CRITICAL [LoggerName] This is a critical message.

  2. Simple: Logs in a human-readable format with Rich colors but without columns-based formatting. Used when the LOG_FORMAT environment variable is set to "simple". Output example: log [2025-10-08T09:26:16.123 LoggerName DEBUG] This is a debug message. [2025-10-08T09:26:17.456 LoggerName INFO] This is an info message. [2025-10-08T09:26:18.789 LoggerName WARNING] This is a warning message. [2025-10-08T09:26:19.012 LoggerName ERROR] This is an error message. [2025-10-08T09:26:20.345 LoggerName CRITICAL] This is a critical message.

  3. JSON: Logs in a JSON format, recommended for easy parsing due to the structured nature of the log records. Used when the LOG_FORMAT environment variable is set to "json". Output example: log {"timestamp": "2025-10-08T11:23:43+0700", "name": "LoggerName", "level": "DEBUG", "message": "..."} {"timestamp": "2025-10-08T11:23:44+0700", "name": "LoggerName", "level": "INFO", "message": "..."} {"timestamp": "2025-10-08T11:23:45+0700", "name": "LoggerName", "level": "WARNING", "message": "..."} {"timestamp": "2025-10-08T11:23:46+0700", "name": "LoggerName", "level": "ERROR", "message": "..."} {"timestamp": "2025-10-08T11:23:47+0700", "name": "LoggerName", "level": "CRITICAL", "message": "..."}

When the LOG_FORMAT environment is not set, the LoggerManager defaults to "text" mode.

__new__()

Initialize the singleton instance.

Returns:

Name Type Description
LoggerManager LoggerManager

The singleton instance.

add_handler(handler)

Add a custom handler to the root logger.

Parameters:

Name Type Description Default
handler Handler

The handler to add to the root logger.

required

get_logger(name=None)

Get a logger instance.

This method returns a logger instance that is a child of the root logger. If name is not provided, the root logger will be returned instead.

Parameters:

Name Type Description Default
name str | None

The name of the child logger. If None, the root logger will be returned. Defaults to None.

None

Returns:

Type Description
Logger

logging.Logger: Configured logger instance.

set_date_format(date_format)

Set date format for all loggers in the hierarchy.

Parameters:

Name Type Description Default
date_format str

The date format to set.

required

set_level(level)

Set logging level for all loggers in the hierarchy.

Parameters:

Name Type Description Default
level int

The logging level to set (e.g., logging.INFO, logging.DEBUG).

required

set_log_format(log_format)

Set logging format for all loggers in the hierarchy.

Parameters:

Name Type Description Default
log_format str

The log format to set.

required

NemoGuardrailConfigConstants

Default NeMo Guardrails configuration constants.

CORE_RESTRICTION_CATEGORIES = ['child_safety_protection', 'violence_physical_harm', 'hate_speech_discrimination', 'privacy_personal_information', 'professional_services_liability', 'misinformation_deception', 'regulated_industries_compliance', 'intellectual_property_copyright', 'system_manipulation_security'] class-attribute instance-attribute

DEFAULT_ALLOWED_TOPICS = ['company products and services', 'technical support', 'product documentation', 'company policies', 'general information about the company', 'help with using company software', 'troubleshooting company products'] class-attribute instance-attribute

DEFAULT_BUSINESS_DENIED_TOPICS = ['competitor products', 'political discussions', 'weather information', 'sports discussions', 'entertainment topics', 'personal life advice', 'gossip and rumors'] class-attribute instance-attribute

DEFAULT_COLANG_CONFIG = None class-attribute instance-attribute

DEFAULT_CONFIG_DICT = {'models': [{'type': 'main', 'engine': 'gllm_invoker', 'model': 'openai/gpt-5-nano', 'parameters': {'credentials': 'OPENAI_API_KEY', 'model_kwargs': {'default_hyperparameters': {'temperature': 0.0, 'top_p': 1, 'max_output_tokens': 256}, 'reasoning_effort': 'minimal'}}}], 'rails': {'dialog': {'single_call': {'enabled': True}}}} class-attribute instance-attribute

DEFAULT_ENABLED_CORE_RESTRICTIONS = CORE_RESTRICTION_CATEGORIES class-attribute instance-attribute

DEFAULT_TOPIC_SAFETY_MODE = TopicSafetyMode.HYBRID class-attribute instance-attribute

generate_colang_config(allowed_topics=None, denied_topics=None, topic_safety_mode=TopicSafetyMode.HYBRID, include_core_restrictions=True, enabled_core_restrictions=None) classmethod

Generate Colang configuration based on topic safety settings.

Parameters:

Name Type Description Default
allowed_topics list[str] | None

List of allowed topics. If None, uses default allowed topics.

None
denied_topics list[str] | None

List of business-specific denied topics. If None, uses default business denied topics. Note: Core safety restrictions (violence, medical advice, etc.) are included by default.

None
topic_safety_mode TopicSafetyMode | str

Topic safety mode. Options: "allowlist", "denylist", "hybrid", "disabled". Defaults to TopicSafetyMode.HYBRID.

HYBRID
include_core_restrictions bool

Whether to include core safety restrictions. If False, only business-specific topic safety will be included. Defaults to True.

True
enabled_core_restrictions list[str] | None

List of enabled core restriction categories. If None, uses default enabled core restrictions. Only used when include_core_restrictions is True.

None

Returns:

Name Type Description
str str

Generated Colang configuration string.

Raises:

Type Description
ValueError

If topic_safety_mode is invalid or configuration is invalid.

TopicSafetyMode

Bases: StrEnum

Topic safety mode enumeration for guardrail configuration.

ALLOWLIST = 'allowlist' class-attribute instance-attribute

DENYLIST = 'denylist' class-attribute instance-attribute

DISABLED = 'disabled' class-attribute instance-attribute

HYBRID = 'hybrid' class-attribute instance-attribute