nemoguardrails.actions.llm.utils
Module Contents
Functions
Data
API
Extract reasoning from <think> tags and remove them from response.content.
This function looks for <think>…</think> tags in the response content, and if found, extracts the reasoning content inside the tags. It has a side-effect: it removes the full reasoning trace and tags from response.content.
Parameters:
The LLM response object
Returns: Optional[str]
The extracted reasoning content, or None if no <think> tags found
Extract text content from response.
Flatten a multimodal user-message payload into a string for colang history.
Multimodal user events carry event_text as a list of OpenAI-style
content parts ([{"type": "text", "text": "..."}, {"type": "image_url", "image_url": {...}}, ...]). Including the full list in the colang
history bloats the context with raw base64 data; this helper extracts the
visible text parts and appends a [+ image] marker when one or more
image parts were present.
Non-string text fields (None or other types) inside a content part
are skipped so the " ".join(...) step cannot crash. If the message
is image-only, the result is just "[+ image]" without a leading
space.
Parameters:
Either a string (already flat) or a list of multimodal content parts.
Returns: str
The flattened text. A list input always produces a string; a string
Log the prompt to LLM call info.
Initialize or update LLM call info in context.
Escape invalid keywords in flow names.
Create the dialog history based on provided events.
Extract tool_calls from BotToolCalls events.
Parameters:
List of events to search through
Returns: Optional[list]
tool_calls if found in BotToolCalls event, None otherwise
Converts a flow to colang format.
Example flow:
to colang:
convert log message to prompt interaction identifier.
Get the current reasoning trace and clear it from the context.
Returns: Optional[str]
Optional[str]: The reasoning trace if one exists, None otherwise.
Get the current response metadata and clear it from the context.
Returns: Optional[dict]
Optional[dict]: The response metadata if it exists, None otherwise.
Get the current tool calls and clear them from the context.
Returns: Optional[list]
Optional[list]: The tool calls if they exist, None otherwise.
Creates a history of user messages and bot responses in colang format. user “Hi, how are you today?” express greeting bot express greeting “Greetings! I am the official NVIDIA Benefits Ambassador AI bot and I’m here to assist you.” user “What can you help me with?” ask capabilities bot inform capabilities “As an AI, I can provide you with a wide range of services, such as …”
Returns first bot action.
Returns first bot intent.
Helper that returns the first non-empty line from a string
Returns first user intent.
Returns the first action before an empty line.
Returns the last user intent from the events.
Returns the last bot utterance from the events.
Returns the last user intent from the events.
Returns the last user utterance from the events.
Returns the last user utterance from the events.
Returns the last user utterance from the events.
Helper that extracts multi-line responses from the LLM. Stopping conditions: when a non-empty line ends with a quote or when the token “user” appears after a newline. Empty lines at the begging of the string are skipped.
Returns the retrieved chunks for current user utterance from the events.
Helper that returns a list with the top k non-empty lines from a string.
If there are less than k non-empty lines, it returns a smaller number of lines.
Removes the action/intent identifiers.
Helper that given a history in colang format, removes all texts.
Helper that removes quotes from a string if the entire string is between quotes
Return True and emit a warning if the LLM produced no visible content because it hit the max_tokens budget.
Reasoning models (OpenAI o-series, gpt-5, DeepSeek-R1, Gemini 2.5, Qwen QwQ, etc.) spend output tokens on internal reasoning before emitting visible text. A small max_tokens budget can be fully consumed by the reasoning phase, leaving empty content and finish_reason=“length”. The call succeeds silently and callers that only inspect response.content see nothing. Callers whose downstream parser does not fail safely on empty input (e.g. self_check_facts, whose parser inverts the result) should use the return value to take an explicit fail-safe branch.