Skip to main content
This example shows how to bring existing Python objects into a Mellea session using the @mify decorator. @mify adds the MifiedProtocol interface to a class or instance so you can pass it directly to session methods like m.act(), m.query(), and m.transform(). Source file: docs/examples/mify/mify.py

Concepts covered

  • Applying @mify as a class decorator
  • Mifying an object instance at runtime (ad-hoc mification)
  • Controlling string representation with stringify_func
  • Choosing a query template with query_type and template_order
  • Selecting which fields the model sees with fields_include
  • Exposing specific methods as tools with funcs_include

Prerequisites

The full example

Imports

MifiedProtocol is used here only for the isinstance assertion that demonstrates what @mify adds to a class. In production code you would not normally need to import it.

Mifying a class with the decorator

Applying @mify to a class is a one-liner. Every instance of the decorated class automatically satisfies MifiedProtocol, which means you can pass any instance to a session method without any further setup.

Ad-hoc mification of an existing instance

You do not have to own a class to mify it. Call mify(instance) on any object to patch in the protocol at runtime. This is useful when integrating with third-party libraries or legacy code you cannot modify. Note that m.act(store) without a custom string representation will not produce useful output unless the class defines __str__. The next section shows how to supply one.

Custom string representation

stringify_func accepts a callable that takes the instance and returns a string. The lambda here produces a short, labelled description. Any callable works — a method on another object, a formatting helper, or a template renderer.

Template integration with TableQuery

query_type=TableQuery tells Mellea which query component to use when m.query() is called on this object. template_order controls the fallback chain for rendering: try the class-specific template first ("*"), then fall back to the generic "Table" template.

Field selection and inline templates

fields_include limits which attributes are visible to the model. Sensitive or irrelevant fields stay private. The template parameter is a Jinja2 string rendered with the included fields as context variables.

Exposing methods as tools

funcs_include whitelists specific methods. The model can call from_markdown as a tool when m.transform() runs. By default, any method that has a docstring (and whose docstring does not contain [no-index]) is exposed. funcs_include overrides that default to give you precise control.

Full file

Key observations

@mify is additive. It does not subclass, wrap, or monkey-patch the class in a destructive way. Existing behaviour is unchanged; the protocol members are added on top. Ad-hoc mification is instance-scoped. Calling mify(instance) mutates only that object. Other instances of the same class are not affected. fields_include is the privacy boundary. If your class holds credentials, internal state, or large fields you do not want sent to the model, list only the fields the model should see. Tool exposure is opt-in by default. Only methods with non-empty docstrings (without [no-index]) are exposed as tools. Use funcs_include to be explicit. What to try next:
  • Read the MObjects and mify concept page for the full design rationale.
  • See docs/examples/mify/rich_document_advanced.py for mify combined with rich document types.
  • See docs/examples/mify/rich_table_execute_basic.py for mifying table objects for data manipulation.

See also: MObjects and mify | Tutorial 05: MIFYing Legacy Code | Examples Index