0
0
LangChainframework~20 mins

PydanticOutputParser for typed objects in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PydanticOutputParser Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this PydanticOutputParser return?
Given this Pydantic model and parser setup, what is the output of parser.parse(text) when text = '{"name": "Alice", "age": 30}'?
LangChain
from pydantic import BaseModel
from langchain.output_parsers import PydanticOutputParser

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

parser = PydanticOutputParser(pydantic_object=Person)

text = '{"name": "Alice", "age": 30}'
result = parser.parse(text)
APerson(name='Alice', age=30)
B{'name': 'Alice', 'age': 30}
CRaises a ValidationError due to missing fields
DReturns the original string unchanged
Attempts:
2 left
💡 Hint
Think about what PydanticOutputParser does with JSON strings matching the model.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly initializes PydanticOutputParser?
Select the code snippet that correctly creates a PydanticOutputParser for a Pydantic model Item.
LangChain
from pydantic import BaseModel
from langchain.output_parsers import PydanticOutputParser

class Item(BaseModel):
    id: int
    description: str
Aparser = PydanticOutputParser(pydantic_object=Item())
Bparser = PydanticOutputParser(model=Item)
Cparser = PydanticOutputParser(pydantic_model=Item)
Dparser = PydanticOutputParser(pydantic_object=Item)
Attempts:
2 left
💡 Hint
Check the parameter name and whether you pass the class or an instance.
🔧 Debug
advanced
2:00remaining
Why does this parsing raise a ValidationError?
Given the model and parser below, why does parsing text = '{"id": "abc", "description": "Test"}' raise a ValidationError?
LangChain
from pydantic import BaseModel
from langchain.output_parsers import PydanticOutputParser

class Item(BaseModel):
    id: int
    description: str

parser = PydanticOutputParser(pydantic_object=Item)

text = '{"id": "abc", "description": "Test"}'
result = parser.parse(text)
ABecause 'id' is a string but should be an int
BBecause 'description' is missing
CBecause the JSON string is malformed
DBecause PydanticOutputParser cannot parse strings
Attempts:
2 left
💡 Hint
Check the type of the 'id' field in the JSON versus the model.
state_output
advanced
2:00remaining
What is the value of result after parsing nested models?
Consider these nested Pydantic models and the parser. What is the value of result after parsing the given JSON string?
LangChain
from pydantic import BaseModel
from langchain.output_parsers import PydanticOutputParser

class Address(BaseModel):
    city: str
    zip_code: str

class User(BaseModel):
    username: str
    address: Address

parser = PydanticOutputParser(pydantic_object=User)

json_text = '{"username": "bob123", "address": {"city": "NYC", "zip_code": "10001"}}'
result = parser.parse(json_text)
ARaises a ValidationError due to nested model
B{'username': 'bob123', 'address': {'city': 'NYC', 'zip_code': '10001'}}
CUser(username='bob123', address=Address(city='NYC', zip_code='10001'))
DUser(username='bob123', address={'city': 'NYC', 'zip_code': '10001'})
Attempts:
2 left
💡 Hint
PydanticOutputParser parses nested JSON into nested Pydantic models.
🧠 Conceptual
expert
2:00remaining
Which statement about PydanticOutputParser behavior is true?
Select the true statement about how PydanticOutputParser handles input and output.
AIt only returns raw JSON strings without validation or conversion.
BIt converts JSON strings into instances of the specified Pydantic model, validating types and nested structures.
CIt automatically converts any input string into a dictionary without errors.
DIt requires manual conversion of JSON strings before parsing.
Attempts:
2 left
💡 Hint
Think about what PydanticOutputParser adds beyond just returning strings.