Tuesday, 30 December 2025

LangChain overview

LangChain is an open source framework with a pre-built agent architecture and integrations for any model or tool — so you can build agents that adapt as fast as the ecosystem evolves

LangChain is the easiest way to start building agents and applications powered by LLMs. With under 10 lines of code, you can connect to OpenAI, Anthropic, Google, and more. LangChain provides a pre-built agent architecture and model integrations to help you get started quickly and seamlessly incorporate LLMs into your agents and applications.

LangChain sequence is chained with Pipe and it is call LCEL


RunnableLambda


A LangChain runnable is a protocol that allows you to create and invoke custom chains. It's designed to sequence tasks, taking the output of one call and feeding it as input to the next, making it suitable for straightforward, linear tasks where each step directly builds upon the previous one.

Eg:

# Simple LCEL Example: String Transformation Chain
from langchain_core.runnables import RunnableLambda

# Create simple transformation functions
def uppercase(text: str) -> str:
    """Convert text to uppercase"""
    print(f"  Step 1: uppercase → {text.upper()}")
    return text.upper()

def add_prefix(text: str) -> str:
    """Add a prefix to text"""
    result = f"RESULT: {text}"
    print(f"  Step 2: add_prefix → {result}")
    return result

def add_emoji(text: str) -> str:
    """Add emoji to text"""
    result = f"✅ {text}"
    print(f"  Step 3: add_emoji → {result}")
    return result

# Create runnables (components that can be chained)
uppercase_runnable = RunnableLambda(uppercase)
prefix_runnable = RunnableLambda(add_prefix)
emoji_runnable = RunnableLambda(add_emoji)

# Build the chain using LCEL
chain = uppercase_runnable | prefix_runnable | emoji_runnable

# Execute the chain
print("Input: 'hello langchain'")
print("\nProcessing:")
result = chain.invoke("hello langchain")
print(f"\nFinal Output: {result}")


Input: 'hello langchain' Processing: Step 1: uppercase → HELLO LANGCHAIN Step 2: add_prefix → RESULT: HELLO LANGCHAIN Step 3: add_emoji → ✅ RESULT: HELLO LANGCHAIN Final Output: ✅ RESULT: HELLO LANGCHAIN

Explanation: In this sequence upon the call chain.invoke("hello langchain") the input text "hello langchain" is first passed to uppercase_runnable which then calls the function uppercase.
The output of this function is then again passed to the next function in the chain which is prefix_runnable and then it's output is again passed to emoji_runnable. In this way LambdaRunnable
help to cascade the output from one function call to it's next call in the sequence.

No comments:

Post a Comment

Building a ReAct Agent with LangGraph & LangSmith

In this post, I walk through building a ReAct (Reasoning + Acting) agent using LangGraph and Groq's openai/gpt-oss-120b model, where the...