Implementation:Ucbepic Docetl Directive SwapWithCode
| Knowledge Sources | |
|---|---|
| Domains | Pipeline_Optimization, LLM_Operations |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for replacing a Reduce operation with a Code Reduce plus optional Map for deterministic logic provided by the DocETL reasoning optimizer.
Description
The SwapWithCodeDirective class replaces a Reduce operation with a Code Reduce operation for deterministic logic plus an optional Map operation to format the output. The Code Reduce handles the core reduction logic (like counting, collecting, or aggregating) while the optional Map operation converts the result to match the expected schema format. This eliminates LLM costs for straightforward aggregation tasks.
Usage
The MOAR agent applies this directive when a reduce operation performs logic that can be implemented more efficiently or deterministically with code rather than an LLM. Examples include counting distinct values, finding most common elements, basic aggregations, set operations, or mathematical computations.
Code Reference
Source Location
- Repository: Ucbepic_Docetl
- File: docetl/reasoning_optimizer/directives/swap_with_code.py
- Lines: 1-339
Signature
class SwapWithCodeDirective(Directive):
name = "swap_with_code"
description = "Replaces a Reduce operation with a Code Reduce plus optional Map for deterministic logic."
def check_applicability(self, ...) -> Tuple[bool, str]: ...
def apply(self, ...) -> Tuple[List[Dict], List[Dict], str, dict]: ...
Import
from docetl.reasoning_optimizer.directives.swap_with_code import SwapWithCodeDirective
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.swap_with_code import SwapWithCodeDirective
directive = SwapWithCodeDirective()
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)