|
| 1 | +--- |
| 2 | +title: "AgentMail" |
| 3 | +description: "Load messages from an AgentMail inbox as LangChain Documents." |
| 4 | +--- |
| 5 | + |
| 6 | +`AgentMailLoader` streams messages from an [AgentMail](https://agentmail.to) inbox as LangChain `Document`s—one document per message, plain-text body as `page_content`, sender / subject / labels / thread / attachment metadata on `metadata`. Useful for indexing an inbox into a vector store for RAG over email. |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +| Class | Package | |
| 11 | +|:------|:--------| |
| 12 | +| `AgentMailLoader` | [`langchain-agentmail`](https://pypi.org/project/langchain-agentmail/) | |
| 13 | + |
| 14 | +## Setup |
| 15 | + |
| 16 | +Install the package: |
| 17 | + |
| 18 | +```bash |
| 19 | +pip install -qU langchain-agentmail |
| 20 | +``` |
| 21 | + |
| 22 | +Set your AgentMail API key (get one at [agentmail.to](https://agentmail.to)): |
| 23 | + |
| 24 | +```python |
| 25 | +import getpass |
| 26 | +import os |
| 27 | + |
| 28 | +if not os.environ.get("AGENTMAIL_API_KEY"): |
| 29 | + os.environ["AGENTMAIL_API_KEY"] = getpass.getpass("AgentMail API key:\n") |
| 30 | +``` |
| 31 | + |
| 32 | +## Instantiation |
| 33 | + |
| 34 | +```python |
| 35 | +from langchain_agentmail import AgentMailLoader |
| 36 | + |
| 37 | +loader = AgentMailLoader( |
| 38 | + inbox_id="ib_abc123", |
| 39 | + labels=["inbox"], # optional — filter messages by label |
| 40 | + limit=100, # optional — cap the number of messages loaded |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +## Load |
| 45 | + |
| 46 | +```python |
| 47 | +docs = loader.load() |
| 48 | +print(docs[0].page_content[:200]) |
| 49 | +print(docs[0].metadata) |
| 50 | +``` |
| 51 | + |
| 52 | +Each `Document` includes the following metadata keys (when present): |
| 53 | + |
| 54 | +- `inbox_id`, `message_id`, `thread_id` |
| 55 | +- `from`, `to`, `cc`, `subject`, `labels`, `timestamp` |
| 56 | +- `has_attachments`, `attachments`: a list of `{attachment_id, filename, content_type, size}` |
| 57 | + |
| 58 | +To download attachment bytes, pair the loader with `AgentMailGetAttachmentTool` from the toolkit—it returns a presigned download URL. |
| 59 | + |
| 60 | +## Lazy load |
| 61 | + |
| 62 | +For larger inboxes, stream documents one at a time instead of materializing the full list: |
| 63 | + |
| 64 | +```python |
| 65 | +for doc in loader.lazy_load(): |
| 66 | + print(doc.metadata["subject"]) |
| 67 | +``` |
| 68 | + |
| 69 | +## Indexing into a vector store |
| 70 | + |
| 71 | +```python |
| 72 | +from langchain_core.vectorstores import InMemoryVectorStore |
| 73 | +from langchain_openai import OpenAIEmbeddings |
| 74 | + |
| 75 | +docs = AgentMailLoader(inbox_id="ib_abc123", limit=200).load() |
| 76 | + |
| 77 | +store = InMemoryVectorStore.from_documents(docs, OpenAIEmbeddings()) |
| 78 | +retriever = store.as_retriever(search_kwargs={"k": 5}) |
| 79 | + |
| 80 | +retriever.invoke("Q3 invoice from acme") |
| 81 | +``` |
| 82 | + |
| 83 | +## API reference |
| 84 | + |
| 85 | +The package source lives at [github.com/agentmail-to/langchain-agentmail](https://github.com/agentmail-to/langchain-agentmail). |
0 commit comments