> This page is for version v0.25.0.
> For other versions, use one of these documentation indexes:
> - Latest (default): https://docs.nvidia.com/nemo/guardrails/latest/llms.txt
> - v0.25.0: https://docs.nvidia.com/nemo/guardrails/v0.25.0/llms.txt
> - v0.24.1: https://docs.nvidia.com/nemo/guardrails/v0.24.1/llms.txt
> - v0.24.0: https://docs.nvidia.com/nemo/guardrails/v0.24.0/llms.txt
> - v0.23.0: https://docs.nvidia.com/nemo/guardrails/v0.23.0/llms.txt
> - v0.22.0: https://docs.nvidia.com/nemo/guardrails/v0.22.0/llms.txt

> 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.

# 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.

**`env`** `= SandboxedEnvironment()`

---

**`output_parsers`** `Dict[Optional[str], Callable]`

---

**`prompt_context`** `= {}`

---

```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._get_tool_conversation_length(
    messages: typing.List[dict]
) -> int
```

Measure serialized message characters without counting inline image payloads.

Only base64 image URLs in structured image_url parts receive a placeholder
for measurement. Text, tool arguments, result content, and message metadata
still count in full. The original messages and image data remain unchanged.

```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.

**Parameters:**

**`message_templates`** `List[Union[str, MessageTemplate]]`

`List[Union[str,` [`MessageTemplate`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/rails/llm/config#nemoguardrails-rails-llm-config-MessageTemplate) `]]` — The message templates to render.

---

**`context`** `Optional[dict]` — default: None

The context for rendering the prompt.

---

**`events`** `Optional[List[dict]]` — default: None

The history of events so far.

---

**Returns:** `List[dict]`

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.

**Parameters:**

**`template_str`** `str`

The template to render.

---

**`context`** `Optional[dict]` — default: None

The context for rendering the prompt.

---

**`events`** `Optional[List[dict]]` — default: None

The history of events so far.

---

**Returns:** `str.`

The rendered template.

```python
nemoguardrails.llm.taskmanager.LLMTaskManager._render_tool_conversation(
    prompt: nemoguardrails.rails.llm.config.TaskPrompt,
    events: typing.List[dict],
    tool_history_start: int,
    context: typing.Optional[dict]
) -> typing.List[dict]
```

Render a general prompt while preserving native tool calls and results.

For a string template, render events before tool_history_start as a user
message and append the remaining events as native chat messages. Message
templates render the full history through their configured filters.
Raise ToolConversationTooLongError if the rendered conversation exceeds
max_length; removing individual events could break a tool exchange.

```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
`"{{ user_input }}"`, the variable is looked up directly so that a
list value (for example, multimodal `[{"type": "text", ...}, ...]`
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: typing.Union[str, nemoguardrails.llm.types.Task]
)
```

```python
nemoguardrails.llm.taskmanager.LLMTaskManager.parse_task_output(
    task: typing.Union[str, 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:**

**`task`** `Union[str, Task]`

`Union[str,` [`Task`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/llm/types#nemoguardrails-llm-types-Task) `]` — The task for which the output is being parsed.

---

**`output`** `str`

The output string to be parsed.

---

**`forced_output_parser`** `Optional[str]` — default: None

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.

**Parameters:**

**`task`** `Union[str, Task]`

`Union[str,` [`Task`](/nemo/guardrails/v0.25.0/guardrails-python-sdk/nemoguardrails/llm/types#nemoguardrails-llm-types-Task) `]` — The name of the task.

---

**`context`** `Optional[dict]` — default: None

The context for rendering the prompt

---

**`events`** `Optional[List[dict]]` — default: None

The history of events so far.

---

**`force_string_to_message`** `Optional[bool]` — default: False

Force the string message to a user message. This should be used for chat models that receive a single message in the task prompt.

---

**Returns:** `Union[str, List[dict]]`

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*\\}\\}$')
```