fix: Anthropic compatibility for memory injection middleware#50
Open
tamashi095 wants to merge 1 commit intoMystenLabs:devfrom
Open
fix: Anthropic compatibility for memory injection middleware#50tamashi095 wants to merge 1 commit intoMystenLabs:devfrom
tamashi095 wants to merge 1 commit intoMystenLabs:devfrom
Conversation
The `injectMemoryContext` helper inserted memories as a separate system message. Anthropic's API rejects multiple system messages separated by user/assistant messages, causing `AI_UnsupportedFunctionalityError`. Fix: append memory context to the existing system message instead of inserting a new one. When no system message exists, prepend as before. Also adds: - `prepublishOnly` script to prevent publishing without building - Unit tests for middleware helpers (bun:test) - tsconfig exclusion for test files
Collaborator
|
Hi @tamashi095 Thanks for the fix! Before approving, could you share a screenshot or recording of the e2e test passing with Could you include:
This helps verify the root cause and confirms the fix resolves it. Thanks! |
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
injectMemoryContextinserted memories as a separate{ role: "system" }message. When the consumer already passes a system prompt tostreamText, this creates multiple system messages separated by user/assistant messages, which Anthropic rejects withAI_UnsupportedFunctionalityError. Fix: append memory context to the existing system message instead of inserting a new one.prepublishOnlyscript to ensuretscruns beforenpm publish, preventing packages from being published withoutdist/.findLastUserMessage,injectMemoryContext, andformatMemoriesusingbun:test.Context
Discovered while integrating MemWal with Sona's Miso chat assistant, which uses
@ai-sdk/anthropic+streamTextwith a system prompt. The middleware worked with OpenAI (which tolerates multiple system messages) but broke on Anthropic.Test plan
bun test packages/sdk/src/ai/middleware.test.ts— 13 tests passbun run buildinpackages/sdk/— clean compile@ai-sdk/anthropic+streamText+ system prompt