Migrating from ConversationalChain
Source: langchain

# Migrating from ConversationalChain[`ConversationChain`](https://python.langchain.com/v0.2/api_reference/langchain/chains/langchain.chains.conversation.base.ConversationChain.html) incorporated a memory of previous messages to sustain a stateful conversation.Some advantages of switching to the LCEL implementation are:- Innate support for threads/separate sessions. To make this work with `ConversationChain`, you'd need to instantiate a separate memory class outside the chain.- More explicit parameters. `ConversationChain` contains a hidden default prompt, which can cause confusion.- Streaming support. `ConversationChain` only supports streaming via callbacks.`RunnableWithMessageHistory` implements sessions via configuration parameters. It should be instantiated with a callable that returns a [chat message history](https://python.langchain.com/v0.2/api_reference/core/chat_history/langchain_core.chat_history.BaseChatMessageHistory.html). By default, it expects this function to take a single argument `session_id`.```python%pip install --upgrade --quiet langchain langchain-openai``````pythonimport osfrom getpass import getpassos.environ["OPENAI_API_KEY"] = getpass()```## Legacy<details open>```pythonfrom langchain.chains import ConversationChainfrom langchain.memory import ConversationBufferMemoryfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_openai import ChatOpenAItemplate = """You are a pirate. Answer the following questions as best you can.Chat history: {history}Question: {input}"""prompt = ChatPromptTemplate.from_template(template)memory = ConversationBufferMemory()chain = ConversationChain(    llm=ChatOpenAI(),    memory=memory,    prompt=prompt,)chain({"input": "how are you?"})```    {'input': 'how are you?',     'history': '',     'response': "Arr matey, I be doin' well on the high seas, plunderin' and pillagin' as usual. How be ye?"}</details>## LCEL<details open>```pythonfrom langchain_core.chat_history import InMemoryChatMessageHistoryfrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.runnables.history import RunnableWithMessageHistoryfrom langchain_openai import ChatOpenAIprompt = ChatPromptTemplate.from_messages(    [        ("system", "You are a pirate. Answer the following questions as best you can."),        ("placeholder", "{chat_history}"),        ("human", "{input}"),    ])history = InMemoryChatMessageHistory()def get_history():    return historychain = prompt | ChatOpenAI() | StrOutputParser()wrapped_chain = RunnableWithMessageHistory(    chain,    get_history,    history_messages_key="chat_history",)wrapped_chain.invoke({"input": "how are you?"})```    "Arr, me matey! I be doin' well, sailin' the high seas and searchin' for treasure. How be ye?"The above example uses the same `history` for all sessions. The example below shows how to use a different chat history for each session.```pythonfrom langchain_core.chat_history import BaseChatMessageHistoryfrom langchain_core.runnables.history import RunnableWithMessageHistorystore = {}def get_session_history(session_id: str) -> BaseChatMessageHistory:    if session_id not in store:        store[session_id] = InMemoryChatMessageHistory()    return store[session_id]chain = prompt | ChatOpenAI() | StrOutputParser()wrapped_chain = RunnableWithMessageHistory(    chain,    get_session_history,    history_messages_key="chat_history",)wrapped_chain.invoke(    {"input": "Hello!"},    config={"configurable": {"session_id": "abc123"}},)```    'Ahoy there, me hearty! What can this old pirate do for ye today?'</details>## Next stepsSee [this tutorial](/docs/tutorials/chatbot) for a more end-to-end guide on building with [`RunnableWithMessageHistory`](https://python.langchain.com/v0.2/api_reference/core/runnables/langchain_core.runnables.history.RunnableWithMessageHistory.html).Check out the [LCEL conceptual docs](/docs/concepts/#langchain-expression-language-lcel) for more background information.