Bird
0
0

You want to define a state schema that stores a user's id (int), name (string), and optional email (string or None). Which is the correct way to define this schema?

hard📝 Application Q8 of 15
LangChain - LangGraph for Stateful Agents
You want to define a state schema that stores a user's id (int), name (string), and optional email (string or None). Which is the correct way to define this schema?
Aclass UserState: id: int name: str email = None
Bclass UserState: id: int name: str email: Optional[str]
Cclass UserState: id: int name: str email: str = None
Dclass UserState: id: int name: str email: str | None = None
Step-by-Step Solution
Solution:
  1. Step 1: Use modern union type hint for optional field

    Python 3.12+ supports str | None to indicate optional string.
  2. Step 2: Assign default None to optional field

    Setting email: str | None = None correctly marks it optional with default None.
  3. Step 3: Check other options for correctness

    Using email: Optional[str] requires an import from typing. Assigning email = None without a type hint is incorrect. Declaring email: str = None mismatches the type hint with the None default.
  4. Final Answer:

    class UserState:\n id: int\n name: str\n email: str | None = None -> Option D
  5. Quick Check:

    Use union type and default None for optional fields [OK]
Quick Trick: Use 'type | None = None' for optional fields in Python 3.12+ [OK]
Common Mistakes:
MISTAKES
  • Forgetting to assign default None for optional fields
  • Using Optional without importing it
  • Assigning None without type hint

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes