nemoguardrails.integrations.langchain.runnable_rails
nemoguardrails.integrations.langchain.runnable_rails
Module Contents
Classes
Data
API
Bases: Runnable[Input, Output]
A runnable that wraps a rails configuration.
This class implements the LangChain Runnable protocol to provide a way to add guardrails to LangChain components. It can wrap LLM models or entire chains and add input/output rails and dialog rails.
Parameters:
The rails configuration to use.
Optional LLM to use with the rails.
Optional list of tools to register with the rails.
Whether to pass through the original prompt or let rails modify it. Defaults to True.
Optional runnable to wrap with the rails.
The key to use for the input when dealing with dict input.
The key to use for the output when dealing with dict output.
Whether to print verbose logs.
Message to return when input is blocked by rails.
Message to return when output is blocked by rails.
The type of the output of this runnable as a type annotation.
Chain this runnable with another, returning a new runnable.
This method handles two different cases:
- If other is a BaseLanguageModel, set it as the LLM for this RunnableRails
- If other is a Runnable, either: a. Set it as the passthrough_runnable if this RunnableRails has no passthrough_runnable yet b. Otherwise, delegate to the standard Runnable.or to create a proper chain
This ensures associativity in complex chains.
Convert LangChain messages to rails message format.
Create messages for passthrough mode.
Extract text content from result, handling both dict and direct formats.
Extract content from output for rails checking.
Extract text content from various input types for passthrough mode.
Extract user input from dictionary, checking configured key first.
Format output for ChatPromptValue input.
Format output for dictionary input.
Format dict output when user input was a BaseMessage.
Format dict output when user input was a list of BaseMessage objects.
Format dict output when user input was a list of dict messages.
Format dict output when the user input was a string.
Format output for BaseMessage input types.
Format the output based on the input type and rails result.
Parameters:
The original input.
The result from the rails.
The context returned by the rails.
Returns: Any
The formatted output.
Raises:
ValueError: If the input type cannot be handled.
Format output for passthrough mode.
Format a streaming chunk based on the input type.
Parameters:
The original input
The current chunk (string or dict with text and metadata)
Returns: Any
The formatted streaming chunk (using AIMessageChunk for LangChain compatibility)
Format output for StringPromptValue input.
Full rails mode async: existing LLMRails processing.
Full rails mode: existing LLMRails processing.
Extract the bot message from context or result.
Initialize the passthrough function for the LLM rails instance.
Convert various input formats to rails message format.
Transform ChatPromptValue to messages list.
Transform dictionary input to messages list.
Transform list from dictionary input to messages.
Transform user input value from dictionary.
Transform input to the format expected by the rails.
Parameters:
The input to transform.
Returns: List[Dict[str, Any]]
A list of messages in the format expected by the rails.
Raises:
ValueError: If the input format cannot be handled.
Batch inputs and process them asynchronously.
Concurrency is controlled via config[‘max_concurrency’] following LangChain best practices.
Invoke this runnable asynchronously.
Stream the output of this runnable asynchronously.
Provides token-by-token streaming of the LLM response with guardrails applied. Uses LLMRails.stream_async() directly for efficient streaming.
Transform the input asynchronously.
This is just an alias for ainvoke.
Batch inputs and process them synchronously.
Get the name of this runnable.
Invoke this runnable synchronously.
Stream the output of this runnable synchronously.
Provides token-by-token streaming of the LLM response with guardrails applied. Handles async context properly by running astream in a separate event loop.
Transform the input.
This is just an alias for invoke.