@generative function whose return annotation tells the
runtime exactly what shape the result must have.
Source file: docs/examples/information_extraction/101_with_gen_slots.py
Concepts covered
- Declaring a generative function with
@generative - Using a
list[str]return type as an extraction contract - Passing a session (
m) as the first argument to a generative function - Keyword-only input via
doc=
Prerequisites
- Quick Start complete
- Ollama running locally with
granite4:micropulled
The full example
Imports and session
start_session() with no arguments creates a session backed by the default
local model. The model_ids import is available if you want to switch to a
specific model later (see Backends and configuration).
Declaring the extraction function
@generative decorator converts a bare function stub into a generative
slot. Three things drive the extraction:
- Parameter names (
doc) become the named inputs the model receives. - Return annotation (
list[str]) tells the runtime to parse and validate the response as a JSON array of strings. If the model returns something that cannot be coerced to that type, Mellea retries automatically. - Docstring is the task description sent to the model. Write it as a precise instruction — the docstring is the prompt.
Running the extraction
Full file
Key observations
The docstring is the prompt. There is no separate template file or prompt string. Writing a clear, imperative docstring is the primary tool for controlling extraction quality. The return type is the schema.list[str] is simple, but the same
mechanism works for Literal["positive", "negative", "neutral"], Pydantic
models, or any other type that Mellea knows how to validate. See
Enforce structured output for richer
return types.
Sessions are explicit. Passing m as the first argument makes the
dependency on a live backend visible at the call site. You can pass different
sessions in tests (for example, a session backed by a mock) without changing
the function definition.
What to try next:
- Replace
list[str]with a Pydantic model to extract multiple fields at once — see Enforce structured output. - Add
requirementsto the@generativecall to enforce constraints on the extracted values — see the requirements system concept. - Look at
docs/examples/information_extraction/advanced_with_m_instruct.pyfor a version that usesm.instruct()directly with structured outputs.
See also: Enforce Structured Output | The Requirements System | Examples Index