Make outputs precise, predictable, and repeatable with Mellea’s library of generative functions.

Get started
without melleawith mellea

Mellea is a Python library for working with LLMs using generative functions.

In traditional programming, functions turn inputs into deterministic outputs. Mellea builds on this by allowing functions to call LLMs to create an output, but with customizable requirements that maintain the rigor of professional software development. If an LLM returns something that doesn’t meet a requirement, Mellea will automatically try it again. This means you can build applications that make use of the power of LLMs while keeping all the benefits of reliable, testable Python code.

Flowchart: input passes through a Python function and LLM call, requirements are checked, and failed outputs retry until a reliable result is returned

Precise

Write generative functions using code. The @generative decorator handles the communication with the LLM.

Predictable

Set the requirements that you want Mellea to validate. Automatic retries mean unwanted outputs never reach your users.

Flexible

Expose any Mellea program as an MCP tool. The calling agent gets the same validated, predictable output as other Mellea users.

Safe

Built-in Granite Guardian integration detects harmful outputs, hallucinations, and jailbreak attempts before they reach your users — no external service required.

Here’s the future of software

Python
from typing import Literal
from pydantic import BaseModel
from mellea import generative, start_session

class ReviewAnalysis(BaseModel):
    sentiment: Literal["positive", "negative", "neutral"]
    score: int    # 1-5
    summary: str  # one sentence

@generative
def analyze_review(text: str) -> ReviewAnalysis:
    """Extract sentiment, a 1-5 score, and a one-sentence summary."""
    ...

m = start_session()
result = analyze_review(m, text="Battery life is great but the screen is dim")

print(result.sentiment)  # "positive", "negative", or "neutral" — always
print(result.score)      # an int, 1-5 — always
print(result.summary)    # a str — always
import mellea
from mellea.stdlib.sampling import RejectionSamplingStrategy

def write_email_with_strategy(m: mellea.MelleaSession, name: str, notes: str) -> str:
    email_candidate = m.instruct(
        f"Write an email to {name} using the notes following: {notes}.",
        requirements=[
            "The email should have a salutation.",
            "Use a formal tone.",
        ],
        strategy=RejectionSamplingStrategy(loop_budget=3),
        return_sampling_results=True,
    )

    if email_candidate.success:
        return str(email_candidate.result)

    # If sampling fails, use the first generation
    print("Expect sub-par result.")
    return email_candidate.sample_generations[0].value
import mellea
from mellea.stdlib.requirements.safety.guardian import (
    GuardianCheck, GuardianRisk,
)

m = mellea.start_session()

response = m.instruct(
    "Write a helpful customer support response to: "
    "How do I reset my password?",
    requirements=[
        "Be concise and professional.",
        GuardianCheck(GuardianRisk.HARM),
        GuardianCheck(GuardianRisk.SOCIAL_BIAS),
    ],
)

print(response)  # validated — or retried until it passes

Granite is a family of models built with enterprises in mind. It has open-weights for transparency, comes in sizes from 350M-32B parameters, and offers an extensive adapter library. This means it can take on specific tasks with the performance of a much larger model while keeping its nimble speed and low costs.

Mellea and Granite bring out the best in each other.

Mellea is designed to be Granite’s SDK. Together, they offer a flexible way to build AI applications that are transparent from the ground up and only do what you want them to.

From the blog

All posts