Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryChat MessagesMemory typesConversation BufferConversation Buffer WindowEntityConversation Knowledge GraphConversation SummaryConversation Summary BufferConversation Token BufferBacked by a Vector StoreMemory in LLMChainMemory in the Multi-Input ChainMemory in AgentMessage Memory in Agent backed by a databaseCustomizing Conversational MemoryCustom MemoryMultiple Memory classesAgentsCallbacksModulesSecurityGuidesMoreModulesMemoryMemory typesConversation BufferOn this pageConversation BufferThis notebook shows how to use ConversationBufferMemory. This memory allows for storing messages and then extracts the messages in a variable.We can first extract it as a string.from langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory()memory.save_context({"input": "hi"}, {"output": "whats up"})memory.load_memory_variables({})    {'history': 'Human: hi\nAI: whats up'}We can also get the history as a list of messages (this is useful if you are using this with a chat model).memory = ConversationBufferMemory(return_messages=True)memory.save_context({"input": "hi"}, {"output": "whats up"})memory.load_memory_variables({})    {'history': [HumanMessage(content='hi', additional_kwargs={}),      AIMessage(content='whats up', additional_kwargs={})]}Using in a chain‚ÄãFinally, let's take a look at using this in a chain (setting verbose=True so we can see the prompt).from langchain.llms import OpenAIfrom langchain.chains import ConversationChainllm = OpenAI(temperature=0)conversation = ConversationChain(    llm=llm,    verbose=True,    memory=ConversationBufferMemory())conversation.predict(input="Hi there!")    > Entering new ConversationChain chain...    Prompt
This notebook shows how to use ConversationBufferMemory. This memory allows for storing messages and then extracts the messages in a variable.
This notebook shows how to use ConversationBufferMemory. This memory allows for storing messages and then extracts the messages in a variable. ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryChat MessagesMemory typesConversation BufferConversation Buffer WindowEntityConversation Knowledge GraphConversation SummaryConversation Summary BufferConversation Token BufferBacked by a Vector StoreMemory in LLMChainMemory in the Multi-Input ChainMemory in AgentMessage Memory in Agent backed by a databaseCustomizing Conversational MemoryCustom MemoryMultiple Memory classesAgentsCallbacksModulesSecurityGuidesMoreModulesMemoryMemory typesConversation BufferOn this pageConversation BufferThis notebook shows how to use ConversationBufferMemory. This memory allows for storing messages and then extracts the messages in a variable.We can first extract it as a string.from langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory()memory.save_context({"input": "hi"}, {"output": "whats up"})memory.load_memory_variables({})    {'history': 'Human: hi\nAI: whats up'}We can also get the history as a list of messages (this is useful if you are using this with a chat model).memory = ConversationBufferMemory(return_messages=True)memory.save_context({"input": "hi"}, {"output": "whats up"})memory.load_memory_variables({})    {'history': [HumanMessage(content='hi', additional_kwargs={}),      AIMessage(content='whats up', additional_kwargs={})]}Using in a chain‚ÄãFinally, let's take a look at using this in a chain (setting verbose=True so we can see the prompt).from langchain.llms import OpenAIfrom langchain.chains import ConversationChainllm = OpenAI(temperature=0)conversation = ConversationChain(    llm=llm,    verbose=True,    memory=ConversationBufferMemory())conversation.predict(input="Hi there!")    > Entering new ConversationChain chain...    Prompt