Bird
0
0

You want to parse a nested Pydantic model output using PydanticOutputParser. Given:

hard📝 component behavior Q8 of 15
LangChain - Output Parsers
You want to parse a nested Pydantic model output using PydanticOutputParser. Given:
class Address(BaseModel):
    city: str
    zip_code: str

class Person(BaseModel):
    name: str
    address: Address

parser = PydanticOutputParser(model=Person)
output = '{"name": "Eve", "address": {"city": "NY", "zip_code": "10001"}}'
result = parser.parse(output)
print(result.address.city)

What will be printed?
ANY
B{'city': 'NY', 'zip_code': '10001'}
CAn error because nested models are not supported
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested model parsing

    PydanticOutputParser supports nested models and parses nested JSON accordingly.
  2. Step 2: Access nested field value

    result.address.city accesses the city string 'NY' inside the nested Address model.
  3. Final Answer:

    NY -> Option A
  4. Quick Check:

    Nested models parse correctly and fields accessible [OK]
Quick Trick: Nested Pydantic models parse nested JSON objects correctly [OK]
Common Mistakes:
  • Thinking nested models cause errors
  • Expecting dict instead of nested model instance
  • Assuming nested fields are None

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes