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

> Requirements are a special type of Component used as input to the "validate" step in Instruct/Validate/Repair design patterns.

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

Requirements are a special type of Component used as input to the "validate" step in Instruct/Validate/Repair design patterns.

## Functions

<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> `requirement_check_to_bool` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/requirements/requirement.py#L22" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
requirement_check_to_bool(x: CBlock | str) -> bool
```

Checks if a given output should be marked converted to `True`.

By default, the requirement check alora outputs: `\{"requirement_likelihood": 0.0\}`.
Returns `True` if the likelihood value is > 0.5.

**Args:**

* `x`: ALoRA output string or CBlock containing JSON with a
  `requirement_likelihood` field.

**Returns:**

* True if the extracted likelihood exceeds 0.5, False otherwise.

<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> `reqify` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/requirements/requirement.py#L93" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
reqify(r: str | Requirement) -> Requirement
```

Map strings to Requirements.

This is a utility method for functions that allow you to pass in Requirements as either explicit Requirement objects or strings that you intend to be interpreted as requirements.

**Args:**

* `r`: A [`Requirement`](../../core/requirement#class-requirement) object or a plain string description to wrap as one.

**Returns:**

* A [`Requirement`](../../core/requirement#class-requirement) instance.

**Raises:**

* `Exception`: If `r` is neither a `str` nor a [`Requirement`](../../core/requirement#class-requirement) instance.

<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> `req` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/requirements/requirement.py#L115" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
req(*args, **kwargs) -> Requirement
```

Shorthand for `Requirement.__init__`.

**Args:**

* `*args`: Positional arguments forwarded to `Requirement.__init__`.
* `**kwargs`: Keyword arguments forwarded to `Requirement.__init__`.

**Returns:**

* A new [`Requirement`](../../core/requirement#class-requirement) instance.

<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> `check` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/requirements/requirement.py#L128" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
check(*args, **kwargs) -> Requirement
```

Shorthand for `Requirement.__init__(..., check_only=True)`.

**Args:**

* `*args`: Positional arguments forwarded to `Requirement.__init__`.
* `**kwargs`: Keyword arguments forwarded to `Requirement.__init__`.

**Returns:**

* A new [`Requirement`](../../core/requirement#class-requirement) instance with `check_only=True`.

<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> `simple_validate` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/requirements/requirement.py#L153" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
simple_validate(fn: Callable[[str], Any]) -> Callable[[Context], ValidationResult]
```

Syntactic sugar for writing validation functions that only operate over the last output from the model (interpreted as a string).

This is useful when your validation logic only depends upon the most recent model output. For example:

`Requirement("Answer 'yes' or 'no'", simple_validate(lambda x: x == 'yes' or x == 'no')`

Validation functions operate over [`Context`](../../core/base#class-context). Often you do not care about the entire context, and just want to consider the most recent output from the model.

**Args:**

* `fn`: the simple validation function that takes a string and returns either a bool or (bool, str)
* `reason`: only used if the provided function returns a bool; if the validation function fails, a static reason for that failure to give to the llm when repairing

**Returns:**

* A validation function that takes a [`Context`](../../core/base#class-context) and returns a [`ValidationResult`](../../core/requirement#class-validationresult).

**Raises:**

* `ValueError`: If `fn` returns a type other than `bool` or
  `tuple[bool, str]`.

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

## 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> `LLMaJRequirement` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/requirements/requirement.py#L11" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

A requirement that always uses LLM-as-a-Judge. Any available constraint ALoRA will be ignored.

**Attributes:**

* `use_aloras`: Always `False` for this class; ALoRA adapters are
  never used even if they are available.

<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> `ALoraRequirement` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/stdlib/requirements/requirement.py#L58" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

A requirement validated by an ALoRA adapter; falls back to LLM-as-a-Judge only on error.

If an exception is thrown during the ALoRA execution path, `mellea` will
fall back to LLMaJ. That is the only case where LLMaJ will be used.

**Args:**

* `description`: Human-readable requirement description.
* `intrinsic_name`: Name of the ALoRA intrinsic to use.
  Defaults to `"requirement-check"`.

**Attributes:**

* `use_aloras`: Always `True`; this class always attempts to use
  ALoRA adapters for validation.

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