Overview
The Schema module defines the foundational data structures used throughout LlamaIndex.TS, including documents, nodes, relationships, and response types.
BaseNode
Base class for all node types in LlamaIndex.
import { BaseNode } from "@llamaindex/core/schema" ;
Properties
Unique identifier for the node (auto-generated if not provided)
Vector embedding of the node content
Arbitrary metadata associated with the node
excludedEmbedMetadataKeys
Metadata keys to exclude when generating embeddings
Metadata keys to exclude when sending to LLM
relationships
Record<NodeRelationship, RelatedNodeInfo>
Relationships to other nodes (parent, child, next, previous)
Methods
Get node content with metadata based on mode getContent ( metadataMode : MetadataMode ): string
Mode: ALL, EMBED, LLM, or NONE
Convert node to RelatedNodeInfo for relationship storage asRelatedNodeInfo (): RelatedNodeInfo
TextNode
Node representing a chunk of text, extending BaseNode.
import { TextNode } from "@llamaindex/core/schema" ;
Additional Properties
The text content of the node
Start character index in the parent document
End character index in the parent document
Template for formatting text with metadata
Template for formatting metadata
Example
const node = new TextNode ({
text: "LlamaIndex is a data framework for LLM applications." ,
metadata: {
source: "documentation" ,
page: 1
}
});
console . log ( node . id_ ); // Auto-generated UUID
console . log ( node . getContent ( MetadataMode . ALL )); // Text with metadata
Document
Represents a complete document, extends TextNode.
import { Document } from "@llamaindex/core/schema" ;
Additional Properties
Document ID (used as doc_id in chunks)
Example
const document = new Document ({
text: "Full document text..." ,
metadata: {
title: "My Document" ,
author: "John Doe" ,
date: "2024-01-01"
}
});
NodeWithScore
Node with a relevance score, used in retrieval results.
interface NodeWithScore < T extends BaseNode = BaseNode > {
node : T ;
score ?: number ;
}
Relevance score (typically 0-1, higher is more relevant)
EngineResponse
Response from query or chat engines.
import { EngineResponse } from "@llamaindex/core/schema" ;
Properties
The generated response text
Source nodes used to generate the response
Additional metadata about the response
Example
const response : EngineResponse = {
response: "LlamaIndex is a data framework..." ,
sourceNodes: [
{
node: textNode ,
score: 0.95
}
],
metadata: {
totalTokens: 150
}
};
Node Relationships
enum NodeRelationship {
SOURCE = "source" ,
PREVIOUS = "previous" ,
NEXT = "next" ,
PARENT = "parent" ,
CHILD = "child"
}
Example: Node with Relationships
const parentDoc = new Document ({ text: "Parent document" });
const childNode = new TextNode ({
text: "Child chunk" ,
relationships: {
[NodeRelationship. SOURCE ]: parentDoc . asRelatedNodeInfo ()
}
});
enum MetadataMode {
ALL = "all" , // Include all metadata
EMBED = "embed" , // For embedding (excludes excludedEmbedMetadataKeys)
LLM = "llm" , // For LLM (excludes excludedLlmMetadataKeys)
NONE = "none" // No metadata
}
Base class for components that transform nodes (e.g., embeddings, parsers).
abstract class TransformComponent < T > {
abstract transform ( nodes : BaseNode [], options ?: any ) : T ;
}
Example
import { TransformComponent } from "@llamaindex/core/schema" ;
class MyTransform extends TransformComponent < Promise < BaseNode []>> {
async transform ( nodes : BaseNode []) : Promise < BaseNode []> {
// Transform nodes
return nodes . map ( node => {
// Add custom metadata
node . metadata . processed = true ;
return node ;
});
}
}
FileReader
Base interface for reading files into documents.
interface BaseReader < T = any > {
loadData ( ... args : any []) : Promise < Document []>;
}
Example
import { SimpleDirectoryReader } from "llamaindex" ;
const reader = new SimpleDirectoryReader ();
const documents = await reader . loadData ( "./docs" );
Output Parsers
interface BaseOutputParser < T > {
parse ( output : string ) : T ;
format ( prompt : string ) : string ;
}
Output parsers transform LLM text outputs into structured data.