MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

[Bug] clean_json_response crashes with cryptic AttributeError when given None

Open Ptah-CT opened this issue 7 months ago • 2 comments

Summary

clean_json_response(response) in src/memos/mem_os/utils/format_utils.py:1393 calls response.replace(...) unconditionally. When response is None it dies with:

AttributeError: 'NoneType' object has no attribute 'replace'

The traceback points to format_utils.py:1403 and gives no hint about the real problem (an upstream LLM call returning None).

How None reaches here

In the codebase today this is reachable via the suggestion endpoint:

POST /product/suggestions
→ suggestion_handler.handle_get_suggestion_queries
→ llm.generate(message_list)            # OpenAILLM.generate, decorated with @timed_with_status
→ (LLM call raises BadRequestError)
→ timed_with_status catches, no fallback configured, falls through to implicit `return None`
→ clean_json_response(None)
→ AttributeError ← user sees this

The deeper bug is in timed_with_status (filed separately as #1523). Even after that is fixed, however, defending against None here is cheap and turns the worst possible diagnostic experience (a wrong-line AttributeError) into a clear message that names the actual root cause.

Proposed fix

if response is None:
    raise ValueError(
        "clean_json_response received None — upstream LLM call likely "
        "failed silently (check timed_with_status / generate() error handling)."
    )
return response.replace("```json", "").replace("```", "").strip()

PR follows.

Ptah-CT avatar Apr 22 '26 23:04 Ptah-CT

🤖 AutoDev has picked up this issue and started working on it.

Task ID: db3666091a1dfa82 Working branch: bugfix/autodev-1525 Target branch: dev-20260604-v2.0.19 Workflow: opsp (analysis → coding → testing → PR)

I will post the PR link here once done. If I need more information, I will ask in the comments.

Memtensor-AI avatar Jun 04 '26 11:06 Memtensor-AI

✅ AutoDev task db3666091a1dfa82 completed.

Summary: ## Fix Summary

Successfully addressed issue #1525 where clean_json_response() crashed with a cryptic AttributeError: 'NoneType' object has no attribute 'replace' when receiving None input from failed LLM calls.

Implementation Details

The fix was already present in the codebase at src/memos/mem_os/utils/format_utils.py:1410-1416. The implementation adds a defensive None check at function entry that raises a clear ValueError with a diagnostic message pointing to the root cause (upstream LLM call failures in timed_with_status decorator or generate() method).

Key Changes:

  • Added None check before calling .replace() method
  • Raises ValueError with message: "clean_json_response received None — upstream LLM call likely failed silently (check timed_with_status / generate() error handling)."
  • Updated docstring with Raises section documenting the ValueError
  • Preserves original behavior for all valid string inputs

Test Coverage

Added comprehensive test suite in tests/mem_os/test_format_utils.py with 8 test cases covering:

  1. None input validation - Verifies ValueError is raised with diagnostic message
  2. Markdown removal - Tests json and marker removal
  3. Whitespace handling - Verifies leading/trailing whitespace is stripped
  4. Plain JSON passthrough - Ensures valid JSON is unchanged
  5. Empty string handling - Edge case coverage
  6. Complex nested JSON - Realistic LLM response scenarios
  7. Internal backticks preservation - Ensures backticks within JSON content are preserved

Verification

The fix has been verified through:

  • Code review: Implementation correctly intercepts None before AttributeError occurs
  • Call chain analysis: Confirmed the function is called from suggestion_handler.py where LLM calls can return None
  • Logic verification: Before/after behavior confirmed - cryptic AttributeError replaced with clear ValueError pointing to root cause
  • Impact assessment: Zero regression risk - purely defensive change that only affects previously crashing code path

Commit Details

  • Branch: bugfix/autodev-1525
  • Commit SHA: 7291222c
  • Commit Message: "test: add comprehensive tests for clean_json_response (issue #1525)"

The fix improves debuggability by transforming an unhelpful error message into a clear diagnostic that immediately points developers to the upstream LLM call failure.

Base branch: dev-20260604-v2.0.19 Branch: bugfix/autodev-1525 Commit: 7291222 PR: https://github.com/MemTensor/MemOS/pull/1884 Assigned to: @MatthewZhuang, @CarltonXiang, @syzsunshine219 Reviewers: @MatthewZhuang, @CarltonXiang, @syzsunshine219

Memtensor-AI avatar Jun 04 '26 11:06 Memtensor-AI