Skip to content

fix(session): handle non-dict LLM responses in memory extraction#618

Merged
MaojiaSheng merged 2 commits intovolcengine:mainfrom
mvanhorn:osc/605-memory-extraction-non-dict-response
Mar 15, 2026
Merged

fix(session): handle non-dict LLM responses in memory extraction#618
MaojiaSheng merged 2 commits intovolcengine:mainfrom
mvanhorn:osc/605-memory-extraction-non-dict-response

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

Summary

Add defensive type checking in memory extraction to handle LLM responses that return a list instead of a dict. This fixes crashes when using local Ollama models.

Why this matters

When using OpenViking with local Ollama models (e.g., llama3.1:latest), memory extraction fails because parse_json_from_response() can return a list [{...}] instead of the expected {"memories": [{...}]}. The subsequent data.get("memories", []) call throws AttributeError since lists don't have .get().

  • #605 - Full repro with Ollama config (bge-m3 + llama3.1)
  • The existing codebase already uses isinstance(data, dict) checks in the merge bundle path (line 559), but the extraction path at line 296-301 lacks this guard

Changes

In openviking/session/memory_extractor.py, after parse_json_from_response():

  • If data is a list, wrap it as {"memories": data} (treat the list as the memories array directly)
  • If data is neither dict nor list, log a warning and fall back to empty dict
  • Existing dict-format responses pass through unchanged

Testing

Added tests/session/test_memory_extractor_response_types.py with 6 test cases:

  • Dict response passthrough (existing behavior preserved)
  • List response wrapping
  • String/None/int fallback to empty dict
  • Empty list handling

Fixes #605

This contribution was developed with AI assistance (Claude Code).

When using local Ollama models, parse_json_from_response may return a
list instead of the expected {"memories": [...]} dict, causing
AttributeError on .get(). Add type checking after parsing: wrap lists
as {"memories": data}, and fall back to {} for other unexpected types.

Closes volcengine#605

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@MaojiaSheng MaojiaSheng merged commit d204bd0 into volcengine:main Mar 15, 2026
6 checks passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenViking project Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Memory extraction is unreliable with local Ollama models (non-dict parser output / empty extract results)

2 participants