> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.
> For full documentation content, see https://docs.nvidia.com/nemo/guardrails/llms-full.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/guardrails/_mcp/server.

# nemoguardrails.llm.taskmanager

## Module Contents

### Classes

| Name                                                               | Description                                                   |
| ------------------------------------------------------------------ | ------------------------------------------------------------- |
| [`LLMTaskManager`](#nemoguardrails-llm-taskmanager-LLMTaskManager) | Interface for interacting with an LLM in a task-oriented way. |

### Data

[`_SINGLE_VAR_PATTERN`](#nemoguardrails-llm-taskmanager-_SINGLE_VAR_PATTERN)

### API

```python
class nemoguardrails.llm.taskmanager.LLMTaskManager(
    config: nemoguardrails.rails.llm.config.RailsConfig
)
```

Interface for interacting with an LLM in a task-oriented way.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager._get_general_instructions()
```

Helper to extract the general instructions.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager._get_messages_text_length(
    messages: typing.List[dict]
) -> int
```

Return the length of the text in the messages for token counting purposes.

This method calculates text length for token limit checks, using placeholders for base64 images
instead of counting their full encoded size. This allows multimodal content with large base64
images to pass the length checks while still preserving the actual content.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager._render_messages(
    message_templates: typing.List[typing.Union[str, nemoguardrails.rails.llm.config.MessageTemplate]],
    context: typing.Optional[dict] = None,
    events: typing.Optional[typing.List[dict]] = None
) -> typing.List[dict]
```

Render a sequence of messages.

:param message\_templates: The message templates to render.
:param context: The context for rendering the prompt.
:param events: The history of events so far.
:return: The rendered messages.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager._render_string(
    template_str: str,
    context: typing.Optional[dict] = None,
    events: typing.Optional[typing.List[dict]] = None
) -> str
```

Render a template using the provided context information.

:param template\_str: The template to render.
:param context: The context for rendering the prompt.
:param events: The history of events so far.
:return: The rendered template.
:rtype: str.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager._resolve_message_content(
    template_str: str,
    context: typing.Optional[dict] = None,
    events: typing.Optional[typing.List[dict]] = None
) -> typing.Union[str, list]
```

Resolve a message-template body to either a list or a rendered string.

When `template_str` matches a single-variable pattern such as
`"&#123;&#123; user_input &#125;&#125;"`, the variable is looked up directly so that a
list value (for example, multimodal `[&#123;"type": "text", ...&#125;, ...]`
content) is preserved as a list instead of being stringified by Jinja.

Lookup precedence for the single-variable case:

* The argument `context` is consulted first; its value seeds the
  candidate result.
* `self.prompt_context` is consulted second, but it overrides the
  argument only when its value is itself a list. This keeps a list
  supplied in `context` safe from being clobbered by a scalar
  fallback in `self.prompt_context`, while still allowing a list in
  `self.prompt_context` to win when the caller did not pass one.

If neither path yields a list (or `template_str` is not a single
variable), the method falls back to the regular Jinja render, which
coerces values to strings.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager.get_max_tokens(
    task: typing.Union[str, nemoguardrails.llm.types.Task]
) -> typing.Optional[int]
```

Return the maximum number of tokens for the given task.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager.get_stop_tokens(
    task: typing.Union[str, nemoguardrails.llm.types.Task]
) -> typing.Optional[typing.List[str]]
```

Return the stop sequence for the given task.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager.has_output_parser(
    task: nemoguardrails.llm.types.Task
)
```

```python
nemoguardrails.llm.taskmanager.LLMTaskManager.parse_task_output(
    task: nemoguardrails.llm.types.Task,
    output: str,
    forced_output_parser: typing.Optional[str] = None
) -> str
```

Parses the output of a task using the configured output parser.

**Parameters:**

The task for which the output is being parsed.

The output string to be parsed.

An optional parser name to force

**Returns:** `str`

The parsed text output.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager.register_filter(
    filter_fn: typing.Callable,
    name: typing.Optional[str] = None
)
```

Register a custom filter for the rails configuration.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager.register_output_parser(
    output_parser: typing.Callable,
    name: str
)
```

Register a custom output parser for the rails configuration.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager.register_prompt_context(
    name: str,
    value_or_fn: typing.Any
)
```

Register a value to be included in the prompt context.

:name: The name of the variable or function that will be used.
:value\_or\_fn: The value or function that will be used to generate the value.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager.render_task_prompt(
    task: typing.Union[str, nemoguardrails.llm.types.Task],
    context: typing.Optional[dict] = None,
    events: typing.Optional[typing.List[dict]] = None,
    force_string_to_message: typing.Optional[bool] = False
) -> typing.Union[str, typing.List[dict]]
```

Render the prompt for a specific task.

:param task: The name of the task.
:param context: The context for rendering the prompt
:param events: The history of events so far.
:param force\_string\_to\_message: Force the string message to a user message.
This should be used for chat models that receive a single message in the task prompt.

:return: A string, for completion models, or an array of messages for chat models.

Note that even chat models can have task prompts defined using a string and not an array of messages.
In this case, the chat model will through an error. If you want to solve this problem, use the
force\_string\_to\_message parameter to force the string message to a user message.

```python
nemoguardrails.llm.taskmanager._SINGLE_VAR_PATTERN = re.compile('^\\{\\{\\s*(\\w+)\\s*\\}\\}$')
```