new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

LangChain · LangGraph · all subjects

state

14 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

MessagesState for storing chat messages

MessagesState is a built-in state type in Python that stores a messages list of chat messages. It is used to maintain conversation history in LangGraph graphs.

MessagesAnnotation for storing chat messages in JavaScript

MessagesAnnotation is a built-in state type in JavaScript that stores a messages list of chat messages. It is used to maintain conversation history in LangGraph graphs.

Pattern: Add new state fields as NotRequired or Optional with None default

Add new state fields as NotRequired (or Optional[...] = None) so old checkpoints still validate. This allows existing checkpoints to load without satisfying the new field.

Pattern: Treat state field removals as deprecations

Keep deprecated state fields defined on the state for at least one drain cycle, even if no node reads it, so existing checkpoints continue to load. This prevents old checkpoints from failing validation when they contain the removed field.

Graph API for state management across components

The Graph API's explicit state management is beneficial when you need to share and coordinate state between different parts of your workflow. Multiple nodes can access and modify shared state through a TypedDict or StateSchema that defines all state fields.

Functional API function-scoped state

In the Functional API, state is naturally scoped to individual functions and passed between functions as needed, rather than being managed in a shared state schema. This is beneficial when state doesn't need to be broadly shared across many components.

StateGraph structure for multi-step workflows

A StateGraph defines workflow steps by creating a state class that inherits from MessagesState or uses Annotation.Root, adding named nodes with add_node or addNode, and connecting them with add_edge or addEdge. State keys correspond to workflow outputs, allowing frontend UI to render each step's results in dedicated regions.

Python StateGraph example with multi-step workflow

Example Python code showing StateGraph with a custom State class inheriting from MessagesState with fields for classification, research, analysis, and synthesis. Nodes are added with graph.add_node() and connected with graph.add_edge(), using START and END special node identifiers. The graph is compiled with graph.compile().

TypeScript StateGraph example with Annotation-based state

Example TypeScript code showing StateGraph with state defined using Annotation.Root spread with MessagesAnnotation.spec and additional string annotations for classification, research, analysis, and synthesis. Nodes are added with .addNode() and connected with .addEdge() using a fluent API. START and END are imported and used for edge connection endpoints.

State should store raw data, not formatted text

Store raw data in state so different nodes can format the same data differently for their needs. Format prompts inside nodes when needed. This separation means you can change prompt templates without modifying state schema, debugging is clearer, and agents can evolve without breaking existing state.

EmailClassification state structure for classification results

Store classification results as single dictionary in state. Fields: intent (Literal["question", "bug", "billing", "feature", "complex"]), urgency (Literal["low", "medium", "high", "critical"]), topic (string), summary (string).

EmailAgentState structure with raw data types

Python TypedDict: email_content (str), sender_email (str), email_id (str), classification (EmailClassification | None), search_results (list[str] | None), customer_history (dict | None), draft_response (str | None), messages (list[str] | None). JavaScript StateSchema with zod: emailContent (string), senderEmail (string), emailId (string), classification (optional EmailClassificationSchema), searchResults (optional array of strings), customerHistory (optional record), responseText (optional string).

Formatting context from raw state data in prompts

In draft_response node: format raw search_results as bulleted list, format raw customer_history as key-value pairs. Build prompt dynamically with formatted context sections. Store only raw response content in state, not formatted text. This enables different nodes to format same data differently.

Search results stored as raw list in state, not formatted text

Store search_results as list of raw document chunks in state. Example: ["Reset password via Settings > Security > Change Password", "Password must be at least 12 characters"]. Format for display only when building prompts in draft_response node. This allows reuse and reformatting without state changes.

Give your agent this brain