02. Python grammar often appeared in LangGraph

TypedDict

dict Wow TypedDict Difference between TypedDict Why dict I'll explain if it is used instead.

  1. dict Wow TypedDict The main difference

    • Type 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.

  2. TypedDict end dict Reasons to use instead

    • Type stability

      • TypedDict Can provide a stricter type check to prevent potential bugs in advance.

    • Code readability

      • TypedDict Using it clearly defines the structure of the dictionary, which improves the readability of the code.

    • IDE support

      • TypedDict With IDE, autocomplete and type hints can be provided more accurately.

    • Documentation

      • TypedDict The 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)

  1. Provide additional information : Add metadata to type hints to provide more detailed information.

  2. Documentation : You can get a documentation effect by including additional explanations in the code itself.

  3. Validation : You can use it with a specific library (e.g. Pydantic) to perform data validation.

  4. Framework support : In some frameworks (e.g. LangGraph) Annotated Use 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

  1. Annotated Available in Python 3.9 and above.

  2. At runtime Annotated Since it is ignored, it does not affect the actual behavior.

  3. Type inspection tool or IDE Annotated You 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

  • right Message left If there is a message with the same ID, right Will be replaced by the message of.

  • Other cases right Message left Is added to.

parameter

  • left (Messages): Basic message list

  • right (Messages): message list to merge or single message

return value

  • Messages : right Messages left New message list merged with

Copy

Copy

If there is a Message with the same ID, it is replaced.

Copy

Copy

Last updated