0
0
LangchainHow-ToBeginner ยท 3 min read

How to Create a Custom Chain in LangChain: Step-by-Step Guide

To create a custom chain in LangChain, subclass Chain and implement the _call method to define your chain's logic. Then, specify the input and output keys by overriding input_keys and output_keys properties.
๐Ÿ“

Syntax

Creating a custom chain involves subclassing Chain and implementing the _call method which contains the chain's processing logic. You also define input_keys and output_keys properties to specify expected inputs and outputs.

  • _call(self, inputs: dict) -> dict: Core method where you write how inputs are processed to outputs.
  • input_keys: List of strings naming required input keys.
  • output_keys: List of strings naming output keys your chain returns.
python
from langchain.chains.base import Chain

class CustomChain(Chain):
    @property
    def input_keys(self):
        return ["input_text"]

    @property
    def output_keys(self):
        return ["output_text"]

    def _call(self, inputs: dict) -> dict:
        # Your custom logic here
        input_text = inputs["input_text"]
        output_text = input_text.upper()  # Example: convert to uppercase
        return {"output_text": output_text}
๐Ÿ’ป

Example

This example shows a simple custom chain that takes an input string and returns it in uppercase. It demonstrates subclassing Chain, defining input/output keys, and implementing the _call method.

python
from langchain.chains.base import Chain

class UpperCaseChain(Chain):
    @property
    def input_keys(self):
        return ["text"]

    @property
    def output_keys(self):
        return ["upper_text"]

    def _call(self, inputs: dict) -> dict:
        text = inputs["text"]
        upper_text = text.upper()
        return {"upper_text": upper_text}

# Using the custom chain
chain = UpperCaseChain()
result = chain.invoke({"text": "hello langchain"})
print(result)
Output
{"upper_text": "HELLO LANGCHAIN"}
โš ๏ธ

Common Pitfalls

Common mistakes when creating custom chains include:

  • Not defining input_keys or output_keys properly, causing runtime errors.
  • Forgetting to implement the _call method, which is required.
  • Returning outputs with keys not listed in output_keys.
  • Passing inputs that do not match input_keys.

Always ensure your input dictionary keys match input_keys and your returned dictionary keys match output_keys.

python
from langchain.chains.base import Chain

# Wrong: missing _call method
class BrokenChain(Chain):
    @property
    def input_keys(self):
        return ["text"]

    @property
    def output_keys(self):
        return ["result"]

# Right: implement _call and matching keys
class FixedChain(Chain):
    @property
    def input_keys(self):
        return ["text"]

    @property
    def output_keys(self):
        return ["result"]

    def _call(self, inputs: dict) -> dict:
        return {"result": inputs["text"].title()}
๐Ÿ“Š

Quick Reference

  • Subclass Chain: Create your own chain class by inheriting from Chain.
  • Define input_keys: List all expected input names.
  • Define output_keys: List all output names your chain returns.
  • Implement _call: Write the logic to process inputs and return outputs.
  • Invoke chain: Use chain.invoke({input_dict}) to run your chain.
โœ…

Key Takeaways

Create a custom chain by subclassing LangChain's Chain and implementing the _call method.
Always define input_keys and output_keys properties to specify expected inputs and outputs.
Ensure the keys in your input dictionary match input_keys and output dictionary keys match output_keys.
Use chain.invoke({inputs}) to run your custom chain and get outputs.
Avoid missing _call implementation or mismatched keys to prevent runtime errors.