0
0
LangChainframework~10 mins

PydanticOutputParser for typed objects in LangChain - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the PydanticOutputParser from langchain.

LangChain
from langchain.output_parsers import [1]
Drag options to blanks, or click blank then click option'
APydanticOutputParser
BJsonOutputParser
CRegexParser
DSimpleOutputParser
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a parser that does not handle Pydantic models.
Misspelling the class name.
2fill in blank
medium

Complete the code to define a Pydantic model named User with a name field of type string.

LangChain
from pydantic import BaseModel

class User([1]):
    name: str
Drag options to blanks, or click blank then click option'
APydanticModel
BModel
CBaseModel
DDataModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong base class name.
Forgetting to inherit from any class.
3fill in blank
hard

Fix the error in creating a PydanticOutputParser for the User model.

LangChain
parser = PydanticOutputParser([1]=User)
Drag options to blanks, or click blank then click option'
Apydantic_object
Bschema
Ctype
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'model', 'schema' or 'type' instead of 'pydantic_object'.
Passing an instance instead of the class.
4fill in blank
hard

Fill both blanks to parse a string output into a User object using the parser.

LangChain
output_str = '{"name": "Alice"}'
user = parser.[1](output_str)
print(user.[2])
Drag options to blanks, or click blank then click option'
Aparse
Bname
Cparse_raw
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using parse_raw instead of parse on the parser.
Trying to access a non-existent attribute.
5fill in blank
hard

Fill all three blanks to create a PydanticOutputParser, parse output, and print the user's name.

LangChain
from langchain.output_parsers import [1]

class User(BaseModel):
    name: str

parser = [2](pydantic_object=User)
result = parser.[3]('{"name": "Bob"}')
print(result.name)
Drag options to blanks, or click blank then click option'
APydanticOutputParser
BJsonOutputParser
Cparse
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parser class.
Calling the wrong method to parse the string.