Skip to navigation

Module Contents

Functions

NameDescription
_previous_lineReturns the previous lines, skipping comments.
apply_input_rail_updatesCopy messages and replace user content with the input-rail event history.
apply_tool_result_updatesCopy messages with tool results approved or rewritten by tool-result rails.
co_v2Creates a history of user messages and bot responses in colang format.
colangFilter that turns an array of events into a colang history.
colang_without_identifiersFilter that turns an array of events into a colang history.
conversation_to_eventsFilter that given a conversation, returns a list of events.
first_turnsReturns the first n turns from a given colang history.
indentIndents the provided text with the provided number of spaces.
last_turnsReturns the last n turns from a given colang history.
remove_text_messagesFilters that given a history in colang format, removes all texts.
remove_trailing_new_line-
to_chat_messagesConvert events to chat messages with native assistant tool calls and tool results.
to_intent_messages-
to_intent_messages_2-
to_messagesFilter that given a history in colang format, returns all messages.
to_messages_v2Filter that given a history in colang 2.0 format, returns all messages.
user_assistant_sequenceFilter that turns an array of events into a sequence of user/assistant messages.
verbose_v1Filter that given a history in colang format, returns a verbose version of the history.

API

nemoguardrails.llm.filters._previous_line(
lines: typing.List[str],
i: int
)

Returns the previous lines, skipping comments.

nemoguardrails.llm.filters.apply_input_rail_updates(
messages: typing.List[dict],
events: typing.List[dict]
) -> typing.List[dict]

Copy messages and replace user content with the input-rail event history.

Align user turns from the end so a message suffix can use a longer cached event history. Retain a placeholder for blocked turns to preserve alignment, and ignore synthetic user events used to resume tool-result generation. The caller’s message dictionaries are not modified.

nemoguardrails.llm.filters.apply_tool_result_updates(
messages: typing.List[dict],
events: typing.List[dict]
) -> typing.List[dict]

Copy messages with tool results approved or rewritten by tool-result rails.

Track each ToolMessage occurrence separately, since later turns may reuse a tool_call_id. Validation updates only the occurrences in its current batch; unapproved results retain a blocked-result placeholder. Match messages from the end so a prompt suffix can use a longer event history. Unchecked results retain their content, and caller messages and events are not modified.

nemoguardrails.llm.filters.co_v2(
events: typing.List[dict]
) -> str

Creates a history of user messages and bot responses in colang format.

user said “Hi, how are you today?” bot say “Greetings! I am the official NVIDIA Benefits Ambassador AI bot and I’m here to assist you.” user said “What can you help me with?” bot say “As an AI, I can provide you with a wide range of services, such as …”

nemoguardrails.llm.filters.colang(
events: typing.List[dict]
) -> str

Filter that turns an array of events into a colang history.

nemoguardrails.llm.filters.colang_without_identifiers(
events: typing.List[dict]
) -> str

Filter that turns an array of events into a colang history.

nemoguardrails.llm.filters.conversation_to_events(
conversation: typing.List
) -> typing.List[dict]

Filter that given a conversation, returns a list of events.

nemoguardrails.llm.filters.first_turns(
colang_history: str,
n: int
) -> str

Returns the first n turns from a given colang history.

nemoguardrails.llm.filters.indent(
text: str,
n_spaces: int
) -> str

Indents the provided text with the provided number of spaces.

nemoguardrails.llm.filters.last_turns(
colang_history: str,
n: int
) -> str

Returns the last n turns from a given colang history.

nemoguardrails.llm.filters.remove_text_messages(
colang_history: str
)

Filters that given a history in colang format, removes all texts.

nemoguardrails.llm.filters.remove_trailing_new_line(
s: str
)
nemoguardrails.llm.filters.to_chat_messages(
events: typing.List[dict]
) -> typing.List[dict]

Convert events to chat messages with native assistant tool calls and tool results.

Properly handles multimodal content by preserving the structure when the content is in the format of a Message object with potential image_url content. Synthetic user events that resume tool generation are omitted, and tool-result rail updates are applied before returning the messages.

nemoguardrails.llm.filters.to_intent_messages(
colang_history: str
) -> typing.List[dict]
nemoguardrails.llm.filters.to_intent_messages_2(
colang_history: str
) -> typing.List[dict]
nemoguardrails.llm.filters.to_messages(
colang_history: str
) -> typing.List[dict]

Filter that given a history in colang format, returns all messages.

nemoguardrails.llm.filters.to_messages_v2(
colang_history: str
) -> typing.List[dict]

Filter that given a history in colang 2.0 format, returns all messages.

nemoguardrails.llm.filters.user_assistant_sequence(
events: typing.List[dict]
) -> str

Filter that turns an array of events into a sequence of user/assistant messages.

The output will look like:

User: hi
Assistant: Hello there!
User: What can you do?
Assistant: I can help with many things.

For multimodal content, it extracts text content and indicates if there were images. Tool calls and rail-processed results are serialized as labeled JSON lines; synthetic user events that resume tool generation are omitted.

nemoguardrails.llm.filters.verbose_v1(
colang_history: str
) -> str

Filter that given a history in colang format, returns a verbose version of the history.