Skip to content

fix(user_memory): prevent LLM no-op explanation from overwriting user profile#1

Open
lightzt99 wants to merge 1 commit into
mainfrom
fix/issue-933-prevent-noop-profile-overwrite
Open

fix(user_memory): prevent LLM no-op explanation from overwriting user profile#1
lightzt99 wants to merge 1 commit into
mainfrom
fix/issue-933-prevent-noop-profile-overwrite

Conversation

@lightzt99

Copy link
Copy Markdown
Owner

Summary

  • Replace the plain-text extraction contract with a structured JSON response {"changed": bool, "profile": str}, giving the LLM an unambiguous way to express "nothing to extract" without generating natural-language explanations that overwrite the stored profile.
  • Wire _call_llm_for_extraction to llm_json_text_with_fallback so models that support response_format={"type":"json_object"} are constrained at the API level; unsupported models degrade automatically.
  • Add a three-layer parse fallback so models that ignore structured-output instructions still work: JSON parse → exact-match no-op list → plain-text passthrough.

Motivation

When a conversation contains no extractable profile information, the LLM sometimes returns a verbose refusal (e.g. "整段对话未包含任何可提取的用户信息,保持不变") instead of an empty string. This bypassed the existing exact-match filter and silently overwrote the user's accumulated profile with the refusal text.

A regex guard was considered but rejected: patterns broad enough to catch all no-op phrasing (e.g. 没有.*信息) also match valid profile content such as "用户目前没有任何工作信息,正在求职", producing false positives with no reliable fix. Structured output resolves the ambiguity at the source.

Changes

Area What changed
src/powermem/prompts/user_profile_prompts.py Rewrite instructions 6–9 and [Target] section to require {"changed": bool, "profile": str}; clarify JSON keys are always English
src/powermem/user_memory/user_memory.py _call_llm_for_extraction: use llm_json_text_with_fallback; _extract_profile: replace single-path logic with three-layer parse fallback; drop _NOOP_PROFILE_RE
tests/unit/test_extract_profile.py 14 new unit tests covering all three layers, edge cases, and the core regression scenario

Note: _extract_topics uses a separate JSON contract and is not affected by this change. The inconsistency is known and tracked separately.

Test plan

  • pytest tests/unit/test_extract_profile.py — 14 passed
  • pytest tests/unit/ — 186 existing tests passed, no regression
  • Send a conversation with no user information (e.g. "今天天气好"), confirm existing profile is unchanged in storage
  • Send a conversation with valid user information, confirm profile is correctly updated
  • Configure a model that does not support response_format (e.g. local Ollama), confirm Layer 3 plain-text fallback activates and logs INFO

Security / privacy

  • All three fallback layers log fixed messages only — raw LLM responses are never written to logs, preventing profile content from leaking into log files.
  • profile field values are written to storage only; they are not executed as code or commands. The indirect prompt-injection vector (profile content later included in LLM context) is pre-existing and out of scope for this PR.

Fixes oceanbase#933

… profile

When a conversation contains no extractable profile information, the LLM
sometimes returns a verbose refusal instead of an empty string, bypassing
the existing exact-match filter and silently overwriting the stored profile.

- Change extraction contract to structured JSON {"changed": bool, "profile": str}
  so the LLM has an unambiguous way to express "nothing to extract"
- Wire _call_llm_for_extraction to llm_json_text_with_fallback to enforce
  response_format=json_object at the API level for capable models
- Add three-layer parse fallback: JSON parse → exact-match list → plain-text
  passthrough for models that don't support structured output
- Drop _NOOP_PROFILE_RE: regex broad enough to catch all no-op phrasing also
  matches valid profile content, making false positives unavoidable
- Add 14 unit tests covering all layers, edge cases, and the core regression

Fixes oceanbase#933
@lightzt99
lightzt99 force-pushed the fix/issue-933-prevent-noop-profile-overwrite branch from 6f6f7ea to 6cc8904 Compare June 1, 2026 08:09
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.

[Bug]:Profile Memory loss, covered by the analysis results of Profile generated LLM | 用户画像记忆丢失,错误覆盖

1 participant