[LangGraph] unique index on (namespace, key) causes duplicate-key collisions

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Unknown
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Python Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      The store creates a compound unique index on (namespace, key), but namespace is stored as an array. In MongoDB this makes it a multikey index - each array element gets its own index entry - so two documents whose namespaces share any path element and have the same key collide.

      store.put(("users", "alice", "preferences"), "food", {...})
      store.put(("users", "bob",   "preferences"), "food", {...})
      
      pymongo.errors.BulkWriteError: E11000 duplicate key error
        collection: assistant_demo.memories
        index:      namespace_1_key_1
        dup key:    { namespace: "preferences", key: "food" }
      

      Note namespace: "preferences" - a single string, not the array. That's the multikey entry. Alice's doc already claimed ("preferences", "food") and ("users", "food"), so Bob collides.

      Source
      https://github.com/langchain-ai/langchain-mongodb/blob/main/libs/langgraph-store-mongodb/langgraph/store/mongodb/base.py#L181-L185

      Solution could be to put index on denormalized namespace string (e.g. "users/bob/preferences") instead.

            Assignee:
            Casey Clements
            Reporter:
            Sergey Zelenov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: