TypedDict
— What is it?TypedDict
is a feature from Python's typing
module that allows you to define a dictionary with specific keys and types, just like a class. It's useful when you're working with structured dictionaries and want type checking and auto-completion in IDEs.
from typing import TypedDict
class Person(TypedDict):
name: str
age: int
p: Person = {"name": "Abhi", "age": 34} # ✅ Valid
So, in your example:
class AgentState(TypedDict):
messages: List[BaseMessage]
intermediate_steps: List[str]
It means:
AgentState
is a dictionary that must contain:
a key "messages"
with a list of BaseMessage
items.
a key "intermediate_steps"
with a list of strings.
BaseMessage
— What is it?BaseMessage
is an abstract base class from LangChain, specifically from langchain.schema
. It represents a message exchanged in a conversation, and can be a:
HumanMessage
(user input)
AIMessage
(LLM response)
SystemMessage
(instructions)
FunctionMessage
(tool call outputs)
from langchain.schema import HumanMessage
msg = HumanMessage(content="Hello!")
So, when you define:
messages: List[BaseMessage]
You're saying the messages
list can contain any combination of HumanMessage
, AIMessage
, etc., because they all inherit from BaseMessage
.
TypedDict
helps define and validate the AgentState structure.
BaseMessage
allows handling chat history in a structured way.
Term | Meaning |
---|---|
TypedDict |
Type-safe dictionary with predefined keys and types (like a schema for a dict). |
BaseMessage |
LangChain base class for messages like user input, AI replies, system messages, etc. |