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

Written by - Aionlinecourse1551 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

Topic modeling using K-means clustering to group customer reviews

Have you ever thought about the ways one can analyze a review to extract all the misleading or useful information?...

Natural 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

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

Build Regression Models in Python for House Price Prediction

Ever wondered how experts predict house prices? This project dives into exactly that! Using Python, we'll build regression models that...

Machine Learning
Deep Learning Interview Guide

Optimizing Chunk Sizes for Efficient and Accurate Document Retrieval Using HyDE Evaluation

This project demonstrates the integration of generative AI techniques with efficient document retrieval by leveraging GPT-4 and vector indexing. It...

Natural Language ProcessingGenerative AI
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