Bird
0
0

Given this LangChain code snippet:

medium📝 component behavior Q13 of 15
LangChain - Output Parsers
Given this LangChain code snippet:
output_parser = StructuredOutputParser.from_format('nameage')
response = '{"name": "Alice", "age": 30}'
parsed = output_parser.parse(response)
print(parsed['age'])

What will be printed?
AAlice
B30
CError: parse method not found
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Understand the parsing process

    The parser converts the JSON string into a dictionary with keys 'name' and 'age'.
  2. Step 2: Access the 'age' key from the parsed dictionary

    parsed['age'] retrieves the value 30 from the dictionary.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    parsed['age'] = 30 [OK]
Quick Trick: Parsed JSON keys return their values directly [OK]
Common Mistakes:
  • Confusing keys and values
  • Expecting parse method to fail
  • Assuming output is string, not dict

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes