How to Use LCEL in Langchain: Syntax and Examples
In Langchain,
LCEL (LangChain Expression Language) is used to create dynamic expressions for chaining logic. You use LCEL by writing expressions as strings that Langchain evaluates to control flow or data manipulation within chains.Syntax
The basic syntax of LCEL involves writing expressions as strings that Langchain evaluates at runtime. These expressions can include variables, functions, and operators to dynamically control chain behavior.
Key parts:
- Variables: Refer to inputs or outputs using
{{variable_name}}. - Functions: Use built-in functions like
len(),lower(), etc. - Operators: Use standard operators like
==,!=,&&,||for logic.
python
expression = "{{input_text}}.lower() == 'hello'"Example
This example shows how to use LCEL in a Langchain conditional chain to check if input text equals 'hello' (case-insensitive) and respond accordingly.
python
from langchain.chains import ConditionalChain from langchain.schema import BaseOutputParser # Define a simple conditional chain using LCEL expression condition = "{{input_text}}.lower() == 'hello'" class SimpleOutputParser(BaseOutputParser): def parse(self, text: str) -> str: return text.strip() chain = ConditionalChain( condition=condition, true_chain=lambda inputs: {'response': 'Hi there!'}, false_chain=lambda inputs: {'response': 'Goodbye!'}, output_parser=SimpleOutputParser() ) # Test with input 'Hello' result_true = chain.invoke({'input_text': 'Hello'}) # Test with input 'Bye' result_false = chain.invoke({'input_text': 'Bye'}) print(result_true) # {'response': 'Hi there!'} print(result_false) # {'response': 'Goodbye!'}
Output
{'response': 'Hi there!'}
{'response': 'Goodbye!'}
Common Pitfalls
Common mistakes when using LCEL include:
- Not using double curly braces
{{}}around variables, so expressions don't evaluate correctly. - Using unsupported functions or syntax not recognized by Langchain's LCEL evaluator.
- Forgetting to handle case sensitivity when comparing strings.
- Not returning the expected data type from expressions, causing chain errors.
Always test expressions independently before integrating into chains.
python
wrong_expression = "input_text.lower() == 'hello'" # Missing {{}} around variable correct_expression = "{{input_text}}.lower() == 'hello'"
Quick Reference
| LCEL Feature | Description | Example |
|---|---|---|
| Variable | Use input/output variables with double curly braces | {{input_text}} |
| Function | Call built-in functions like lower(), len() | {{input_text}}.lower() |
| Operator | Use logical and comparison operators | {{input_text}} == 'test' |
| Conditional | Combine expressions with &&, || | {{a}} == 1 && {{b}} == 2 |
Key Takeaways
Use double curly braces {{}} to reference variables in LCEL expressions.
LCEL expressions support functions and logical operators for dynamic chain control.
Test LCEL expressions separately to avoid syntax and evaluation errors.
LCEL helps make Langchain chains flexible by evaluating expressions at runtime.
Avoid unsupported syntax and always handle string case sensitivity in comparisons.