Skip to content

Nemo engine

Default NeMo Guardrails engine implementation.

NemoGuardrailEngine(config)

Bases: GuardrailEngine

Default engine that keeps current NeMo Guardrails behavior.

This engine implements the GuardrailEngine protocol to provide NeMo Guardrails functionality for content safety checking.

Attributes:

Name Type Description
config NemoGuardrailEngineConfig

Engine configuration specifying what content types to check.

rails LLMRails | None

LLMRails instance.

Initializes a new instance of the NemoGuardrailEngine class.

The engine uses structured safety configuration via content_safety_config and topic_safety_config. Either or both can be provided; if only one is given, that check runs and the other is skipped.

LLM Invoker Override: If self.config.nemo_kwargs is provided and contains a model configuration with type: main, that model will be overridden by self.config.lm_invoker to ensure consistency across the SDK.

Example

Activate the new configuration by providing at least one safety config::

from gllm_guardrail.config.safety import ContentSafetyConfigBuilder, TopicSafetyConfigBuilder

# Both enabled (recommended)
config = NemoGuardrailEngineConfig(
    lm_invoker=my_invoker,
content_safety_config=ContentSafetyConfigBuilder(),
topic_safety_config=TopicSafetyConfigBuilder(),
)

# Only content safety enabled, topic safety skipped
config = NemoGuardrailEngineConfig(
    lm_invoker=my_invoker,
    content_safety_config=ContentSafetyConfigBuilder(),
)

engine = NemoGuardrailEngine(config=config)

Parameters:

Name Type Description Default
config NemoGuardrailEngineConfig

NemoGuardrailEngineConfig specifying both base and NeMo-specific settings.

required

check_input(content, **kwargs) async

Check input content for safety violations.

This method implements NeMo Guardrails checking for user input such as query content, context, or prompts before sending to LLM.

Parameters:

Name Type Description Default
content str

The input text to evaluate for safety.

required
**kwargs Any

Additional engine-specific parameters.

{}

Returns:

Name Type Description
GuardrailResult GuardrailResult

GuardrailResult indicating if content is safe, with reason if unsafe.

Raises:

Type Description
RuntimeError

If the guardrail engine is not initialized.

check_output(content, **kwargs) async

Check output content for safety violations.

This method implements NeMo Guardrails checking for system output such as LLM responses, generated text, or static messages before showing to users.

Parameters:

Name Type Description Default
content str

The output text to evaluate for safety.

required
**kwargs Any

Additional engine-specific parameters.

{}

Returns:

Name Type Description
GuardrailResult GuardrailResult

GuardrailResult indicating if content is safe, with reason if unsafe.

Raises:

Type Description
RuntimeError

If the guardrail engine is not initialized.

NemoGuardrailEngineConfig

Bases: BaseGuardrailEngineConfig

NeMo-specific configuration that extends base guardrail config.

This config inherits the basic guardrail_mode from BaseGuardrailEngineConfig and adds NeMo-specific settings for advanced guardrail functionality.

Attributes:

Name Type Description
lm_invoker BaseLMInvoker | None

Invoker used by the NeMo adapter.

nemo_kwargs dict[str, Any] | None

NeMo-specific kwargs.

content_safety_config ContentSafetyConfigBuilder | None

Content safety configuration.

topic_safety_config TopicSafetyConfigBuilder | None

Topic safety configuration.

Example

Provide both configs to enable full content and topic safety::

from gllm_guardrail.config.safety import ContentSafetyConfigBuilder, TopicSafetyConfigBuilder

config = NemoGuardrailEngineConfig(
    lm_invoker=my_invoker,
    content_safety_config=ContentSafetyConfigBuilder(),
    topic_safety_config=TopicSafetyConfigBuilder(),
)

Either config can be provided alone to enable only that safety check::

# Only content safety, topic safety disabled
config = NemoGuardrailEngineConfig(
    lm_invoker=my_invoker,
    content_safety_config=ContentSafetyConfigBuilder(),
)

# Only topic safety, content safety disabled
config = NemoGuardrailEngineConfig(
    lm_invoker=my_invoker,
    topic_safety_config=TopicSafetyConfigBuilder(),
)

Raises:

Type Description
ValidationError

If neither content_safety_config nor topic_safety_config is provided.

from_model_config(model_id, credentials=None, config=None, **kwargs) classmethod

Create a NemoGuardrailEngineConfig from a model configuration.

Parameters:

Name Type Description Default
model_id str | ModelId

The model ID.

required
credentials str | dict[str, Any] | None

The credentials for the model.

None
config dict[str, Any] | None

The model configuration.

None
**kwargs Any

Additional kwargs to pass to the constructor.

{}

Returns:

Name Type Description
NemoGuardrailEngineConfig NemoGuardrailEngineConfig

The created NemoGuardrailEngineConfig.

init_guardrails_provider_registration()

Register global NeMo Guardrails chat provider once.

This avoids redundant registration per preset/component. Safe to call once during process initialization.

Returns:

Name Type Description
bool bool

True if registration succeeded, False otherwise.