Skip to content

Commit fb3dc25

Browse files
authored
Merge pull request #116 from couchbase-examples/artifact/langgraph-couchbase-persistence-2026-04-28
Refresh LangGraph Couchbase persistence notebook
2 parents 3a80129 + d6f0e16 commit fb3dc25

1 file changed

Lines changed: 36 additions & 53 deletions

File tree

langgraph/couchbase_persistence_langgraph.ipynb

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@
5757
"cell_type": "markdown",
5858
"metadata": {},
5959
"source": [
60-
"Requires Couchbase Python SDK and langgraph package"
60+
"Requires Couchbase Python SDK, LangGraph, and langchain-openai.\n",
61+
"\n",
62+
"Set `OPENAI_API_KEY` before running. For local Couchbase runs, this notebook also reads `CB_CONN_STR`, `CB_USER`, `CB_PASS`, `CB_BUCKET_NAME`, and `CB_SCOPE_NAME` from the environment.\n",
63+
"\n",
64+
"For the local example below, create bucket `test`, scope `langgraph`, and the `checkpoints` / `checkpoint_writes` collections first, or provide equivalent names through the environment variables. The default `CB_*` values shown later are for local development only.\n"
6165
]
6266
},
6367
{
@@ -67,7 +71,7 @@
6771
"outputs": [],
6872
"source": [
6973
"%%capture --no-stderr\n",
70-
"%pip install -U langgraph==0.3.22 langgraph-checkpointer-couchbase "
74+
"%pip install -U langgraph==1.1.10 langgraph-checkpointer-couchbase langchain-openai\n"
7175
]
7276
},
7377
{
@@ -88,11 +92,21 @@
8892
"\n",
8993
"\n",
9094
"def _set_env(var: str):\n",
91-
" if not os.environ.get(var):\n",
92-
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
95+
" if os.environ.get(var):\n",
96+
" return os.environ[var]\n",
97+
" value = getpass.getpass(f\"{var}: \")\n",
98+
" if not value:\n",
99+
" raise RuntimeError(f\"{var} must be set before running this notebook\")\n",
100+
" os.environ[var] = value\n",
101+
" return value\n",
93102
"\n",
94103
"\n",
95-
"_set_env(\"OPENAI_API_KEY\")"
104+
"_set_env(\"OPENAI_API_KEY\")\n",
105+
"CB_CONN_STR = os.environ.get(\"CB_CONN_STR\", \"couchbase://localhost\")\n",
106+
"CB_USERNAME = os.environ.get(\"CB_USER\", \"Administrator\")\n",
107+
"CB_PASSWORD = os.environ.get(\"CB_PASS\", \"password\")\n",
108+
"CB_BUCKET_NAME = os.environ.get(\"CB_BUCKET_NAME\", \"test\")\n",
109+
"CB_SCOPE_NAME = os.environ.get(\"CB_SCOPE_NAME\", \"langgraph\")\n"
96110
]
97111
},
98112
{
@@ -120,7 +134,7 @@
120134
"from typing import Literal\n",
121135
"from langchain_core.tools import tool\n",
122136
"from langchain_openai import ChatOpenAI\n",
123-
"from langgraph.prebuilt import create_react_agent\n",
137+
"from langchain.agents import create_agent\n",
124138
"\n",
125139
"\n",
126140
"@tool\n",
@@ -142,14 +156,7 @@
142156
"cell_type": "markdown",
143157
"metadata": {},
144158
"source": [
145-
"### Couchbase Connection and intialization\n",
146-
"\n",
147-
"There are 2 ways to initialize a saver.\n",
148-
"\n",
149-
"1. `from_conn_info` - Provide details of the connection string, username, password. The package will handle connection itself.\n",
150-
"2. `from_cluster` - Provide a connected Couchbase.Cluster object. \n",
151-
"\n",
152-
"We will be using `from_conn_info` in the sync tutorial and `from_cluster` in the async one, but any of the above can be used as per requirements\n"
159+
"### Couchbase Connection and initialization\n\nThere are 2 ways to initialize a saver.\n\n1. `from_conn_info` - Provide details of the connection string, username, password. The package will handle connection itself.\n2. `from_cluster` - Provide a connected Couchbase.Cluster object. \n\nWe will be using `from_conn_info` in the sync tutorial and `from_cluster` in the async one, but any of the above can be used as per requirements\n"
153160
]
154161
},
155162
{
@@ -163,18 +170,7 @@
163170
"cell_type": "markdown",
164171
"metadata": {},
165172
"source": [
166-
"Below is usage of CouchbaseSaver (for synchronous use of graph, i.e. `.invoke()`, `.stream()`). CouchbaseSaver implements four methods that are required for any checkpointer:\n",
167-
"\n",
168-
"- `.put` - Store a checkpoint with its configuration and metadata.\n",
169-
"- `.put_writes` - Store intermediate writes linked to a checkpoint (i.e. pending writes).\n",
170-
"- `.get_tuple` - Fetch a checkpoint tuple using a given configuration (`thread_id` and `checkpoint_id`).\n",
171-
"- `.list` - List checkpoints that match a given configuration and filter criteria.\n",
172-
"\n",
173-
"Here we will create a Couchbase connection. We are using local setup with bucket `test`, `langgraph` scope. You may change bucket and scope if required. We will also require `checkpoints` and `checkpoint_writes` as collections inside.\n",
174-
"\n",
175-
"Then a [ReAct Agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/) is created with GPT Model, weather tool and Couchbase checkpointer.\n",
176-
"\n",
177-
"LangGraph's graph is invoked with message for GPT, storing all the state in Couchbase. We use get, get_tuple and list methods to fetch the states again"
173+
"Below is usage of CouchbaseSaver (for synchronous use of graph, i.e. `.invoke()`, `.stream()`). CouchbaseSaver implements four methods that are required for any checkpointer:\n\n- `.put` - Store a checkpoint with its configuration and metadata.\n- `.put_writes` - Store intermediate writes linked to a checkpoint (i.e. pending writes).\n- `.get_tuple` - Fetch a checkpoint tuple using a given configuration (`thread_id` and `checkpoint_id`).\n- `.list` - List checkpoints that match a given configuration and filter criteria.\n\nHere we will create a Couchbase connection. We are using local setup with bucket `test`, `langgraph` scope. You may change bucket and scope if required. We will also require `checkpoints` and `checkpoint_writes` as collections inside the configured scope.\n\nThen a [ReAct Agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/) is created with GPT Model, weather tool and Couchbase checkpointer.\n\nLangGraph's graph is invoked with message for GPT, storing all the state in Couchbase. We use get, get_tuple and list methods to fetch the states again"
178174
]
179175
},
180176
{
@@ -186,19 +182,19 @@
186182
"from langgraph_checkpointer_couchbase import CouchbaseSaver\n",
187183
"\n",
188184
"with CouchbaseSaver.from_conn_info(\n",
189-
" cb_conn_str=\"couchbase://localhost\",\n",
190-
" cb_username=\"Administrator\",\n",
191-
" cb_password=\"password\",\n",
192-
" bucket_name=\"test\",\n",
193-
" scope_name=\"langgraph\",\n",
185+
" cb_conn_str=CB_CONN_STR,\n",
186+
" cb_username=CB_USERNAME,\n",
187+
" cb_password=CB_PASSWORD,\n",
188+
" bucket_name=CB_BUCKET_NAME,\n",
189+
" scope_name=CB_SCOPE_NAME,\n",
194190
") as checkpointer:\n",
195-
" graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)\n",
191+
" graph = create_agent(model, tools=tools, checkpointer=checkpointer)\n",
196192
" config = {\"configurable\": {\"thread_id\": \"1\"}}\n",
197193
" res = graph.invoke({\"messages\": [(\"human\", \"what's the weather in sf\")]}, config)\n",
198194
" \n",
199195
" latest_checkpoint = checkpointer.get(config)\n",
200196
" latest_checkpoint_tuple = checkpointer.get_tuple(config)\n",
201-
" checkpoint_tuples = list(checkpointer.list(config))"
197+
" checkpoint_tuples = list(checkpointer.list(config))\n"
202198
]
203199
},
204200
{
@@ -291,16 +287,7 @@
291287
"cell_type": "markdown",
292288
"metadata": {},
293289
"source": [
294-
"This is the asynchronous example, Here we will create a Couchbase connection. We are using local setup with bucket `test`, `langgraph` scope. We will also require `checkpoints` and `checkpoint_writes` as collections inside. These are the methods supported by the library\n",
295-
"\n",
296-
"- `.aput` - Store a checkpoint with its configuration and metadata.\n",
297-
"- `.aput_writes` - Store intermediate writes linked to a checkpoint (i.e. pending writes).\n",
298-
"- `.aget_tuple` - Fetch a checkpoint tuple using a given configuration (`thread_id` and `checkpoint_id`).\n",
299-
"- `.alist` - List checkpoints that match a given configuration and filter criteria.\n",
300-
"\n",
301-
"Then a [ReAct Agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/) is created with GPT Model, weather tool and Couchbase checkpointer.\n",
302-
"\n",
303-
"LangGraph's graph is invoked with message for GPT, storing all the state in Couchbase. We use aget, aget_tuple and alist methods to fetch the states again"
290+
"This is the asynchronous example, Here we will create a Couchbase connection. We are using local setup with bucket `test`, `langgraph` scope. We will also require `checkpoints` and `checkpoint_writes` as collections inside the configured scope. These are the methods supported by the library\n\n- `.aput` - Store a checkpoint with its configuration and metadata.\n- `.aput_writes` - Store intermediate writes linked to a checkpoint (i.e. pending writes).\n- `.aget_tuple` - Fetch a checkpoint tuple using a given configuration (`thread_id` and `checkpoint_id`).\n- `.alist` - List checkpoints that match a given configuration and filter criteria.\n\nThen a [ReAct Agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/) is created with GPT Model, weather tool and Couchbase checkpointer.\n\nLangGraph's graph is invoked with message for GPT, storing all the state in Couchbase. We use aget, aget_tuple and alist methods to fetch the states again"
304291
]
305292
},
306293
{
@@ -314,13 +301,9 @@
314301
"from couchbase.auth import PasswordAuthenticator\n",
315302
"from couchbase.options import ClusterOptions\n",
316303
"\n",
317-
"cb_conn_str = \"couchbase://localhost\"\n",
318-
"cb_username = \"Administrator\"\n",
319-
"cb_password = \"password\"\n",
320-
"\n",
321-
"auth = PasswordAuthenticator(cb_username, cb_password)\n",
304+
"auth = PasswordAuthenticator(CB_USERNAME, CB_PASSWORD)\n",
322305
"options = ClusterOptions(auth)\n",
323-
"cb_cluster = await ACluster.connect(cb_conn_str, options)"
306+
"cb_cluster = await ACluster.connect(CB_CONN_STR, options)\n"
324307
]
325308
},
326309
{
@@ -333,18 +316,18 @@
333316
"\n",
334317
"async with AsyncCouchbaseSaver.from_cluster(\n",
335318
" cluster=cb_cluster,\n",
336-
" bucket_name=\"test\",\n",
337-
" scope_name=\"langgraph\",\n",
319+
" bucket_name=CB_BUCKET_NAME,\n",
320+
" scope_name=CB_SCOPE_NAME,\n",
338321
") as checkpointer:\n",
339-
" graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)\n",
322+
" graph = create_agent(model, tools=tools, checkpointer=checkpointer)\n",
340323
" config = {\"configurable\": {\"thread_id\": \"2\"}}\n",
341324
" res = await graph.ainvoke(\n",
342325
" {\"messages\": [(\"human\", \"what's the weather in nyc\")]}, config\n",
343326
" )\n",
344327
"\n",
345328
" latest_checkpoint = await checkpointer.aget(config)\n",
346329
" latest_checkpoint_tuple = await checkpointer.aget_tuple(config)\n",
347-
" checkpoint_tuples = [c async for c in checkpointer.alist(config)]"
330+
" checkpoint_tuples = [c async for c in checkpointer.alist(config)]\n"
348331
]
349332
},
350333
{

0 commit comments

Comments
 (0)