Skip to main content
Version: Next

mellea.stdlib.components.instruction

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

CLASS Instruction ​

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.
  • audio: Audio clips to include in the prompt.

Attributes:

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

Methods:

FUNC parts ​

parts(self) -> list[Span]

Returns all of the constituent parts of an Instruction.

Returns:

  • list[Span]: All non-None constituent blocks and
  • components making up this instruction, including description,
  • prefix, grounding context, requirements, and in-context-learning
  • examples.

FUNC format_for_llm ​

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.

FUNC apply_user_dict_from_jinja ​

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.

FUNC images ​

images(self) -> list[ImageBlock | ImageUrlBlock] | None

Returns the images associated with this instruction.

FUNC audio ​

audio(self) -> list[AudioBlock | AudioUrlBlock] | None

Returns the audio associated with this instruction.

FUNC requirements ​

requirements(self) -> list[Requirement]

Returns a list of Requirement instances.

FUNC copy_and_repair ​

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.