Bird
0
0

How can you modify this LangServe class to handle both string and list inputs by returning their length? class LengthAPI: def __call__(self, input): return len(input)

hard📝 Application Q9 of 15
LangChain - Production Deployment
How can you modify this LangServe class to handle both string and list inputs by returning their length? class LengthAPI: def __call__(self, input): return len(input)
ANo change needed; len() works for both strings and lists
BAdd type check and convert list to string before len()
CRaise error if input is not string
DUse input.length instead of len(input)
Step-by-Step Solution
Solution:
  1. Step 1: Recall len() behavior

    len() works on strings and lists, returning their length.
  2. Step 2: Confirm no modification needed

    Since len() supports both types, the class works as is.
  3. Final Answer:

    No change needed; len() works for both strings and lists -> Option A
  4. Quick Check:

    len() supports strings and lists [OK]
Quick Trick: len() works on many types including strings and lists [OK]
Common Mistakes:
MISTAKES
  • Thinking len() only works on strings
  • Trying to convert list to string unnecessarily
  • Using input.length which is invalid in Python

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes