> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mellea.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# mellea.stdlib.components.instruction

> `Instruction` component for instruct/validate/repair loops.

export const SidebarFix = () => <script dangerouslySetInnerHTML={{
  __html: `
        (function () {
          const INTERVAL_MS = 500;

          const upgradeSidebar = () => {
            const links = document.querySelectorAll('a[href^="#"]');

            links.forEach((link) => {
              if (link.dataset.badged === "true") return;

              const rawText = (link.textContent || "").trim();

              // ========== FUNC ==========
              if (rawText.startsWith("FUNC ")) {
                const label = rawText.replace(/^FUNC\\s+/, "").trim();

                while (link.firstChild) link.removeChild(link.firstChild);

                // 👉 Make the whole link a single flex row & prevent wrapping
                link.style.display = "flex";
                link.style.alignItems = "center";
                link.style.whiteSpace = "nowrap";
                link.style.columnGap = "0.5rem";

                const badge = document.createElement("span");
                badge.style.marginRight = "0.5rem";
                badge.style.display = "inline-flex";
                badge.style.alignItems = "center";
                badge.style.borderRadius = "9999px";
                badge.style.padding = "0rem 0.6rem";
                badge.style.fontSize = "0.5rem";
                badge.style.fontWeight = "700";
                badge.style.letterSpacing = "0.05em";
                badge.style.backgroundColor = "rgba(48, 100, 227, 0.20)";
                badge.style.color = "#1D4ED8";

                badge.textContent = "FUNC";

                link.appendChild(badge);
                link.appendChild(document.createTextNode(label));
                link.dataset.badged = "true";
                return;
              }

              // ========== CLASS ==========
              if (rawText.startsWith("CLASS ")) {
                const label = rawText.replace(/^CLASS\\s+/, "").trim();

                while (link.firstChild) link.removeChild(link.firstChild);

                // 👉 Same flex / nowrap treatment for class links
                link.style.display = "flex";
                link.style.alignItems = "center";
                link.style.whiteSpace = "nowrap";
                link.style.columnGap = "0.5rem";

                const badge = document.createElement("span");
                badge.style.marginRight = "0.5rem";
                badge.style.display = "inline-flex";
                badge.style.alignItems = "center";
                badge.style.borderRadius = "9999px";
                badge.style.padding = "0rem 0.6rem";
                badge.style.fontSize = "0.5rem";
                badge.style.fontWeight = "700";
                badge.style.letterSpacing = "0.05em";
                badge.style.backgroundColor = "rgba(74, 222, 128, 0.20)";
                badge.style.color = "#15803D";

                badge.textContent = "CLASS";

                link.appendChild(badge);
                link.appendChild(document.createTextNode(label));
                link.dataset.badged = "true";
                return;
              }
            });
          };

          upgradeSidebar();
          setInterval(upgradeSidebar, INTERVAL_MS);
        })();
      `
}} />;

<SidebarFix />

`Instruction` component for instruct/validate/repair loops.

`Instruction` is the primary component type used with `MelleaSession.instruct`. It
packages a task `description`, a list of [`Requirement`](../../core/requirement#class-requirement) constraints, optional
in-context-learning examples, a grounding context dict, user variables for Jinja2
template interpolation, and output/input prefix overrides into a single renderable
unit. The session's sampling strategy evaluates each requirement against the model's
output and may repair or resample until all requirements pass.

## Classes

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#4ADE8033]/20 text-[#15803D]">CLASS</span> `Instruction` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/components/instruction.py#L29" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

The Instruction in an instruct/validate/repair loop.

**Args:**

* `description`: The task description shown to the model.
* `requirements`: Constraints the output must satisfy.
* `icl_examples`: In-context-learning examples.
* `grounding_context`: Named context
  passages injected into the prompt.
* `user_variables`: Jinja2 variable substitutions applied
  to all string parameters.
* `prefix`: A prefix prepended before the model's generation.
* `output_prefix`: A prefix prepended to the model's output token
  stream (currently unsupported; must be `None`).
* `images`: Images to include in the prompt.

**Attributes:**

* `requirements`: The resolved list of requirement instances
  attached to this instruction.

<div className="h-8" />

**Methods:**

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `parts` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/components/instruction.py#L145" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
parts(self)
```

Returns all of the constituent parts of an Instruction.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `format_for_llm` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/components/instruction.py#L160" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
format_for_llm(self) -> TemplateRepresentation
```

Format this instruction for the language model.

**Returns:**

* A template representation containing the
* description, requirements, in-context examples, grounding context,
* and optional prefix/repair fields.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `apply_user_dict_from_jinja` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/components/instruction.py#L193" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
apply_user_dict_from_jinja(user_dict: dict[str, str], s: str) -> str
```

Render a Jinja2 template string using the provided variable dictionary.

**Args:**

* `user_dict`: Mapping of Jinja2 variable names to their
  string replacement values.
* `s`: A string treated as a Jinja2 template to be rendered.

**Returns:**

* The rendered string with all Jinja2 placeholders substituted.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `requirements` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/components/instruction.py#L208" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
requirements(self) -> list[Requirement]
```

Returns a list of Requirement instances.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `copy_and_repair` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/components/instruction.py#L212" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
copy_and_repair(self, repair_string: str) -> Instruction
```

Create a deep copy of this instruction with the repair string set.

**Args:**

* `repair_string`: The repair feedback string to attach, typically
  describing which requirements failed and why.

**Returns:**

* A new `Instruction` identical to this one but with
* `_repair_string` set to `repair_string`.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />
