[Solved] TypeError when chaining Runnables in LangChain: Expected a Runnable, callable or dict

Written by- Aionlinecourse126 times views

[Solved] TypeError when chaining Runnables in LangChain: Expected a Runnable, callable or dict

In the field of natural language processing, LangChain or the Large Language Model brings the evolution of the ability to understand more complex queries of human language. So, this powerful framework is commonly used for building applications. However, working with this framework sometimes we might encounter this type of error "TypeError: when chaining Runnables in LangChain: Expected a Runnable, callable or dict". To fix this issue we first understand why we get this error.

We get this error basically when we try to chain multiple tasks(Runnables) at the same time, but one or more tasks do not meet the expected type requirements. A runnable is an object that is defined in a 'run' method. A callable is any function or method, and a dict should map string keys to callable or runnable objects in LangChain.


Solution 1:

The issue is context of type str. Pass in a lambda function instead:

  rag_chain = (
    {"context": lambda x: context, "question": RunnablePassthrough()} | rag_custom_prompt | llm
)


Solution 2:

Use llm from langchain.llms library for chain instead

from langchain.llms import OpenAI

llm = OpenAI(model_name="gpt-3.5-turbo")

We can fix the error "TypeError: when chaining Runnables in LangChain: Expected a Runnable, callable or dict" by following these steps. These techniques help to maintain the flexibility and modularity of our text processing workflows.


Thank you for reading the article.