Challenge - 5 Problems
PydanticOutputParser Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about what PydanticOutputParser does with JSON strings matching the model.
✗ Incorrect
PydanticOutputParser parses the JSON string into an instance of the Pydantic model, so the output is a Person object with the given attributes.
📝 Syntax
intermediate2: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
Attempts:
2 left
💡 Hint
Check the parameter name and whether you pass the class or an instance.
✗ Incorrect
The correct parameter is 'pydantic_object' and it expects the Pydantic model class, not an instance.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the type of the 'id' field in the JSON versus the model.
✗ Incorrect
The 'id' field is expected to be an int, but the JSON provides a string 'abc', causing a ValidationError.
❓ state_output
advanced2: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)
Attempts:
2 left
💡 Hint
PydanticOutputParser parses nested JSON into nested Pydantic models.
✗ Incorrect
The parser converts nested JSON objects into nested Pydantic model instances, so address is an Address instance inside User.
🧠 Conceptual
expert2:00remaining
Which statement about PydanticOutputParser behavior is true?
Select the true statement about how PydanticOutputParser handles input and output.
Attempts:
2 left
💡 Hint
Think about what PydanticOutputParser adds beyond just returning strings.
✗ Incorrect
PydanticOutputParser parses JSON strings into Pydantic model instances, validating types and nested data automatically.