You’re absolutely right to question this! Let’s break it down clearly.
RunnableSequence executes left to right, meaning:
- The first
RunnableLambda gets the initial input.
- Its output is passed as input to the next
RunnableLambda.
- This continues until the final result.
However, the issue arises due to how the functions are structured.
In the original example:
return f"Step 1 processed: {input}"
return f"Step 2 processed: {input}"
return f"Step 3 processed: {input}"
Each function prepends its message to the previous output. Since function execution is left to right, but string concatenation happens as “new text + previous result”, the final output appears reversed.
If you want step1 → step2 → step3 to execute in that order and reflect properly in the output, modify the functions to append instead of prepend:
from langchain.schema.runnable import RunnableLambda, RunnableSequence
# Define processing steps that append instead of prepend
return f"{input} -> Step 1 processed"
return f"{input} -> Step 2 processed"
return f"{input} -> Step 3 processed"
chain = RunnableSequence(*runnables)
output = chain.invoke("Start")
print(output) # Expected: "Start -> Step 1 processed -> Step 2 processed -> Step 3 processed"
- Each step appends to the existing string instead of prepending.
- Since execution is left to right, the final result maintains the correct order.
If you want a structured output that avoids string manipulation issues:
return input + ["Step 1 processed"]
return input + ["Step 2 processed"]
return input + ["Step 3 processed"]
RunnableLambda(lambda x: step1(x) if isinstance(x, list) else step1([x])),
chain = RunnableSequence(*runnables)
output = chain.invoke([])
print(output) # ['Step 1 processed', 'Step 2 processed', 'Step 3 processed']
✅ RunnableSequence executes left to right.
✅ If functions prepend their output, it appears reversed.
✅ The correct approach is to append results to preserve execution order.
This should now behave exactly as you expect! 🚀 Let me know if you need further tweaks.