Skip to content

fix(tools): support multi-part and array message content in OpenAI middleware - #1638

Open
Adityakk9031 wants to merge 1 commit into
supermemoryai:mainfrom
Adityakk9031:fix/openai-middleware-array-messages
Open

fix(tools): support multi-part and array message content in OpenAI middleware#1638
Adityakk9031 wants to merge 1 commit into
supermemoryai:mainfrom
Adityakk9031:fix/openai-middleware-array-messages

Conversation

@Adityakk9031

Copy link
Copy Markdown
Contributor

Problem

In packages/tools/src/openai/middleware.ts, getLastUserMessage and getConversationContent previously assumed message content was strictly a string.

When callers use modern OpenAI multimodal or multi-part messages where content is an array of content parts ([{ type: "text", text: "..." }, ...]), typeof content === "string" evaluated to false, causing getLastUserMessage to return "".

In createWithMemory, !userMessage evaluated to true, which caused the middleware to:

  1. Silently skip semantic memory retrieval.
  2. Skip saving the conversation context to memory.
  3. Pass through the completion request without injecting any Supermemory context.

Solution

  1. Added extractMessageText in openai/middleware.ts to cleanly extract and join text parts from both string content and array content parts (ChatCompletionContentPartText).
  2. Updated getLastUserMessage and getConversationContent to use extractMessageText.
  3. Added unit tests under packages/tools/src/openai-middleware.test.ts covering string, multi-part, and multimodal inputs.

Verification

Ran bun test packages/tools/src/openai-middleware.test.ts (all 6 tests passed).

@Adityakk9031

Copy link
Copy Markdown
Contributor Author

Hey @Dhravya @MaheshtheDev, could you please take a look at this fix when you have a chance? Thank you!

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