fix(user_memory): prevent LLM no-op explanation from overwriting user profile#1
Open
lightzt99 wants to merge 1 commit into
Open
fix(user_memory): prevent LLM no-op explanation from overwriting user profile#1lightzt99 wants to merge 1 commit into
lightzt99 wants to merge 1 commit into
Conversation
… 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
force-pushed
the
fix/issue-933-prevent-noop-profile-overwrite
branch
from
June 1, 2026 08:09
6f6f7ea to
6cc8904
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
{"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._call_llm_for_extractiontollm_json_text_with_fallbackso models that supportresponse_format={"type":"json_object"}are constrained at the API level; unsupported models degrade automatically.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
src/powermem/prompts/user_profile_prompts.py[Target]section to require{"changed": bool, "profile": str}; clarify JSON keys are always Englishsrc/powermem/user_memory/user_memory.py_call_llm_for_extraction: usellm_json_text_with_fallback;_extract_profile: replace single-path logic with three-layer parse fallback; drop_NOOP_PROFILE_REtests/unit/test_extract_profile.pyNote:
_extract_topicsuses 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 passedpytest tests/unit/— 186 existing tests passed, no regressionresponse_format(e.g. local Ollama), confirm Layer 3 plain-text fallback activates and logsINFOSecurity / privacy
profilefield 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