instruct()with user variables and requirements- Rejection sampling and
SamplingResult - Composing generative functions into a pipeline
Prerequisites: Quick Start complete, Mellea installed (@generativein depth: This tutorial uses@generativein the final pipeline step. For a dedicated walkthrough of typed returns,Literal, and Pydantic models, see Tutorial 03: Using Generative Slots.
uv add mellea), Ollama running locally with granite4:micro downloaded.
Step 1: One instruction
Start with the smallest possible program: a single call toinstruct().
instruct() returns a ModelOutputThunk. Calling str() on it (or accessing
.value) gives you the string. This is already a generative program: it calls an
LLM and returns structured text.
The problem is reliability. The model might return two sentences, or three, or
include a preamble. Move to the next step to enforce the format.
Step 2: Adding user variables
Hardcoding the text in the instruction string makes the function impossible to reuse. Useuser_variables and {{double_braces}} template syntax:
Step 3: Enforcing constraints with requirements
Pass a list of plain-English requirements to constrain the output. Mellea checks each requirement after generation and retries if any fail:Step 4: Deterministic validation
For facts you can check in code — word counts, format, length — usesimple_validate:
Step 5: Rejection sampling and inspecting results
By default,instruct() retries up to twice if any requirement fails. Use
RejectionSamplingStrategy to control the budget and inspect results:
return_sampling_results=True, instruct() returns a SamplingResult with
.success, .result, and .sample_generations. This gives you programmatic
control over what to do when the model can not satisfy your requirements.
Step 6: Composing the pipeline
Assemble all the pieces into a complete pipeline:summarize_feedback feeds classify_sentiment; the original feedback
feeds extract_issues. There is no global state, no prompt accumulation — each
call is self-contained.
Full example: docs/examples/instruct_validate_repair/101_email_with_requirements.py
What you have built
| Step | What it does |
|---|---|
instruct() | Calls the LLM with a structured instruction |
| User variables | Injects dynamic values into the prompt template |
| Requirements | Enforces plain-English constraints via IVR |
simple_validate | Adds deterministic checks (word count, format) |
RejectionSamplingStrategy | Controls retry budget and exposes SamplingResult |
@generative | Typed functions with LLM-backed implementations (Tutorial 03) |
| Composition | Independent typed functions wired into a pipeline |
See also: Tutorial 02: Streaming and Async | Instruct, Validate, Repair | The Requirements System | Generative Functions | MObjects and mify | Use Images and Vision