Skip to content

Fix #2456: Malformed extraction JSON silently becomes an empty successful /product/add resp - #2457

Open
Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.36from
Memtensor-AI:bugfix/autodev-2456-20261003133440133
Open

Memtensor-AI wants to merge 2 commits into
MemTensor:dev-v2.0.36from
Memtensor-AI:bugfix/autodev-2456-20261003133440133

Conversation

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

Description

Fix issue #2456: malformed extraction JSON with a trailing comma before } or ] (common output from GLM / Z.AI / Kimi style models) was silently converted to {} by parse_json_result, which downstream surfaced as POST /product/add returning {"code":200,"message":"Memory added successfully","data":[]} with zero memories persisted. The multi_modal_struct._process_string_fine exception-based fallback never fired because no exception was raised. Related to #1355 (fixed by #1977) but a distinct failure path — that fix only corrected SimpleStructMemReader's fallback key.

The fix adds a narrow last-chance repair in src/memos/mem_reader/utils.py::parse_json_result: a _strip_trailing_commas scanner walks the payload character-by-character, tracks whether it is inside a quoted string (respecting backslash-escaped quotes), and removes commas that sit immediately before } or ] outside strings. On successful repair it emits a [JSONParse] Repaired malformed JSON... warning so the previously-silent failure mode is observable; on genuine failure it still returns {} to preserve the historical contract for the 15 call sites. Zero signature change, zero API contract change.

Testing: 11 new regression cases in tests/mem_reader/test_parse_json_result.py cover the exact repro from the bug body, trailing commas before }/]/newlines/fenced code blocks, safety cases (literal ,} inside strings must survive, escaped quotes must not confuse the scanner), and the preserved {} return for genuinely malformed JSON. All 11 pass. Adjacent suites (test_simple_structure, test_simple_struct_fallback) still green: 20/20. Full tests/mem_reader/ run: 80 passed, 2 pre-existing environment failures (test_doc_local_file, test_doc_mixed) requiring the optional markitdown extra — confirmed reproducible on untouched dev-v2.0.36 HEAD. Ruff format + check clean. Archive synced and pushed to memos-autodev-specs main (commit 33ce785).

Related Issue (Required): Fixes #2456

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Not run; documentation-only change.

  • Unit Test
  • Test Script Or Test Steps (please provide)
  • Pipeline Automated API Test (please provide)

Checklist

  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have created related documentation issue/PR in MemOS-Docs (if applicable)
  • I have linked the issue to this PR (if applicable)
  • I have mentioned the person who will review this PR

@WeiminLee please review this PR.

Reviewer Checklist

`parse_json_result` previously returned `{}` when the LLM emitted a trailing
comma before `}` or `]` (e.g. `[{"value": "x"},]`). The empty dict
propagated through `multi_modal_struct._process_string_fine` — whose
exception-based fallback never fired because no exception was raised — and
surfaced as `POST /product/add` returning
`{"code":200, "message":"Memory added successfully", "data": []}` with no
memories stored. Related to MemTensor#1355 / fixed-by MemTensor#1977, but a distinct failure
path: the SimpleStructMemReader fallback-key fix does not cover this case.

Add a narrow last-chance repair that strips commas sitting immediately
before `}` or `]` **outside** quoted strings (respecting backslash-escaped
quotes), then reparses with `json.loads`. On success emit a
`[JSONParse] Repaired malformed JSON...` warning so the previously-silent
failure mode is observable. On repair failure keep returning `{}` to
preserve the historical contract for the 15 call sites.

Regression tests in `tests/mem_reader/test_parse_json_result.py` cover the
exact reproduction from the bug report plus safety cases (literal `,}`
inside strings, escaped quotes, genuinely-malformed JSON still returns
`{}`).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Memtensor-AI Memtensor-AI added ai:generated Generated or modified by AI | 由 AI 生成或修改 area:memory 记忆存储、检索、更新、召回逻辑 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Oct 3, 2026
@Memtensor-AI

Memtensor-AI commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

🤖 Open Code Review

Target: PR #2457
Task: f2cc0bcd220b1a33
Base: dev-v2.0.36
Head: bugfix/autodev-2456-20261003133440133
Head SHA: 131b5a39fbdbae764aaa76adc8ad5dba56e83925

✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s).

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

🔧 Open Code Review requested Agent fix

Open Code Review found 3 issue(s). I have resumed the development Agent to fix them.

  • Task: f2cc0bcd220b1a33
  • Fix attempt: 1/2
  • Finding delta: 0 repeated / 3 new / 0 likely resolved

The Agent will push a new commit to this PR branch. OCR will recheck after the commit is pushed.

…mma repair

Three follow-up issues from the Open Code Review on PR MemTensor#2457:

1. tests/mem_reader/test_parse_json_result.py — the escaped-quote
   regression actually had its trailing ``,}`` *inside* the quoted
   value, so the raw string was already valid JSON and
   ``_strip_trailing_commas`` was never exercised.  Move the trailing
   comma outside the string and assert the inner commas (and the
   escaped quotes around them) survive the repair.  Also add a
   positive case for ``,,}`` runs so the fix below is pinned.

2. src/memos/mem_reader/utils.py — ``_strip_trailing_commas`` only
   remembered the *latest* candidate comma, so ``{"a":1,,}`` was
   repaired to ``{"a":1,}`` which still fails ``json.loads``.  The
   outer ``logger.warning`` only fires when ``repaired == t``, so the
   silent fallthrough re-created the exact failure mode (MemTensor#2456) the
   repair was meant to kill.  Track *all* pending comma indices and
   flush the whole run on a closer; a string open or any other
   non-whitespace token clears them.

3. src/memos/mem_reader/utils.py — when the repair modified the text
   but the result still failed ``json.loads``, the inner except
   silently swallowed the second error with no diagnostic.  Emit a
   ``logger.debug`` with the repair-attempt error so future debugging
   has a breadcrumb; the outer WARNING still fires with the original
   decode error for operator visibility.

Verified: ``PYTHONPATH=src pytest tests/mem_reader/test_parse_json_result.py``
12 passed (adds 1 new case).  The 2 pre-existing failures in
``tests/mem_reader/test_coarse_memory_type.py`` are unrelated
(missing optional ``markitdown`` dep).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:generated Generated or modified by AI | 由 AI 生成或修改 area:memory 记忆存储、检索、更新、召回逻辑 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants