Facing ValidationError: 1 validation error for StuffDocumentsChain #25484
Replies: 1 comment 2 replies
-
Hello, @anand-it-is! I'm here to assist you with any bugs, questions, or contributions you might have. Feel free to ask for help while waiting for a human maintainer. To resolve the In your case, the Here's how you can modify your code:
Here's the updated code snippet: from langchain.chains import LLMChain, StuffDocumentsChain
from langchain.prompts import PromptTemplate
# Define the prompt template with additional instructions
prompt_template = """
You are a helpful assistant. Answer the question based on the provided documents.
- If you're instructed to answer the questions in 2 lines. Answer the question in 2 lines only.
- If the answer is not in the provided documents, say "I don't know".
- Use the provided documents to answer the question in detail.
Question: {query}
Context: {input_documents}
Answer:
"""
# Load the QA chain with the prompt template
prompt = PromptTemplate(
input_variables=["query", "input_documents"],
template=prompt_template
)
# Initialize the LLMChain
llm_chain = LLMChain(llm=llm, prompt=prompt)
# Initialize the StuffDocumentsChain with the correct document_variable_name
chain = StuffDocumentsChain(
llm_chain=llm_chain,
document_prompt=prompt,
document_variable_name="input_documents"
)
# Prepare inputs for the chain
input_documents = " ".join([doc.page_content for doc in docs]) # Concatenate document content into a single string
inputs = {
"query": query, # The user's question
"input_documents": input_documents # The concatenated text from relevant documents
}
# Get the response from the language model based on the query and documents
response = chain.run(inputs)
st.write(response) This ensures that the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I'm trying to implement Prompt Engineering using Langchain. I'm getting this error "ValidationError: 1 validation error for StuffDocumentsChain root document_variable_name context was not found in llm_chain input_variables: ['input_documents', 'query'] (type=value_error)
Traceback:
I'm not able to figure out the ways to sort this out.
System Info
After running python -m langchain_core.sys_info
Let me know if you need any other info.
I'm getting something like this in screenshot below


Beta Was this translation helpful? Give feedback.
All reactions