pip install mellea, Ollama running locally.
Concept overview: Generative functions explains the design and trade-offs.
@generative is the idiomatic way to define type-safe LLM functions in Mellea. You
write a function signature with type hints and a docstring — Mellea generates the
implementation, calls the backend, and parses the output into the declared return type.
Basic @generative
...). The decorator generates a prompt from the
signature and docstring, calls the backend, and returns a value of the declared type.
The first argument is always the MelleaSession.
Literal types constrain the model to output one of the allowed values.
Pydantic structured output
Return complex structured objects using Pydantic models:Pre- and post-conditions
Add runtime constraints withprecondition_requirements (checked before generation)
and requirements (checked after). Both accept the same requirement types as
instruct():
PreconditionException is raised immediately — the model
is never called:
Composing generative functions
Chain multiple@generative functions to build typed pipelines. The output of one
call becomes the input to the next:
Chain-of-thought reasoning
Advanced: This section shows a performance-oriented pattern for math and reasoning tasks.The Pydantic structured output pattern works well for explicit chain-of-thought (CoT) reasoning. Separating the reasoning step from the answer extraction step can significantly improve accuracy on tasks like GSM8K.
Thought titles can be surfaced in a UI for observability into the
model’s reasoning process.
See also: Generative Functions | Enforce Structured Output | Write Custom Verifiers