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

Written by - Aionlinecourse1072 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.

Recommended Projects

Deep Learning Interview Guide

Medical Image Segmentation With UNET

Have you ever thought about how doctors are so precise in diagnosing any conditions based on medical images? Quite simply,...

Computer Vision
Deep Learning Interview Guide

Build A Book Recommender System With TF-IDF And Clustering(Python)

Have you ever thought about the reasons behind the segregation and recommendation of books with similarities? This project is aimed...

Machine LearningDeep LearningNatural Language Processing
Deep Learning Interview Guide

Automatic Eye Cataract Detection Using YOLOv8

Cataracts are a leading cause of vision impairment worldwide, affecting millions of people every year. Early detection and timely intervention...

Computer Vision
Deep Learning Interview Guide

Crop Disease Detection Using YOLOv8

In this project, we are utilizing AI for a noble objective, which is crop disease detection. Well, you're here if...

Computer Vision
Deep Learning Interview Guide

Vegetable classification with Parallel CNN model

The Vegetable Classification project shows how CNNs can sort vegetables efficiently. As industries like agriculture and food retail grow, automating...

Machine LearningDeep Learning
Deep Learning Interview Guide

Banana Leaf Disease Detection using Vision Transformer model

Banana cultivation is a significant agricultural activity in many tropical and subtropical regions, providing a vital source of income and...

Deep LearningComputer Vision
Deep Learning Interview Guide

Credit Card Default Prediction Using Machine Learning Techniques

This project aims to develop and assess machine learning models in predicting customer defaults, assisting businesses in evaluating the risk...

Machine Learning