0
0
LangChainframework~10 mins

RunnablePassthrough and RunnableLambda in LangChain - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a RunnablePassthrough that returns the input as output.

LangChain
from langchain_core.runnables import RunnablePassthrough

passthrough = RunnablePassthrough()
result = passthrough.invoke([1])
print(result)
Drag options to blanks, or click blank then click option'
A'Hello World'
B123
CNone
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Passing None or a number instead of a string.
Expecting RunnablePassthrough to modify the input.
2fill in blank
medium

Complete the code to create a RunnableLambda that doubles the input number.

LangChain
from langchain_core.runnables import RunnableLambda

runnable = RunnableLambda(lambda x: x [1] 2)
result = runnable.invoke(5)
print(result)
Drag options to blanks, or click blank then click option'
A+
B-
C*
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds instead of doubling.
Using '//' does integer division, not doubling.
3fill in blank
hard

Fix the error in the RunnableLambda that should return the input string in uppercase.

LangChain
runnable = RunnableLambda(lambda x: x.[1]())
result = runnable.invoke('hello')
print(result)
Drag options to blanks, or click blank then click option'
Alower
Bupper
Ccapitalize
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lower()' converts to lowercase, not uppercase.
Using 'capitalize()' only changes the first letter.
4fill in blank
hard

Fill both blanks to create a RunnableLambda that returns the length of the input string if it is longer than 3 characters.

LangChain
runnable = RunnableLambda(lambda x: len(x) if len(x) [1] 3 else [2])
print(runnable.invoke('test'))
Drag options to blanks, or click blank then click option'
A>
B0
CNone
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' reverses the condition.
Returning 0 instead of None changes the meaning.
5fill in blank
hard

Fill all three blanks to create a RunnableLambda that returns a dictionary with the input string as key and its uppercase as value if the string length is at least 4.

LangChain
runnable = RunnableLambda(lambda x: [1] if len(x) [2] 4 else [3])
print(runnable.invoke('code'))
Drag options to blanks, or click blank then click option'
A{x: x.upper()}
B>=
C{}
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '>=' excludes strings of length 4.
Returning None instead of an empty dictionary changes the output type.