Skip to main content

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

id_
string
Unique identifier for the node (auto-generated if not provided)
embedding
number[]
Vector embedding of the node content
metadata
Record<string, any>
Arbitrary metadata associated with the node
excludedEmbedMetadataKeys
string[]
Metadata keys to exclude when generating embeddings
excludedLlmMetadataKeys
string[]
Metadata keys to exclude when sending to LLM
relationships
Record<NodeRelationship, RelatedNodeInfo>
Relationships to other nodes (parent, child, next, previous)

Methods

getContent
method
Get node content with metadata based on mode
getContent(metadataMode: MetadataMode): string
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

text
string
The text content of the node
startCharIdx
number
Start character index in the parent document
endCharIdx
number
End character index in the parent document
textTemplate
string
Template for formatting text with metadata
metadataTemplate
string
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

id_
string
Document ID (used as doc_id in chunks)
metadata
Record<string, any>
Document-level metadata

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;
}

EngineResponse

Response from query or chat engines.
import { EngineResponse } from "@llamaindex/core/schema";

Properties

response
string
The generated response text
sourceNodes
NodeWithScore[]
Source nodes used to generate the response
metadata
Record<string, any>
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()
  }
});

Metadata Modes

enum MetadataMode {
  ALL = "all",      // Include all metadata
  EMBED = "embed",  // For embedding (excludes excludedEmbedMetadataKeys)
  LLM = "llm",      // For LLM (excludes excludedLlmMetadataKeys)
  NONE = "none"     // No metadata
}

TransformComponent

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.