How to use mappings¶
Overview¶
Mappings describe how data flows along a graph edge. Instead of hand-writing adapter code between nodes, you declare a list of small operations — passthrough, rename, constant, default — and Zeroth applies them when the orchestrator traverses the edge. This guide shows how to declare a mapping between two nodes and apply it to real data.
Minimal example¶
from zeroth.contracts.mappings import (
EdgeMapping,
MappingExecutor,
PassthroughMappingOperation,
RenameMappingOperation,
ConstantMappingOperation,
DefaultMappingOperation,
)
# Declare the mapping on an edge from "extract" -> "summarise"
mapping = EdgeMapping(
edge_id="extract__to__summarise",
operations=[
PassthroughMappingOperation(target_path="text", source_path="body"),
RenameMappingOperation(target_path="source_url", source_path="url"),
ConstantMappingOperation(target_path="language", value="en"),
DefaultMappingOperation(
target_path="max_tokens",
source_path="limits.max_tokens",
default_value=512,
),
],
)
upstream_output = {"body": "Hello world", "url": "https://example.com"}
downstream_input = MappingExecutor().apply(mapping, upstream_output)
# -> {"text": "Hello world", "source_url": "https://example.com",
# "language": "en", "max_tokens": 512}
In a real graph you attach the EdgeMapping to the edge definition; the orchestrator invokes MappingExecutor automatically during traversal.
Common patterns¶
- Passthrough-only edges — when two nodes already share the same schema, a single
PassthroughMappingOperationper field is enough. Prefer this overRenameMappingOperationwhen the names already match. - Constant injection for modes/flags — use
ConstantMappingOperationto pin a downstream field (e.g."language": "en") without exposing it in the upstream output contract. - Defaults for optional inputs — use
DefaultMappingOperationwith asource_pathofNoneto inject a default that upstream nodes never have to know about. - Dotted paths — every
source_pathandtarget_pathsupports dotted notation to walk into nested dicts; use it to unpack structured upstream outputs.
Pitfalls¶
- Silent field loss. If you forget to declare an operation for a field the downstream contract requires, validation fails at runtime — not at graph load. Run
MappingValidatorin tests to catch missing fields early. - Mixing
constantanddefaultsemantics.ConstantMappingOperationalways overrides;DefaultMappingOperationonly fills in when the source is missing. Using constant where you meant default hides upstream values. - Non-JSON-serialisable constants. Mapping operations are Pydantic models; any value you put into
ConstantMappingOperation.valuemust round-trip through JSON to be persistable in a run snapshot. - Forgetting discriminator.
MappingOperationis a discriminated union; each subclass has aLiteraloperationtag. When constructing from a dict, always include"operation": "passthrough"(etc.). - Editing vs versioning. Treat mappings as versioned alongside their edges — editing a mapping in place invalidates old run snapshots.