OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug] Memory extraction silently yields 0 memories when the VLM answers in prose or leaks tool calls as DSML markup — with patch

Open tizerluo opened this issue 3 months ago • 5 comments

Environment

  • openviking-server v0.4.17.1 (Docker ghcr.io/volcengine/openviking), Python 3.13
  • auth_mode: trusted, local AGFS + local vectordb
  • VLM backends exercised: moonshot/kimi-k2.6 (thinking model), deepseek/deepseek-v4-flash(-vision-exp)

Symptom

Session commits report success while memory extraction silently produces nothing. Three distinct failure modes, all in session/memory/extract_loop.py:

  1. DSML tool-call leak (DeepSeek V4 family). When the model decides to call a tool, the serving stack sometimes leaves the native <|DSML|tool_calls><|DSML|invoke name="...">... markup in content instead of structured tool_calls (same family as vllm-project/vllm#48931). _call_llm finds no tool calls, JSON parsing of the content fails, the iteration is wasted; after retries the run ends with 0 memories.
  2. Prose-answer trap (thinking models). A thinking model (e.g. kimi-k2.6) may answer an iteration with free-form reasoning such as "I need to check existing memories first, let me search..." — no tool call, no JSON. The failure branch then sets _disable_tools_for_iteration = True, so the next iteration runs with tools disabled — exactly the opposite of what the model asked for — and extraction degrades to 0 memories.
  3. Silence. All of the above surface only in container logs / per-session .failed.json; the queue/metrics surface reports success (see also #4486).

Root cause

  • _call_llm only accepts structured tool_calls or JSON-parsable content; DSML markup in content is unhandled.
  • The failure branch disables tools unconditionally after a prose answer instead of letting the model continue with tools.

Patch

Two small, additive patches against v0.4.17.1 — full diff: https://gist.github.com/tizerluo/f26126f1cd666dcc7598fc6bb3631455 (we run it as a bind-mounted extract_loop.py):

  1. _parse_dsml_tool_calls(content) — detects <|DSML|invoke name="..."> blocks in content (tolerant to the <|DSML| vs <||DSML|| variants), converts them into ToolCall objects (string="false" params are JSON-decoded), and _call_llm returns them exactly like native tool calls.
  2. Prose continuation — on parse_error with non-empty prose content and tools still enabled, append the model's answer as an assistant message plus a short "continue with tools or return the final JSON" nudge, keeping tools enabled for one extra iteration instead of disabling them.

Test evidence

  • DSML parser unit-tested against real leaked payloads captured from production logs (single/double full-width-bar variants, string vs JSON params, multi-invoke, plain-text negative case).
  • Live on our instance: with kimi-k2.6, extraction of a 3-section memory write completed and landed (events + entity cards rebuilt); with deepseek-v4-flash-vision-exp, extraction completed cleanly. Before the patch, both models intermittently produced parse_error / Extracted 0 memories.

Happy to turn this into a PR if you prefer.

tizerluo avatar Sep 02 '26 01:09 tizerluo

Nice write-up — I walked the same paths in openviking/session/memory/extract_loop.py (main) and can confirm both failure modes, plus one interaction that makes them worse together than separately:

  1. DSML leak burns the format-error retry budget. _call_llm only accepts structured tool_calls (response.has_tool_calls, ~line 1125) or JSON-parsable content (parse_json_with_stability, ~line 1142). Leaked <|DSML|invoke ...> markup falls through to Case 3 → failure_kind="parse_error". Back in run(), that consumes the single format-error retry (_format_retry_count == 0, ~line 384) — so the one "real" retry is spent on a garbage response, leaving zero budget for a genuine formatting slip later.

  2. Prose answer → unconditional tool-disable (~line 421) fights the model's own plan. After any unparseable iteration the loop sets _disable_tools_for_iteration = True, so the next _call_llm passes tools=None (~line 1076) even when the prose explicitly said it wanted to search first ("let me check existing memories"). The model is then forced to emit final JSON with no tool results — hence 0 memories. The flag was clearly designed for the unknown-tool case (~lines 298-301); reusing it as a catch-all format-error handler is what creates the prose trap. Your "keep tools enabled one extra iteration" fix is the right minimal correction; a bound (disable only after N consecutive failures) would keep it safe against genuinely stuck loops.

  3. The observability gap is real — and there's already a hook for it. On the final failure the loop DOES record an error: ResolvedOperations(errors=[...]) (~lines 410-418). But nothing in the commit path appears to promote errors to queue/metrics, so "commit success + 0 memories" is only visible in logs / .failed.json (#4486). Promoting a non-empty errors list to a first-class signal (e.g. a memory_extract.failed counter + per-session status) would make this whole class of bug self-alerting instead of silent.

Both patch items look like the right direction; happy to sanity-check the PR when it lands.

pm25coder avatar Sep 02 '26 04:09 pm25coder

Opened a fix PR: https://github.com/volcengine/OpenViking/pull/4607

ktz03 avatar Sep 02 '26 14:09 ktz03

Thank you for the detailed report, investigation, and proposed patch.

After reviewing the two cases, we believe both originate outside the OpenViking protocol boundary:

  1. Raw DSML in content is a leak of DeepSeek V4's model-native tool-call serialization. An OpenAI-compatible serving backend is expected to parse that representation and return standard structured tool_calls. Missing DSML wrapper tokens, parser configuration mismatches, and parser edge cases should therefore be handled in the model/serving layer (for example, the corresponding DeepSeek/vLLM parser), rather than by adding a vendor-specific DSML parser to OpenViking.

  2. A Kimi thinking model returning free-form planning prose instead of either a structured tool call or the requested JSON is a model-side format-adherence issue. OpenViking's extraction contract expects the backend to return standard tool_calls or valid JSON matching the requested schema. We do not recommend adding model-specific continuation behavior for non-standard prose responses.

Adding these fallbacks in OpenViking would duplicate vendor protocol parsing, broaden the compatibility surface, and risk interpreting ordinary model content as executable tool calls. For that reason, we do not plan to handle either behavior in OpenViking and recommend fixing or configuring the corresponding model-serving layer instead.

Thanks again for documenting the cases so clearly and for offering the patch.

yeshion23333 avatar Sep 03 '26 09:09 yeshion23333

Appreciate the thorough review — and the boundary argument is fair. Agreed that OpenViking should not vendor a DSML parser: leaked DSML is DeepSeek's serialization, and the correct home for that is the serving/parser layer (the vLLM parser issue I tracked while investigating, vllm-project/vllm#48931, is exactly that class of work), and treating thinking-model prose as a format violation is likewise a model-side contract question. Neither is an argument for OpenViking to grow vendor-specific parsers.

The one finding I'd keep separate from the boundary discussion is the observability gap, because it does not depend on where the root cause lives:

  • Whatever the trigger — a serving-layer misparse, an adapter mismatch, a timeout, a genuinely novel failure — the loop ends the same way today: it records ResolvedOperations(errors=[...]) and that record does not propagate. The commit path reports success with zero memories, and the failure is only visible in logs or .failed.json files someone has to go looking for.
  • The user-facing symptom is therefore not "bad output from a model" but "extraction reported done, produced nothing, and no signal fired." That symptom will keep occurring while upstream layers are being fixed, and it will also catch the next boundary-unknown failure that nobody has classified yet.
  • Promoting a non-empty errors list to a first-class signal is not vendor-specific and does not broaden the parse surface: it is just making the result type honest about what already happened. A memory_extract.failed counter plus per-session status, or treating any non-empty errors as extract-failure rather than success, would turn this entire class of incident self-alerting.

Happy to leave it there — the boundary call on where fixes belong is the maintainers', and this is only about the extraction loop not reporting a failed run as a success.

pm25coder avatar Sep 03 '26 11:09 pm25coder

Adding an operations-side data point for anyone who lands here with the same silent-loss class.

Environment: self-hosted OpenViking v0.4.19 (docker), VLM behind an OpenAI-compatible gateway (OpenRouter), model deepseek/deepseek-v4-flash-0731 with extra_request_body.reasoning.enabled=false.

Observed after a routine VLM swap: ~2 of 4 substantial sessions committed with status=completed while result.memories_extracted={}; the matching history/archive_NNN/memory_diff.json had adds/updates/deletes all 0 and trace_id=null. The only evidence lived in the container log:

extract_loop:run:420 - Failed to parse memory operations (iteration 4/4)
failure_kind=parse_error error=Line 1: only assignments and SDK/object calls are allowed

with the model's content hallucinating a different memory framework (from memobase import Memobase / mb.create(...)) or calling a method that does not exist for that type (sdk.set_events() where the protocol only accepts create_events()). So: non-empty content, wrong protocol — not empty_response.

Two workarounds that made the class survivable without touching OpenViking (neither asks for a vendor-specific parser, consistent with the boundary argument above):

  1. Chunked re-push. A 133-message archived session re-pushed as a single commit (~196k prompt tokens) failed twice: once 4/4 parse_error, once wedging the task in running with a frozen updated_at and total silence in both container logs and the provider's request log. The same messages re-pushed as ~45-message chunks (one commit per chunk) extracted normally (0 → 14 memories). In our case batch size was the dominant variable, not the model's capability.
  2. External watchdog. Since a failed extraction still reports completed, we poll /api/v1/tasks and treat any session_commit whose memories_extracted sums to 0 (with a sufficiently large archive) as a lost commit, then re-push it. Status alone cannot distinguish "extracted nothing" from "extracted nothing yet", and a commit that is still running looks identical.

Both exist purely because of the gap raised in this thread: promoting a non-empty errors list (or any non-zero failure record) to a first-class signal — a memory_extract.failed counter, or simply not reporting success when errors is non-empty — would let this whole class self-alert instead of requiring out-of-band polling. That part is independent of where the parser fix lands.

Happy to share the exact repro (session sizes, prompt-token counts, task JSON) if useful to whoever owns the extraction loop.

wjmyjay5-beep avatar Sep 11 '26 19:09 wjmyjay5-beep