Skip to content

Error in RootListenersTracer.on_chain_end callback: KeyError('answer') while streaming a RAG Chain Output #24713

Description

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

I have the following code for building a RAG Chatbot (using this example):

from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain.chains import create_retrieval_chain
from langchain.chains.history_aware_retriever import create_history_aware_retriever
from langchain.chains.combine_documents import create_stuff_documents_chain

vectordb = FAISS.load_local(persist_directory, embedding, index_name, allow_dangerous_deserialization=True)
retriever=vectordb.as_retriever()

llm = ChatOpenAI()

....
prompt={.....}
....

question_answer_chain = create_stuff_documents_chain(llm, prompt, output_parser=parser)
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)

conversational_rag_chain = RunnableWithMessageHistory(
    rag_chain,
    get_session_history,
    input_messages_key="input",
    history_messages_key="chat_history",
    output_messages_key="answer",
)

while True:
    query = input("Ask a question: ")

    for chunk in conversational_rag_chain.stream(
        {"input": query,},
        config={
            "configurable": {
                "session_id": "demo_1"
            }
        }
    ):
        if answer_chunk := chunk.get("answer"):
            print(f"{answer_chunk}", end="", flush=True)
    
    print()

Error Message and Stack Trace (if applicable)

Ask a question: How many colors are in rainbow?
Error in RootListenersTracer.on_chain_end callback: KeyError('answer')
Error in callback coroutine: KeyError('answer')
A rainbow typically has seven colors, which are: Red, Orange, Yellow, Green, Blue, Indigo, Violet.</s>
Ask a question: 

Description

Hi,

I am trying to get the answer as stream, the problem is whenever the conversational_rag_chain.stream() is initiating, using an input it is giving the following errors:
Error in RootListenersTracer.on_chain_end callback: KeyError('answer')
Error in callback coroutine: KeyError('answer')
and then the output is printing as intended.

My question is, how can I solve it? I have entered output_messages_key="answer" in the conversational_rag_chain already, so am I doing something wrong or is a bug?

Any little discussion or help is welcome. Thanks in advance.

System Info

System Information

OS: Linux
OS Version: #39-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul 5 21:49:14 UTC 2024
Python Version: 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0]

Package Information

langchain_core: 0.2.23
langchain: 0.2.11
langchain_community: 0.2.10
langsmith: 0.1.93
langchain_chroma: 0.1.2
langchain_openai: 0.1.17
langchain_text_splitters: 0.2.2

Packages not installed (Not Necessarily a Problem)

The following packages were not found:

langgraph
langserve

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelated to a bug, vulnerability, unexpected error with an existing featureinvestigateFlagged for investigation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions