Skip to content

fix: guard against null entries in LLM extraction response#338

Open
dingguagua996-stack wants to merge 1 commit intoCortexReach:masterfrom
dingguagua996-stack:fix/issue-capture-null
Open

fix: guard against null entries in LLM extraction response#338
dingguagua996-stack wants to merge 1 commit intoCortexReach:masterfrom
dingguagua996-stack:fix/issue-capture-null

Conversation

@dingguagua996-stack
Copy link
Copy Markdown

Summary

When the smartExtraction LLM returns malformed JSON with null entries in the memories array (e.g. {memories: [null, {...}]}), accessing raw.category throws:

TypeError: Cannot read properties of null (reading 'category')

This crash kills the entire auto-capture pipeline, producing the log:

memory-lancedb-pro: capture failed: TypeError: Cannot read properties of null (reading 'category')

Root Cause

In src/smart-extractor.ts, the candidate processing loop iterates over result.memories without checking if individual entries are valid objects:

for (const raw of result.memories) {
  const category = normalizeCategory(raw.category ?? ""); // 💥 raw is null!
}

Some LLM models (especially under load or rate limiting) occasionally return arrays with null entries mixed in with valid objects.

Fix

Add a null/type guard at the top of the loop to skip invalid entries gracefully:

if (!raw || typeof raw !== 'object') {
  invalidCategoryCount++;
  continue;
}

Evidence

Observed 7 occurrences across multiple days in production:

2026-03-21T16:14:36 capture failed: TypeError: Cannot read properties of null (reading 'category')
2026-03-21T20:26:05 capture failed: TypeError: Cannot read properties of null (reading 'category')
2026-03-22T20:06:13 capture failed: TypeError: Cannot read properties of null (reading 'category')
2026-03-24T02:46:33 capture failed: TypeError: Cannot read properties of null (reading 'category')

Files Changed

File Change
src/smart-extractor.ts Add null/type guard for candidate entries in extraction loop

When the LLM returns malformed JSON with null entries in the memories
array (e.g. {memories: [null, {...}]}), accessing raw.category throws:
  TypeError: Cannot read properties of null (reading 'category')

This crash was observed in production (7 occurrences across multiple
days), causing the entire auto-capture pipeline to fail silently.

Add a null/type guard at the top of the candidate processing loop to
skip invalid entries gracefully instead of crashing.

Fixes capture failures logged as:
  memory-lancedb-pro: capture failed: TypeError: Cannot read
  properties of null (reading 'category')
@AliceLJY
Copy link
Copy Markdown
Collaborator

看了代码,fix 方向没问题,smart-extractor 里确实缺 null guard。

我们刚发了 v1.1.0-beta.10,main 有比较大的变动,麻烦 rebase 到最新 master。另外补一个回归测试:在 test/smart-extractor-branches.mjs 里加一个 memories: [null, validEntry] 的 case,确认不会炸。

rebase + 补测试后可以合 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants