Skip to content

Conversation

@victorm-lc
Copy link
Contributor

🐛 Fix: Memory Loading Issues in LangGraph Studio + Weather API Enhancement

Problem

When running agents with long-term memory in LangGraph Studio (langgraph dev), memory was not loading properly for users, and hot-reloading caused ModuleNotFoundError crashes.

Root Causes Identified

1. Namespace Type Mismatch

The load_memory() and create_memory() functions were using inconsistent types for the namespace:

  • load_memory() used state["customer_id"] (integer)
  • create_memory() used str(state["customer_id"]) (string)

This meant memory was stored at namespace ("memory_profile", "1") but loaded from ("memory_profile", 1) - two different namespaces!

2. Pydantic Model Serialization Issue

The create_memory() function was storing Pydantic model instances directly:

store.put(namespace, key, {"memory": updated_memory})  # ❌ Stores object

When LangGraph's disk-backed store pickles this, it saves class references (e.g., __agents__memory_enabled_music_store_supervisor_with_interrupt.UserProfile). During hot-reload, Python can't find these module references → crash.

3. Memory Format Handling

The format_user_memory() function only handled Pydantic models with attributes, but after retrieval from the store, data could be either a dict or a Pydantic model.

Changes Made

agents/memory_enabled_music_store_supervisor_with_interrupt.py

  1. Fixed namespace consistency (line 142):

    user_id = str(state["customer_id"])  # Convert to string consistently
  2. Fixed memory serialization (line 205):

    store.put(namespace, key, {"memory": updated_memory.model_dump()})  # Convert to dict
  3. Fixed memory format handling (lines 128-132):

    # Handle both Pydantic model (attributes) and dict (keys) representations
    if isinstance(profile, dict):
        music_prefs = profile.get('music_preferences', [])
    else:
        music_prefs = getattr(profile, 'music_preferences', [])

agents/lg101-weather-agent.py

  • Enhanced with real weather API integration (OpenWeatherMap or similar)

Testing

  • ✅ Memory now stores and loads correctly across sessions
  • ✅ Hot-reload works without ModuleNotFoundError
  • ✅ Memory preferences persist properly for users
  • ✅ Works in both notebook environment and LangGraph Studio

Impact

  • Users' music preferences are now properly loaded and used for personalized recommendations
  • Development workflow is smoother with working hot-reload
  • No need to manually clear .langgraph_api folder after code changes

Related Issues

Fixes memory loading in LangGraph Studio for agents using long-term memory store.

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.

2 participants