CBlock
(a content block wrapping a string value), Component (an abstract composable
generative unit), ModelOutputThunk (a lazily-evaluated model response),
Context and ContextTurn (stateful conversation history containers),
TemplateRepresentation (the structured rendering of a component for prompt
templates), ImageBlock, and ModelToolCall. Understanding these types is
the starting point for building custom components or sampling strategies.
Functions
FUNC blockify
CBlock, leaving CBlock and Component objects unchanged.
Args:
s: A plain string,CBlock, orComponentto normalise.
- A
CBlockwrappingsif it was a string; otherwisesunchanged.
Exception: Ifsis not astr,CBlock, orComponent.
FUNC get_images_from_component
Component, or None if absent or empty.
Args:
c: TheComponentwhoseimagesattribute is inspected.
- A non-empty list of
ImageBlockobjects if the component has an imagesattribute with at least one element;Noneotherwise.
Classes
CLASS CBlock
A CBlock is a block of content that can serve as input to or output from an LLM.
Args:
value: The underlying string content of the block.meta: Optional metadata about this block (e.g., the inference engine’s completion object). Defaults to an empty dict.cache: IfTrue, the inference engine may store the KV cache for this block. Experimental.
FUNC value
FUNC value
CLASS ImageBlock
A ImageBlock represents an image (as base64 PNG).
Args:
value: A valid base64-encoded PNG string (with or without a data URI prefix).meta: Optional metadata to associate with this image block.
FUNC is_valid_base64_png
s: The string to validate, optionally prefixed with a data URI header.
Trueif the string decodes to a PNG image,Falseotherwise.
FUNC pil_to_base64
image: The PIL image to encode.
- A base64-encoded string of the image serialised as PNG.
FUNC from_pil_image
ImageBlock from a PIL image object.
Converts the image to a base64-encoded PNG string and wraps it in a new
ImageBlock instance.
Args:
image: The PIL image to encode.meta: Optional metadata to associate with the block.
- A new
ImageBlockcontaining the base64-encoded PNG.
CLASS ComponentParseError
Raised by Component.parse() when the underlying parsing method throws an exception.
CLASS Component
A Component is a composite data structure that is intended to be represented to an LLM.
Methods:
FUNC parts
Component.
Returns:
- list[Component | CBlock]: A list of child
ComponentorCBlockobjects that make - up this component. The list may be empty for leaf components.
NotImplementedError: If the concrete subclass has not overridden this method.
FUNC format_for_llm
Component into a TemplateRepresentation or plain string for LLM consumption.
Returns:
- TemplateRepresentation | str: A structured
TemplateRepresentation(for components - with tools, fields, or templates) or a plain string for simple components.
NotImplementedError: If the concrete subclass has not overridden this method.
FUNC parse
S from a given ModelOutputThunk.
Delegates to the component’s underlying _parse method and wraps any
exception in a ComponentParseError for uniform error handling.
Args:
computed: The model output thunk whose value should be parsed.
- The parsed result produced by
_parse, typed according to the component’s type parameter.
ComponentParseError: If the underlying_parsecall raises any exception.
CLASS GenerateType
Used to track what functions can be used to extract a value from a ModelOutputThunk.
Attributes:
NONE: No generation function has been set; the thunk is either already computed or uninitialized.ASYNC: The generation function is async-compatible;avalue/astreammay be used.SYNC: The generation function is synchronous only; async extraction methods are unavailable.
CLASS ModelOutputThunk
A ModelOutputThunk is a special type of CBlock that we know came from a model’s output. It is possible to instantiate one without the output being computed yet.
Args:
value: The raw model output string, orNoneif not yet computed.meta: Optional metadata from the inference engine (e.g., completion object).parsed_repr: An already-parsed representation to attach; set when re-wrapping existing output.tool_calls: Tool calls returned by the model alongside the text output.
FUNC is_computed
FUNC value
FUNC value
FUNC avalue
- The complete text output from the model.
Exception: Propagates any errors from the underlying inference engine api request.RuntimeError: If called when the ModelOutputThunk’s generate function is not async compatible.
FUNC astream
- The accumulated output text up to and including the newly received chunk(s).
Exception: Propagates any errors from the underlying inference engine api request.RuntimeError: If called when the ModelOutputThunk’s generate function is not async compatible.
CLASS ContextTurn
A turn of model input and model output.
Args:
model_input: The input component or content block for this turn, orNonefor an output-only partial turn.output: The model’s output thunk for this turn, orNonefor an input-only partial turn.
CLASS Context
A Context is used to track the state of a MelleaSession.
A context is immutable. Every alteration leads to a new context.
Attributes:
is_root_node:Truewhen this context is the root (empty) node of the linked list.previous_node: The context node from which this one was created, orNonefor the root node.node_data: The data associated with this context node, orNonefor the root node.is_chat_context: Whether this context operates in chat (multi-turn) mode.
FUNC from_previous
previous: The existing context to extend.data: The component or content block to associate with the new node.
- A new context instance whose
previous_nodeisprevious.
FUNC reset_to_new
- A freshly initialised root context with no data or history.
FUNC is_root_node
FUNC previous_node
FUNC node_data
FUNC is_chat_context
FUNC as_list
last_n_components is None, then all components are returned.
Args:
last_n_components: Maximum number of most-recent components to include. PassNoneto return the full history.
- list[Component | CBlock]: Components in chronological order (oldest first).
FUNC actions_for_available_tools
None if it is not possible to construct such a list. Can be used to make
the available tools differ from the tools of all the actions in the context. Can be
overridden by subclasses.
Returns:
- list[Component | CBlock] | None: The list of actions whose tools should be made
- available during generation, or
Noneif unavailable.
FUNC last_output
ModelOutputThunk found within the last N context components.
Args:
check_last_n_components: Number of most-recent components to search through. Defaults to 3.
- ModelOutputThunk | None: The most recent output thunk, or
Noneif none is found - within the searched components.
FUNC last_turn
FUNC add
c to this context.
Args:
c: The component or content block to add to the context.
- A new context node with
cas its data and this context as its previous node.
FUNC view_for_generation
None if it is not possible to construct such a list (e.g., the context
is in an inconsistent state). Concrete subclasses define the ordering and filtering logic.
Returns:
- list[Component | CBlock] | None: An ordered list of components suitable for passing
- to a backend, or
Noneif generation is not currently possible.
CLASS AbstractMelleaTool
Abstract base class for Mellea Tool.
Attributes:
name: The unique name used to identify the tool in JSON descriptions and tool-call dispatch.as_json_tool: A JSON-serialisable description of the tool, compatible with the function-calling schemas expected by supported inference backends.
FUNC run
*args: Positional arguments forwarded to the tool implementation.**kwargs: Keyword arguments forwarded to the tool implementation.
- The result produced by the tool; the concrete type depends on the implementation.
FUNC as_json_tool
CLASS TemplateRepresentation
Representing a component as a set of important attributes that can be consumed by the formatter.
Args:
obj: The original component object being represented.args: Named arguments extracted from the component for template substitution.tools: Tools available for this representation, keyed by the tool’s function name. Defaults toNone.fields: An optional ordered list of field values for positional templates.template: An optional Jinja2 template string to use when rendering.template_order: An optional ordering hint for template sections/keys.images: Optional list of image blocks associated with this representation.
CLASS GenerateLog
A dataclass for capturing log entries for a single generation call.
GenerateLog provides a structured way to include various details in log entries, making it useful for maintaining detailed
records of events or operations where context and additional data are significant.
Args:
date: Timestamp when the generation was logged.prompt: The prompt string or chat-message list sent to the model.backend: Identifier of the inference backend used for this generation.model_options: Model configuration options applied to this call.model_output: The raw output returned by the backend API.action: The component or block that triggered the generation.result: TheModelOutputThunkproduced by this generation call.is_final_result: Whether this log entry corresponds to the definitive final result.extra: Arbitrary extra metadata to attach to the log entry.
CLASS ModelToolCall
A dataclass for capturing the tool calls a model wants to make.
Provides a unified way to call tools post generation.
Args:
name: The name of the tool the model requested to call.func: TheAbstractMelleaToolinstance that will be invoked.args: The keyword arguments the model supplied for the tool call.
FUNC call_func
- The value returned by
func.run(**args); the concrete type depends on the tool.