Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(memory): add Claude-compatible mode to ZepMemory #7808

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

raffareis
Copy link

Fix System Messages with Anthropic's Claude in Zep Memory

Problem

When using Zep Memory with Anthropic's Claude models, the integration fails because Claude only permits system messages as the first message. This causes errors when the memory retrieval mechanism in ZepMemory returns a SystemMessage containing summary content followed by other message types.

Solution

This PR adds utility functions and a configuration option to handle Claude's limitation with system messages, similar to the solution already implemented in ZepCloudMemory:

  1. Added a new separateMessages option that controls how memory is returned:

    • When true (default): Returns separate messages with proper types (SystemMessage, HumanMessage, AIMessage) - maintains backward compatibility
    • When false: Condenses all memory content into a single HumanMessage for Claude compatibility
  2. Added utility functions:

    • zepMemoryContextToSystemPrompt: Extracts summary and facts as a system prompt
    • condenseZepMemoryIntoHumanMessage: Combines all memory into a formatted HumanMessage
    • zepMemoryToMessages: Preserves memory structure with proper message types

Implementation Details

  • The default for separateMessages is true to maintain backward compatibility with existing code
  • Added type safety improvements to avoid "implicitly any" type errors
  • Improved handling of role/roleType values with actualRole = roleType || role
  • Added comprehensive JSDoc documentation for the new functions and options
  • The implementation is almost identical to the one in ZepCloudMemory but specifically for the open-source Zep integration

Testing Recommendations

  • Test with both separateMessages: true and separateMessages: false to ensure both modes work as expected
  • Specifically test with Claude models to verify the fix resolves the system message limitation
  • Verify backwards compatibility by testing with existing code that expects the original behavior

Related Issues

Fixes #7807 - System messages don't work with Anthropic's Claude

Add separateMessages option to ZepMemory class to address Claude API
limitation with system messages. When set to false, all memory content
is condensed into a single HumanMessage instead of using SystemMessage.
Default is true for backward compatibility.
Fixes langchain-ai#7807
Copy link

vercel bot commented Mar 6, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Mar 6, 2025 6:41pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Mar 6, 2025 6:41pm

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. auto:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Mar 6, 2025
@jacoblee93
Copy link
Collaborator

Looks fine to me, but CC @danielchalef for a final review!

@jacoblee93 jacoblee93 added lgtm PRs that are ready to be merged as-is hold On hold labels Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature hold On hold lgtm PRs that are ready to be merged as-is size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

System messages don't work with Anthropic's Claude.
2 participants