Implementation:Run llama Llama index BaseObjectNodeMapping
Overview
The BaseObjectNodeMapping module defines the abstract base class and a concrete implementation for mapping arbitrary Python objects to LlamaIndex nodes and back. It provides the foundational contract that all object-to-node mapping strategies must implement. This module resides at llama-index-core/llama_index/core/objects/base_node_mapping.py (181 lines).
Purpose
This module establishes the bidirectional mapping interface between arbitrary Python objects and BaseNode instances. This mapping is critical for the ObjectIndex system, enabling non-text objects (tools, SQL tables, custom objects) to be stored in, retrieved from, and persisted alongside LlamaIndex indices.
Constants
| Name | Value | Description |
|---|---|---|
DEFAULT_PERSIST_FNAME |
"object_node_mapping.pickle" |
Default filename used when persisting object-node mappings to disk. |
OT |
TypeVar("OT") |
Generic type variable representing the type of objects being mapped. |
Key Components
Class: BaseObjectNodeMapping (Abstract)
An abstract generic base class (Generic[OT]) that defines the interface for all object-to-node mappings.
Abstract Methods
| Method | Signature | Description |
|---|---|---|
from_objects |
@classmethod (objs: Sequence[OT], *args, **kwargs) -> BaseObjectNodeMapping |
Factory method to initialize a mapping from a list of objects. Must be implemented by subclasses that need initial object registration. |
obj_node_mapping |
@property -> Dict[Any, Any] |
Returns the internal data structure mapping between nodes and objects. |
_add_object |
(obj: OT) -> None |
Internal method to add a single object to the mapping store. |
to_node |
(obj: OT) -> BaseNode |
Converts a single object into a BaseNode representation.
|
_from_node |
(node: BaseNode) -> OT |
Internal method to reconstruct an object from a node. |
persist |
(persist_dir, obj_node_mapping_fname) -> None |
Serializes the mapping to disk. |
Concrete Methods
| Method | Signature | Description |
|---|---|---|
validate_object |
(obj: OT) -> None |
Hook for subclasses to validate objects before adding them. Default implementation is a no-op. |
add_object |
(obj: OT) -> None |
Public method that validates the object, then delegates to _add_object.
|
to_nodes |
(objs: Sequence[OT]) -> Sequence[BaseNode] |
Batch conversion that calls to_node() for each object in the sequence.
|
from_node |
(node: BaseNode) -> OT |
Public method that calls _from_node() and then validates the resulting object.
|
Class Method: from_persist_dir
@classmethod
def from_persist_dir(
cls,
persist_dir: str = DEFAULT_PERSIST_DIR,
obj_node_mapping_fname: str = DEFAULT_PERSIST_FNAME,
) -> "BaseObjectNodeMapping[OT]"
Iterates over all direct subclasses of BaseObjectNodeMapping and attempts to load from the given directory. Returns the first successfully loaded mapping. If all subclasses fail (raising NotImplementedError or PickleError), it raises an exception with the accumulated errors.
Class: SimpleObjectNodeMapping
A concrete mapping that works for any Python object with a meaningful string representation (str(obj)).
Constructor
def __init__(self, objs: Optional[Sequence[Any]] = None) -> None
Initializes an internal dictionary self._objs mapping hash(str(obj)) to the object itself. Validates all objects during initialization.
Method Implementations
| Method | Implementation Details |
|---|---|
from_objects |
Class method that simply instantiates SimpleObjectNodeMapping(objs).
|
obj_node_mapping |
Property returning self._objs (Dict[int, Any]). Also has a setter.
|
_add_object |
Adds the object keyed by hash(str(obj)).
|
to_node |
Creates a TextNode with id_=str(hash(str(obj))) and text=str(obj).
|
_from_node |
Looks up the object by hash(node.get_content(metadata_mode=MetadataMode.NONE)).
|
persist |
Creates the directory if needed and pickles the entire mapping instance to object_node_mapping.pickle. Raises ValueError if pickling fails.
|
from_persist_dir |
Class method that unpickles and returns the SimpleObjectNodeMapping instance from disk.
|
Dependencies
| Module | Items Imported |
|---|---|
os |
File system operations (directory creation, path checks). |
pickle |
Serialization and deserialization of the mapping. |
abc |
abstractmethod decorator for abstract interface methods.
|
llama_index.core.schema |
BaseNode, MetadataMode, TextNode
|
llama_index.core.storage.storage_context |
DEFAULT_PERSIST_DIR
|
llama_index.core.utils |
concat_dirs for path construction.
|
Object-to-Node Conversion
In SimpleObjectNodeMapping, the conversion relies on the string representation of objects:
- Object to Node:
str(obj)becomes the text content;hash(str(obj))becomes the node ID. - Node to Object: The node's text content (extracted with
MetadataMode.NONE) is hashed to look up the original object in the internal dictionary.
This approach means that two objects with identical string representations will collide in the mapping dictionary.
Persistence Strategy
The SimpleObjectNodeMapping uses Python's pickle module for persistence. The entire mapping instance is serialized, which means all stored objects must themselves be picklable. The from_persist_dir class method on BaseObjectNodeMapping provides a polymorphic loading mechanism that iterates through known subclasses to find one capable of deserializing the stored file.
Design Notes
- The abstract base class uses the Template Method pattern: public methods (
add_object,from_node) handle validation, then delegate to abstract internal methods (_add_object,_from_node). - The
from_persist_dirclass method on the base class acts as a polymorphic factory, trying each subclass until one succeeds. SimpleObjectNodeMappingrelies onhash(str(obj))for identity, which is simple but assumes objects have stable, unique string representations.