langchainjs icon indicating copy to clipboard operation
langchainjs copied to clipboard

fix(community): add Claude-compatible mode to ZepMemory

Open raffareis opened this issue 9 months ago • 3 comments

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

raffareis avatar Mar 06 '25 18:03 raffareis

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 13, 2025 4:40pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Mar 13, 2025 4:40pm

vercel[bot] avatar Mar 06 '25 18:03 vercel[bot]

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

jacoblee93 avatar Mar 11 '25 00:03 jacoblee93

Bump @danielchalef!

jacoblee93 avatar Mar 22 '25 18:03 jacoblee93

@jacoblee93 Hi, I'm an engineer at Zep, @danielchalef asked me to take a look at this one, looks good to me, @raffareis Appreciate the contribution!

paul-paliychuk avatar May 05 '25 23:05 paul-paliychuk