fix(openai-sdk): don't inject empty system prompt when no memories are found#999
Open
devteamaegis wants to merge 1 commit into
Open
Conversation
add_system_prompt() modified the message list even when the memory
lookup returned nothing (memories == "").
- If an existing system prompt was present, " \n " (whitespace) was
appended to its content.
- If no system prompt existed, a new {"role": "system", "content": ""}
message was prepended.
Both cases pollute the conversation context with empty/whitespace tokens
that waste prompt budget and can confuse models sensitive to blank
system messages.
Fix: return messages unchanged when memories is falsy.
Adds a regression test covering both cases (with and without existing
system prompt).
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.
Bug
In
packages/openai-sdk-python/src/supermemory_openai/middleware.py,add_system_prompt()builds amemoriesstring and then unconditionally modifies the message list — even whenmemoriesis empty (i.e. the profile and search results both returned nothing).What happens today when no memories are found:
"You are helpful. \n "(trailing whitespace appended).{"role": "system", "content": ""}message is prepended to the list.Both outcomes corrupt the conversation: the first wastes tokens with whitespace junk; the second injects a blank system turn that some models treat as an explicit instruction.
Minimal reproduction:
Fix
Add an early return before the modification block:
One line. No behaviour change when memories are present.
Tests
Added
test_empty_memories_do_not_modify_messagestotests/test_middleware.pycovering both cases (messages with and without a pre-existing system prompt).