mellea.stdlib.requirements.requirement
Requirements are a special type of Component used as input to the "validate" step in Instruct/Validate/Repair design patterns.
Functions
FUNC requirement_check_to_bool
requirement_check_to_bool(x: CBlock | ModelOutputThunk | str) -> bool
Convert a requirement-check adapter output string to a boolean result.
Parses the JSON output produced by the requirement-check adapter and
returns True when the score exceeds 0.5.
Args:
x: Adapter output string or CBlock containing JSON with the contract\{"requirement_check"\: \{"score"\: <float>\}\}.
Returns:
Trueif the extracted score exceeds 0.5,Falseotherwise.
Raises:
json.JSONDecodeError: Ifxis not valid JSON.ValueError: If the parsed JSON is not an object (e.g. a list, string, or number).AdapterSchemaMismatchError: If the parsed output does not contain the expectedrequirement_check.scorestructure, or if the score is not a finite number in the range 0.0-1.0. Callers that previously treatedFalseas "requirement not met" must now catch this error separately.
FUNC reqify
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: ARequirementobject or a plain string description to wrap as one.
Returns:
- A
Requirementinstance.
Raises:
Exception: Ifris neither astrnor aRequirementinstance.
FUNC req
req(*args, **kwargs) -> Requirement
Shorthand for Requirement.__init__.
Args:
*args: Positional arguments forwarded toRequirement.__init__.**kwargs: Keyword arguments forwarded toRequirement.__init__.
Returns:
- A new
Requirementinstance.
FUNC check
check(*args, **kwargs) -> Requirement
Shorthand for Requirement.__init__(..., check_only=True).
Args:
*args: Positional arguments forwarded toRequirement.__init__.**kwargs: Keyword arguments forwarded toRequirement.__init__.
Returns:
- A new
Requirementinstance withcheck_only=True.
FUNC simple_validate
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. 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
Contextand returns aValidationResult.
Raises:
ValueError: Iffnreturns a type other thanboolortuple[bool, str].
Classes
CLASS LLMaJRequirement
A requirement that always uses LLM-as-a-Judge. Any available constraint ALoRA will be ignored.
Attributes:
use_aloras: AlwaysFalsefor this class; ALoRA adapters are never used even if they are available.
CLASS ALoraRequirement
A requirement validated by an ALoRA adapter; never falls back to LLMaJ.
If the adapter is unavailable or cannot activate, validation raises
ValueError rather than degrading to LLMaJ. Use a plain Requirement to
have mellea try the adapter and fall back to LLMaJ.
If the adapter generates output but the output fails schema validation
(requirement_check_to_bool raises AdapterSchemaMismatchError), the
error is surfaced on the returned ValidationResult.error by
Requirement.validate. The result fails closed (bool(result) is False),
so schema drift is neither silently
treated as a pass nor raised as an unexpected exception; callers can inspect
result.error to tell it apart from an ordinary "requirement not met".
Args:
description: Human-readable requirement description.intrinsic_name: Name of the ALoRA intrinsic to use. Defaults to"requirement-check".adapter_types: Adapter types to look up forintrinsic_name. Required whenintrinsic_nameis a custom, non-catalog adapter function (seeIntrinsic). Optional for a catalog name, which otherwise supplies its own — but an explicit value here overrides the catalog's, it is not ignored.
Attributes:
use_aloras: AlwaysTrue; this class always attempts to use ALoRA adapters for validation.
Raises:
ValueError: If a customintrinsic_nameis given withoutadapter_types, or, at validation time, if the adapter is unavailable or cannot activate.