Skip to main content

Add your description here

Project description

langmem-adapter

langmem-adapter is a lightweight, generic adapter for LangMem tools. It seamlessly integrates LangMem tools into your openai agents sdk agents by supporting both static (pre‑injected store) and dynamic store injection.

With dynamic mode, you can have per-invocation store creation (e.g. for database connections) and dynamically formatted namespaces based on your context.

Features

  • Dynamic & Static Modes:
    • Static Mode: Use an already‑constructed tool instance with a fixed (static) namespace.
    • Dynamic Mode: Provide a store provider (async context manager) and a factory callable so that each call gets a fresh store from a connection pool.
  • Context-Based Namespace Formatting:
    Format namespaces dynamically using a tuple of template strings. For example:
    ("email_assistant", "{langgraph_user_id}", "collection")
    The placeholders are filled from a Pydantic context.
  • Type Safety:
    Uses Pydantic for argument validation and ensures UUIDs and JSON fields are correctly converted.
  • Generic Context Support:
    The adapter is generic over a context type (bounded to Pydantic’s BaseModel), ensuring your context data is correctly typed.

Installation

Install via pip (assuming you publish it on PyPI):

pip install langmem-adapter

Or install from source:

git clone https://github.com/mjunaidca/langmem-adapter.git
cd langmem-adapter
pip install .

Usage

Dynamic Mode Example

In dynamic mode, you provide:

  • A factory callable that takes a store and a namespace and returns a new LangMem tool instance.
  • A store_provider that returns an async context manager yielding a fresh store.
  • A namespace template as a tuple of strings with placeholders to be filled from the context.
from langmem import create_manage_memory_tool, create_search_memory_tool
from langmem_adapter import LangMemToolAdapter  # import from your package
from your_store_module import get_store  # your async context manager
from pydantic import BaseModel

# Define your context model.
class UserContext(BaseModel):
    langgraph_user_id: str
    # add other fields as needed

# Initialize the adapter for the manage memory tool.
manage_adapter = LangMemToolAdapter(
    lambda store, namespace=None: create_manage_memory_tool(namespace=namespace, store=store),
    store_provider=get_store,
    namespace_template=("email_assistant", "{langgraph_user_id}", "collection")
)
manage_memory_tool = manage_adapter.as_tool()

# Similarly, for the search memory tool.
search_adapter = LangMemToolAdapter(
    lambda store, namespace=None: create_search_memory_tool(namespace=namespace, store=store),
    store_provider=get_store,
    namespace_template=("email_assistant", "{langgraph_user_id}", "search")
)
search_memory_tool = search_adapter.as_tool()

# In your agent or wherever you need to use the tool, the dynamic namespace will be
# formatted using the context (an instance of UserContext) passed in through the RunContextWrapper.

Static Mode Example

If you have a persistent store (with connection pooling) and a fixed namespace, you can use static mode:

from langmem import create_manage_memory_tool
from langmem_adapter import LangMemToolAdapter

# Pre-create your store (with pooling) and tool.
store = ...  # your pre-initialized store instance
manage_memory_tool_instance = create_manage_memory_tool(namespace=("email_assistant", "collection"), store=store)

adapter = LangMemToolAdapter(manage_memory_tool_instance)
manage_memory_tool = adapter.as_tool()

# This tool will use the static namespace you provided.

API Reference

Class: LangMemToolAdapter[TContext]

A generic adapter that binds a LangMem tool with a dynamic store provider and dynamic namespace formatting.

Constructor Parameters:

  • langmem_tool: Any
    • In static mode, an already‑constructed tool instance.
    • In dynamic mode, a factory callable that accepts a store (and optionally a namespace) and returns a tool instance.
  • store_provider: Optional[Callable[[], Any]]
    An async context manager that yields a store instance. If provided, dynamic mode is enabled.
  • namespace_template: Optional[tuple[str, ...]]
    A tuple of template strings used to dynamically construct the namespace. Placeholders in these strings (e.g. "{langgraph_user_id}") are replaced with values from the context (which must be a Pydantic model).

Method: as_tool() -> FunctionTool
Returns a FunctionTool instance that can be used by your agent.

Internal Method: _on_invoke_tool(ctx: RunContextWrapper[TContext], args: str) -> str
Parses and validates the input arguments, converts types as needed, dynamically creates a new tool instance if in dynamic mode, and invokes the tool’s callable.

Contributing

Contributions are welcome! Please submit issues or pull requests via GitHub.

License

MIT License


This package abstracts away the complexities of dynamic store injection, context-based namespace formatting, and type-safe argument conversion. It’s small, reusable, and perfect for integrating LangMem tools into various projects.

Happy coding!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langmem_adapter-0.0.1.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langmem_adapter-0.0.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file langmem_adapter-0.0.1.tar.gz.

File metadata

  • Download URL: langmem_adapter-0.0.1.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for langmem_adapter-0.0.1.tar.gz
Algorithm Hash digest
SHA256 50b931ff7bd0c0dfeb541b6e7fa2e91d87c9e3b73b9b43bb135cc46b22d12897
MD5 b2cc8f5ff874114f564d8a0c4ae994fb
BLAKE2b-256 178887b6a3da2926557746fb214a6a92aa0e7b400baff577de8070892ce55055

See more details on using hashes here.

File details

Details for the file langmem_adapter-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langmem_adapter-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 18bdacb7b0190099a4a118ed3c797d0513f99d948de5b8e971f764354736ac07
MD5 25c26e06e905ed7cb3cd2cf1678e81bf
BLAKE2b-256 8bba6628d5b8b9ccb243562e546e48a5c2e99bb9854cbb493afc00c991f7bf2e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page