Skip to main content

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 LangMemOpenAIAgentToolAdapter  # 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 = LangMemOpenAIAgentToolAdapter(
    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 = LangMemOpenAIAgentToolAdapter(
    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 LangMemOpenAIAgentToolAdapter

# 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 = LangMemOpenAIAgentToolAdapter(manage_memory_tool_instance)
manage_memory_tool = adapter.as_tool()

# This tool will use the static namespace you provided.

API Reference

Class: LangMemOpenAIAgentToolAdapter[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!


Metadata

Release files for langmem-adapter 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langmem-adapter 0.0.2
File Size Uploaded Provenance Actions
langmem_adapter-0.0.2.tar.gz 16.7 kB Without provenance Details for langmem_adapter-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langmem-adapter 0.0.2
File Interpreter ABI Platform Actions
langmem_adapter-0.0.2-py3-none-any.whl Python 3 none any Details for langmem_adapter-0.0.2-py3-none-any.whl

Total release size: 23.8 kB

Release files / langmem_adapter-0.0.2.tar.gz

Download URL langmem_adapter-0.0.2.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
767f02df26a5ea32effc65307687239664af9706ef3f0a94ee4302d57425f262
BLAKE2b-256 checksum
How to use checksums
2dda822174e2e4b754754a04e86c7ce266210d441c0cafa3583e19e3402b4a5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.26

Release files / langmem_adapter-0.0.2-py3-none-any.whl

Download URL langmem_adapter-0.0.2-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
83e560047db4b4ab1cfcdd4b235a3288c4f6df67e74925147884a5964daa0489
BLAKE2b-256 checksum
How to use checksums
a672048e234fe4b5119079836e4409f00c597040d201d156791f237e25fbe808
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.26

Security for langmem-adapter 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page