Bird
0
0

Given this Pydantic model and parser:

medium📝 component behavior Q13 of 15
LangChain - Output Parsers
Given this Pydantic model and parser:
from pydantic import BaseModel

class User(BaseModel):
    name: str
    age: int

parser = PydanticOutputParser(pydantic_object=User)

text = '{"name": "Alice", "age": 30}'
result = parser.parse(text)

What will result contain?
AA dictionary with keys 'name' and 'age'
BAn error because parse expects a list
CA string containing the original text
DA User object with name='Alice' and age=30
Step-by-Step Solution
Solution:
  1. Step 1: Understand parser.parse behavior

    parser.parse converts the JSON string into a typed Pydantic model instance, here User.
  2. Step 2: Analyze the input text and output

    The text is a JSON string with correct keys and types matching User. So parse returns a User object with those values.
  3. Final Answer:

    A User object with name='Alice' and age=30 -> Option D
  4. Quick Check:

    parse returns typed model instance [OK]
Quick Trick: parse returns model instance, not dict or string [OK]
Common Mistakes:
  • Expecting a dict instead of model instance
  • Thinking parse returns raw text
  • Assuming parse needs a list input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes