lobehub/lobehub · error · Error

Invalid ReasoningGraph: ${path}${issue?.message ?? 'unknown

Error message

Invalid ReasoningGraph: ${path}${issue?.message ?? 'unknown error'}

What it means

Thrown by readGraphConfig when the provided graph config file fails Zod validation against ReasoningGraphSchema. The message includes the first Zod issue's path (dot-joined field path) and its message, pinpointing which part of the graph structure is invalid. This is a schema-conformance check on the agent's reasoning graph definition.

Source

Thrown at apps/cli/src/commands/agent.ts:29

  replayAgentEvents,
  streamAgentEvents,
  streamAgentEventsViaWebSocket,
} from '../utils/agentStream';
import { resolveLocalDeviceId } from '../utils/device';
import { confirm, outputJson, printTable, truncate } from '../utils/format';
import { log, setVerbose } from '../utils/logger';
import { resolveAgentId } from './agent/resolveAgentId';
import { registerAgentSpaceFsCommand } from './agent/spaceFs';

const readGraphConfig = async (graphFile: string): Promise<unknown> => {
  const content = await readFile(graphFile, 'utf8');
  const graph = JSON.parse(content);
  const result = ReasoningGraphSchema.safeParse(graph);

  if (!result.success) {
    const issue = result.error.issues[0];
    const path = issue?.path.length ? `${issue.path.join('.')}: ` : '';
    throw new Error(`Invalid ReasoningGraph: ${path}${issue?.message ?? 'unknown error'}`);
  }

  return result.data;
};

const readJsonObjectFile = async (
  filePath: string,
  label: string,
): Promise<Record<string, unknown>> => {
  const content = await readFile(filePath, 'utf8');
  const parsed = JSON.parse(content);

  if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
    throw new Error(`${label} JSON must be a plain object`);
  }

  return parsed as Record<string, unknown>;
};

View on GitHub (pinned to 10f24d7ade)

Solutions

  1. Read the path in the error message: it tells you exactly which field failed (e.g. 'nodes.0.type' or 'edges').
  2. Check the ReasoningGraphSchema definition in @lobechat/types to see the required shape for the failing field.
  3. Validate the file against the schema programmatically: import { ReasoningGraphSchema } from '@lobechat/types' and run safeParse to see all issues.
  4. Compare against a known-good graph file example from the docs or tests.

Example fix

// Error: 'Invalid ReasoningGraph: nodes.0.type: invalid enum value'
// Before:
{ "nodes": [{ "id": "n1", "type": "startpoint" }], "edges": [] }
// After (correct enum value per schema):
{ "nodes": [{ "id": "n1", "type": "start" }], "edges": [] }
Defensive patterns

Strategy: validation

Validate before calling

// Validate the graph file against the schema before running the CLI
import { readFile } from 'node:fs/promises';
import { ReasoningGraphSchema } from '@lobechat/types';

async function validateGraphFile(path: string): Promise<void> {
  const content = JSON.parse(await readFile(path, 'utf8'));
  const result = ReasoningGraphSchema.safeParse(content);
  if (!result.success) {
    for (const issue of result.error.issues) {
      console.error(`${issue.path.join('.')}: ${issue.message}`);
    }
    process.exit(1);
  }
}

Type guard

import { ReasoningGraphSchema } from '@lobechat/types';

function isReasoningGraph(value: unknown): boolean {
  return ReasoningGraphSchema.safeParse(value).success;
}

Try / catch

try {
  await runAgentCommand({ graph: graphFile });
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Invalid ReasoningGraph:')) {
    // The error message includes the zod path and issue
    console.error('Graph validation failed:', e.message);
    console.error('Check the field path in the message and fix the graph file.');
  }
  throw e;
}

Prevention

When it happens

Trigger: Running lh agent with --graph <file> where the file is valid JSON but doesn't conform to ReasoningGraphSchema — e.g. missing required nodes, invalid edge definitions, wrong node types, or unknown top-level keys.

Common situations: 1) Graph file was authored against an older schema version that has since changed. 2) Typo in a node type or edge kind field. 3) Missing required fields like 'nodes' or 'edges'. 4) Circular or malformed edge references.

Related errors


AI-assisted analysis of lobehub/lobehub@10f24d7ade (2026-08-12). Data as JSON: /api/errors/991132bccb84a9e7. Report an issue: GitHub.