Concept overview: MObjects and mify explains the design and trade-offs.This tutorial shows how to make existing Python objects queryable and transformable by the LLM using
@mify — without changing their Python interface or behaviour.
By the end you will have covered:
- Applying
@mifyto an existing class m.query()— ask questions about an objectm.transform()— produce a transformed version of an object- Controlling which fields and methods the LLM sees
- Using
stringify_funcfor custom text representations
pip install mellea, Ollama running locally with granite4:micro downloaded.
The scenario
You have aCustomerRecord class — existing code that you cannot rewrite. You want
to start asking the LLM questions about individual records and generating
personalised summaries.
Step 1: Apply @mify
Decorate the class with@mify. This adds the LLM-queryable protocol to every
instance, without touching the class’s Python interface:
@mify exposes all instance attributes as fields and adds the
MObject protocol to every instance. The LLM sees a text representation
of the object built from those fields.
Full example: docs/examples/mify/mify.py
Step 2: Control the text representation
If the default field listing is too verbose or structured incorrectly, supply astringify_func to produce exactly the text the LLM receives:
Step 3: Limit which fields are visible
To hide internal state from the LLM, usefields_include with a Jinja2 template:
last_purchase field is not in fields_include so it is never sent to the
model.
Step 4: Use m.transform()
m.transform() asks the LLM to produce a modified version of the object by
calling one of its methods. Expose the target method with funcs_include:
to_summary(summary=...) with the generated text, and the return
value of that method is the result.
Step 5: Mify an object ad hoc
You can also mify an existing object instance without decorating its class — useful when you don’t own the class definition:What you built
A set of patterns for making legacy Python objects LLM-queryable without modifying their class definitions:| Pattern | Use when |
|---|---|
@mify (default) | All fields can be exposed |
stringify_func | Custom text representation needed |
fields_include + template | Only a subset of fields should be visible |
funcs_include | Specific methods should be callable by the LLM |
mify(obj) | You don’t own the class |