02. Python grammar often appeared in LangGraph
TypedDict
dict Wow TypedDict Difference between TypedDict Why dict I'll explain if it is used instead.
dictWowTypedDictThe main differenceType inspection
dict: No type check at runtime.TypedDict: Provides static type checking. This means that when writing code, an IDE or type checker can catch the error in advance.
Type of key and value
dict: Normally specify the type of key and value (eg Dict[str, str]).TypedDict: You can specify specific types for each key.
flexibility
dict: You can add or remove keys at runtime.TypedDict: You must follow the defined structure. Additional keys cause type errors.
TypedDictenddictReasons to use insteadType stability
TypedDictCan provide a stricter type check to prevent potential bugs in advance.
Code readability
TypedDictUsing it clearly defines the structure of the dictionary, which improves the readability of the code.
IDE support
TypedDictWith IDE, autocomplete and type hints can be provided more accurately.
Documentation
TypedDictThe code itself acts as a document, clearly showing the structure of the dictionary.
Copy
Copy
However, the true value of TypedDict is revealed when using static type checkers.
For example, if you use a static type checker like mypy, or if you enable type check functions in IDEs like PyCharm, VS Code, etc., it will error these type mismatches and adding undefined keys. The static type checker allows you to see the following error messages.
Annotated
This grammar allows you to add metadata to type hints. Annotated Main reasons for using
Provide additional information (type hints) / documentation
Additional information can be included in the type hint. This provides more context for people or tools reading code.
You can include additional descriptions of the code directly in the type hint.
name: Annotated[str, "name"]
age: Annotated[int, "age"]
Annotated Is a special type hint provided by Python's typing module that allows you to add metadata to existing types.
Annotated Provides the ability to include additional information in the type hint. This allows you to increase the readability of your code and provide more detailed type information.
Annotated main function (reason for use)
Provide additional information : Add metadata to type hints to provide more detailed information.
Documentation : You can get a documentation effect by including additional explanations in the code itself.
Validation : You can use it with a specific library (e.g. Pydantic) to perform data validation.
Framework support : In some frameworks (e.g. LangGraph)
AnnotatedUse to define special behaviors.
Basic grammar
Type: Basic type (e.g.int,str,List[str]Etc)metadata1,metadata2, ...: Metadata you want to add
Copy
Example of use
Basic use
Copy
Used with Pydantic
Copy
Copy
Use in LangGraph (add_messages)
add_messages Is a function that adds a message to the list in LangGraph.
Copy
Copy
Reference
AnnotatedAvailable in Python 3.9 and above.At runtime
AnnotatedSince it is ignored, it does not affect the actual behavior.Type inspection tool or IDE
AnnotatedYou need to support to see the effect.
add_messages
messages tall add_messages The reader function is annotated, which instructs LangGraph to add a new message to the existing list.
The state key without annotations is overwritten by each update to store the most recent values.
add_messages The function receives 2 factors (left, right) and acts in a way that merges left and right messages.
Main features
Merge two message listings.
It remains "append-only" by default.
If you have a message with the same ID, replace the existing message with a new message.
Motion method
rightMessageleftIf there is a message with the same ID,rightWill be replaced by the message of.Other cases
rightMessageleftIs added to.
parameter
left(Messages): Basic message listright(Messages): message list to merge or single message
return value
Messages:rightMessagesleftNew message list merged with
Copy
Copy
If there is a Message with the same ID, it is replaced.
Copy
Copy
Last updated