mastra-ai/mastra · error

Node ${id} not found

Error message

Node ${id} not found

What it means

updateNodeContent(id, newContent) looks up the node by id in the internal Map and throws if it does not exist. Only the node's content is mutated; the embedding and edges stay as-is, so the id must reference a node created via addNode()/createGraph().

Source

Thrown at packages/rag/src/graph-rag/index.ts:176

      }
    }

    // Assign directly rather than via addEdge: the serialized edge list already
    // contains both directions, and addEdge would add the reverse edge again.
    graph.edges = (snapshot.edges ?? []).map(edge => ({ ...edge }));

    return graph;
  }

  clear(): void {
    this.nodes.clear();
    this.edges = [];
  }

  updateNodeContent(id: string, newContent: string): void {
    const node = this.nodes.get(id);
    if (!node) {
      throw new Error(`Node ${id} not found`);
    }
    node.content = newContent;
  }

  // Get neighbors of a node
  private getNeighbors(nodeId: string, edgeType?: string): { id: string; weight: number }[] {
    return this.edges
      .filter(edge => edge.source === nodeId && (!edgeType || edge.type === edgeType))
      .map(edge => ({
        id: edge.target,
        weight: edge.weight,
      }))
      .filter(node => node !== undefined);
  }

  // Calculate cosine similarity between two vectors
  private cosineSimilarity(vec1: number[], vec2: number[]): number {
    if (!vec1 || !vec2) {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Use the ids returned by getNodes() (e.g. node.id) rather than external document ids.
  2. If you need stable ids, build the graph with addNode() supplying your own GraphNode.id instead of createGraph().
  3. Check the node exists first with getNodes().some(n => n.id === id) before updating.
  4. If the graph was cleared or rebuilt, refresh your stored id references.

Example fix

// before
graph.updateNodeContent(docId, newText);
// after
const node = graph.getNodes().find(n => n.metadata?.docId === docId);
if (!node) throw new Error(`No graph node for document ${docId}`);
graph.updateNodeContent(node.id, newText);
Defensive patterns

Strategy: validation

Validate before calling

const exists = graph.getNodes().some(n => n.id === id);
if (!exists) throw new Error(`Node ${id} not in graph; known ids: ${graph.getNodes().map(n => n.id).slice(0, 5).join(',')}...`);

Type guard

const nodeExists = (graph: GraphRAG, id: string): boolean =>
  graph.getNodes().some(n => n.id === id);

Try / catch

try {
  graph.updateNodeContent(id, newContent);
} catch (e) {
  if ((e as Error).message === `Node ${id} not found`) {
    // look up the correct node id via getNodes()/metadata before retrying
  } else throw e;
}

Prevention

When it happens

Trigger: Calling graph.updateNodeContent('some-id', text) with an id that was never added, an id computed with the wrong scheme (createGraph uses numeric string indices '0','1',...), or after clear() was called.

Common situations: Assuming ids are document/chunk identifiers when createGraph actually assigns index-based ids; using ids from a different GraphRAG instance; stale ids kept from before the graph was rebuilt from new chunks.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/d841e60c7be346ed. Report an issue: GitHub.