Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ucbepic Docetl Directive OperatorFusion

From Leeroopedia


Knowledge Sources
Domains Pipeline_Optimization, LLM_Operations
Last Updated 2026-02-08 00:00 GMT

Overview

Concrete tool for combining two sequential operations into a single operation to reduce LLM processing costs provided by the DocETL reasoning optimizer.

Description

The OperatorFusionDirective class combines two sequential operations into a single operation to reduce LLM processing costs by avoiding duplicate document reads and API calls. It can fuse Map+Filter, Map+Map, and Map+Reduce patterns into a single combined operation with a merged prompt that performs both tasks simultaneously.

Usage

The MOAR agent applies this directive when there are two sequential LLM operations processing the same document keys and the goal is to optimize cost by combining them into one operation. Two consecutive operations must be specified as target operators.

Code Reference

Source Location

Signature

class OperatorFusionDirective(Directive):
    name = "operator_fusion"
    description = "Combines two sequential operations into a single operation to reduce LLM processing costs."

    def check_applicability(self, ...) -> Tuple[bool, str]: ...
    def apply(self, ...) -> Tuple[List[Dict], List[Dict], str, dict]: ...

Import

from docetl.reasoning_optimizer.directives.operator_fusion import OperatorFusionDirective

I/O Contract

Inputs

Name Type Required Description
op_config Dict Yes Operation configuration to transform
pipeline_ops List[Dict] Yes Full pipeline operations list
op_idx int Yes Index of target operation
dataset_descriptions Dict Yes Dataset schema descriptions

Outputs

Name Type Description
new_ops List[Dict] Transformed operation configs
new_steps List[Dict] Updated pipeline steps
explanation str Human-readable description of changes
metadata dict Additional metadata about the transformation

Usage Examples

# Directives are typically invoked by the MOAR agent automatically
# Example of manual invocation:
from docetl.reasoning_optimizer.directives.operator_fusion import OperatorFusionDirective

directive = OperatorFusionDirective()
applicable, reason = directive.check_applicability(op_config, pipeline_ops, op_idx, dataset_descriptions)
if applicable:
    new_ops, new_steps, explanation, metadata = directive.apply(op_config, pipeline_ops, op_idx, dataset_descriptions)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment