Skip to content

fix(openai-sdk): don't inject empty system prompt when no memories are found#999

Open
devteamaegis wants to merge 1 commit into
supermemoryai:mainfrom
devteamaegis:fix/empty-memories-system-prompt-injection
Open

fix(openai-sdk): don't inject empty system prompt when no memories are found#999
devteamaegis wants to merge 1 commit into
supermemoryai:mainfrom
devteamaegis:fix/empty-memories-system-prompt-injection

Conversation

@devteamaegis
Copy link
Copy Markdown

Bug

In packages/openai-sdk-python/src/supermemory_openai/middleware.py, add_system_prompt() builds a memories string and then unconditionally modifies the message list — even when memories is empty (i.e. the profile and search results both returned nothing).

What happens today when no memories are found:

  1. Existing system prompt present — the content becomes "You are helpful. \n " (trailing whitespace appended).
  2. No system prompt present — a new {"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:

# memories resolves to "" when profile/search return nothing
messages = [{"role": "user", "content": "Hello"}]
result = await add_system_prompt(messages, ...)
# Before fix: [{"role": "system", "content": ""}, {"role": "user", "content": "Hello"}]
# After fix:  [{"role": "user", "content": "Hello"}]   (unchanged)

Fix

Add an early return before the modification block:

if not memories:
    return messages

One line. No behaviour change when memories are present.

Tests

Added test_empty_memories_do_not_modify_messages to tests/test_middleware.py covering both cases (messages with and without a pre-existing system prompt).

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).
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.

1 participant