mellea: a Python library for writing structured, testable generative programs with type-annotated outputs and automatic repair

mellea: a Python library for writing structured, testable generative programs with type-annotated outputs and automatic repair

What it solves

It addresses the unpredictability and fragility of LLM calls in AI pipelines, where silent failures and untestable outputs often make workflows brittle. Mellea replaces guesswork and manual prompt engineering with a structured, testable approach to generative programming.

How it works

Mellea uses a @generative decorator to transform standard Python functions into structured LLM calls. It leverages Python type hints and Pydantic schemas to ensure outputs are correctly typed and formatted. The library also allows users to attach natural-language requirements to calls, which the system then validates and automatically retries if they are not met.

Who it’s for

Developers building AI-powered pipelines and agents who need guaranteed output schemas, verifiable requirements, and predictable results from LLMs.

Highlights

  • Structured Output: Uses Pydantic schemas to enforce data types at generation time.
  • Automatic Repair: Validates outputs against natural-language requirements and performs automatic retries.
  • Sampling Strategies: Supports multiple methods for improving reliability, such as majority voting and rejection sampling.
  • Broad Backend Support: Compatible with OpenAI, Ollama, HuggingFace, WatsonX, LiteLLM, and Bedrock.
  • MCP Compatibility: Allows generative programs to be exposed as Model Context Protocol (MCP) tools.

Sources