# Lesson 4: Persistence and Streaming -\> AttributeError: '\_GeneratorContextManager' object has no attribute 'get\_next\_version'

**URL:** <https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391>\
**Category:** AI Agents in LangGraph\
**Created:** [September 24, 2024, 4:20pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391 "2024-09-24T16:20:43Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordi\_Lucas\_Rial](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jordi\_Lucas\_Rial](https://community.deeplearning.ai/u/Jordi_Lucas_Rial)\
**Post date:** [September 24, 2024, 4:20pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/1 "2024-09-24T16:20:43Z")

</div>

Hi everyone!

I have the error bellow after that I execute this code:

for event in abot.graph.stream({“messages”: messages}, thread):  
for v in event.values():  
print(v[‘messages’])

* * *

→ 663 self.checkpointer\_get\_next\_version = checkpointer.get\_next\_version  
664 self.checkpointer\_put\_writes = checkpointer.put\_writes  
665 else:

AttributeError: ‘\_GeneratorContextManager’ object has no attribute ‘get\_next\_version’

Someone with the same problem?

Thanks!

---

<div class="post-metadata">

**Author:** ![Jordi\_Lucas\_Rial](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jordi\_Lucas\_Rial](https://community.deeplearning.ai/u/Jordi_Lucas_Rial)\
**Post date:** [September 24, 2024, 4:52pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/2 "2024-09-24T16:52:33Z")

</div>

It works if you put all code that depens on “memory” inside a **with** declaration:

```
with SqliteSaver.from_conn_string(":memory:") as memory:
  abot = Agent(model, [tool], system=prompt, checkpointer=memory)

  messages = [HumanMessage(content="What is the weather in sf?")]
  thread = {"configurable": {"thread_id": "1"}}
  
  for event in abot.graph.stream({"messages": messages}, thread):
      for v in event.values():
          print(v['messages'])

```

Someone with a solution without refactoring?

Thanks!

---

<div class="post-metadata">

**Author:** ![wnakano](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@wnakano](https://community.deeplearning.ai/u/wnakano)\
**Post date:** [October 22, 2024, 2:20am UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/3 "2024-10-22T02:20:52Z")

</div>

There is a quite forward solution. Create a .db file (e.g checkpoints.db). If you created this .db file in the same level as the script/notebook, you can go straight:

```python
from langgraph.checkpoint.sqlite import SqliteSaver

db_path = 'checkpoints.db'
conn = sqlite3.connect(db_path, check_same_thread=False)

memory = SqliteSaver(conn)

```

Remark: the addition of `check_same_thread=False` comes from the requirement of the db connection and its writer being available in the same thread.

---

<div class="post-metadata">

**Author:** ![jxstanford](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/jxstanford/32/98500_2.png) [@jxstanford](https://community.deeplearning.ai/u/jxstanford)\
**Post date:** [October 27, 2024, 12:52am UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/4 "2024-10-27T00:52:53Z")

</div>

This works for the synchronous example. The code can be spread across cells while still using the context.

```auto
from contextlib import ExitStack

stack = ExitStack()
memory = stack.enter_context(SqliteSaver.from_conn_string(":memory:"))

abot = Agent(model, [tool], system=prompt, checkpointer=memory)
messages = [HumanMessage(content="What is the weather in Issaquah?")]
thread = {"configurable": {"thread_id": "1"}}
for event in abot.graph.stream({"messages": messages}, thread):
    for v in event.values():
        print(v['messages'])

messages = [HumanMessage(content="What about in la?")]
thread = {"configurable": {"thread_id": "1"}}
for event in abot.graph.stream({"messages": messages}, thread):
    for v in event.values():
        print(v)

messages = [HumanMessage(content="Which one is warmer?")]
thread = {"configurable": {"thread_id": "1"}}
for event in abot.graph.stream({"messages": messages}, thread):
    for v in event.values():
        print(v)

messages = [HumanMessage(content="Which one is warmer?")]
thread = {"configurable": {"thread_id": "2"}}
for event in abot.graph.stream({"messages": messages}, thread):
    for v in event.values():
        print(v)

# Exit all contexts
stack.close()

```

Here is a similar solution for the async version:

```auto
import asyncio
from contextlib import AsyncExitStack
from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver

stack = AsyncExitStack()
memory = await stack.enter_async_context(AsyncSqliteSaver.from_conn_string(":memory:"))

abot = Agent(model, [tool], system=prompt, checkpointer=memory)

messages = [HumanMessage(content="What is the weather in SF?")]
thread = {"configurable": {"thread_id": "4"}}
async for event in abot.graph.astream_events({"messages": messages}, thread, version="v1"):
        kind = event["event"]
        if kind == "on_chat_model_stream":
            content = event["data"]["chunk"].content
            if content:
                # Empty content in the context of OpenAI means
                # that the model is asking for a tool to be invoked.
                # So we only print non-empty content
                print(content, end="|")

await stack.aclose()

```

---

<div class="post-metadata">

**Author:** ![linux2ram](https://avatars.discourse-cdn.com/v4/letter/l/f14d63/32.png) [@linux2ram](https://community.deeplearning.ai/u/linux2ram)\
**Post date:** [October 31, 2024, 8:46pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/5 "2024-10-31T20:46:55Z")

</div>

from langgraph.checkpoint.memory import MemorySaver

memory = MemorySaver()

fixed the issue

---

<div class="post-metadata">

**Author:** ![aifaber](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@aifaber](https://community.deeplearning.ai/u/aifaber)\
**Post date:** [November 11, 2024, 11:54am UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/6 "2024-11-11T11:54:31Z")

</div>

memory = MemorySaver()

It works. !!  
Then What about “AsyncSqliteSaver”?

---

<div class="post-metadata">

**Author:** ![milcio79](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@milcio79](https://community.deeplearning.ai/u/milcio79)\
**Post date:** [December 2, 2024, 4:45pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/7 "2024-12-02T16:45:46Z")

</div>

I changed one function from this:

```auto
def call_openai(self, state: AgentState):
        messages = model.invoke(state['messages'])
        if self.system:
            messages = [SystemMessage(content=self.system)] + messages
        message = self.model.invoke(messages)
        return {'messages': [message]}

```

to this:

```auto
def call_openai(self, state):
        messages = state['messages']
        if self.system:
            messages = [SystemMessage(content=self.system)] + messages
        response = self.model.invoke(messages)
        return {'messages': [response]}

```

and changed every interaction and call to the model, that were like this:

```auto
for event in abot.graph.stream({"messages": messages}, thread):
    for v in event.values():
        print(v['messages'])

```

to this:

```auto
with SqliteSaver.from_conn_string(":memory:") as checkpointer:
    agent = Agent(model, [tool], checkpointer, system=prompt)

    # Mensajes de prueba
    messages = [HumanMessage(content="What is the weather in SF?")]
    thread = {"configurable": {"thread_id": "1"}}

    # Streaming de eventos
    for event in agent.graph.stream({"messages": messages}, thread):
        for value in event.values():
            print(value['messages'])

```

And now is working well!!!

---

<div class="post-metadata">

**Author:** ![Jenys](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jenys](https://community.deeplearning.ai/u/Jenys)\
**Post date:** [January 3, 2025, 1:58pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/8 "2025-01-03T13:58:42Z")

</div>

Hi,

Have you figured out how to fix it for this part:

from langgraph.checkpoint.aiosqlite import AsyncSqliteSaver  
memory = AsyncSqliteSaver.from\_conn\_string(“:memory:”)

It is clear that  
from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver  
should be used instead but this is still not working:

memory = AsyncSqliteSaver.from\_conn\_string(“:memory:”)

and if I use:  
memory = MemorySaver()

I don’t get the tokens ☹  
I implemented 5 different versions with the suggestions from here and also from gpt-o1 but none returns tokens, even though tokenstreaming is set to true for the model.  
According to gpt-o1 debugging: Based on your debug output, your model is sending _only a single chunk_ with either an empty content or the full final content—i.e. **no incremental tokens** at all.

It would be good if this lesson is updated to reflect the current state.

---

<div class="post-metadata">

**Author:** ![fanzhou0111](https://avatars.discourse-cdn.com/v4/letter/f/6bbea6/32.png) [@fanzhou0111](https://community.deeplearning.ai/u/fanzhou0111)\
**Post date:** [January 9, 2025, 5:16pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/9 "2025-01-09T17:16:32Z")

</div>

Thank you, this works for me. Appreciate!

---

<div class="post-metadata">

**Author:** ![fanzhou0111](https://avatars.discourse-cdn.com/v4/letter/f/6bbea6/32.png) [@fanzhou0111](https://community.deeplearning.ai/u/fanzhou0111)\
**Post date:** [January 9, 2025, 5:48pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/10 "2025-01-09T17:48:11Z")

</div>

this helped me. Thank you !

---

<div class="post-metadata">

**Author:** ![cde](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/cde/32/65928_2.png) [@cde](https://community.deeplearning.ai/u/cde)\
**Post date:** [April 21, 2025, 7:22pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/11 "2025-04-21T19:22:22Z")

</div>

I know it’s been many months since your reply. Thank you BTW. I just wondering if for lesson 5 you also yse `MemorySaver`, and if you did, could you please share your implementation? It was working for me at first, but after I clear all outputs and restart, I keep getting `IndexError: list index out of range`  
I would really appreciate your help!

---

<div class="post-metadata">

**Author:** ![cde](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/cde/32/65928_2.png) [@cde](https://community.deeplearning.ai/u/cde)\
**Post date:** [April 21, 2025, 8:05pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/12 "2025-04-21T20:05:30Z")

</div>

NVM, I realized that there was a mistake in the `reduce` function. The IDE re-arranged the indentation around the “for” loop. Thanks!

---

<div class="post-metadata">

**Author:** ![emilio\_cobog](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/emilio_cobog/32/136785_2.png) [@emilio\_cobog](https://community.deeplearning.ai/u/emilio_cobog)\
**Post date:** [May 15, 2025, 10:06pm UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/13 "2025-05-15T22:06:05Z")

</div>

It’s true, ‘from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver’ works, but then, when executing the streaming tokens funcionality I got this error:

AttributeError: ‘\_AsyncGeneratorContextManager’ object has no attribute ‘get\_next\_version’

Have you solved it by any chance?

---

<div class="post-metadata">

**Author:** ![yinhe](https://avatars.discourse-cdn.com/v4/letter/y/f05b48/32.png) [@yinhe](https://community.deeplearning.ai/u/yinhe)\
**Post date:** [June 13, 2025, 8:41am UTC](https://community.deeplearning.ai/t/lesson-4-persistence-and-streaming-attributeerror-generatorcontextmanager-object-has-no-attribute-get-next-version/697391/14 "2025-06-13T08:41:33Z")

</div>

```auto
import asyncio

from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
from langgraph.prebuilt import create_react_agent

from common.llm_models import gpt_4o_mini

async def main():
    # Create an AsyncSqliteSaver asynchronous context manager
    saver = AsyncSqliteSaver.from_conn_string("checkpoints.db")
    # Enter the asynchronous context manager to get the actual checkpointer instance
    checkpointer = await saver. __aenter__ ()
    agent = create_react_agent(gpt_4o_mini, tools=[], checkpointer=checkpointer)
    response = agent.astream({"messages": "who are you?", "remaining_steps": 3},
                             stream_mode="messages",
                             config={"configurable": {"thread_id": "123"}}
                             )
    async for chunk in response:
        print(chunk)
    await saver. __aexit__ (None, None, None)

if __name__ == ' __main__':
    asyncio.run(main())

```

If `async with` is not used, the above approach can be used to manage the context manually.  
The `async with` statement automatically calls ` __aenter__ ()` to obtain the `checkpointer` object.
