LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Graphs
    • Functional API
    • Pregel
    • Checkpointing
    • Storage
    • Caching
    • Types
    • Runtime
    • Config
    • Errors
    • Constants
    • Channels
    • Agents
    LangGraph Checkpoint
    LangGraph Store
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewGraphsFunctional APIPregelCheckpointingStorageCachingTypesRuntimeConfigErrorsConstantsChannelsAgents
    LangGraph Checkpoint
    LangGraph Store
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    Language
    Theme
    Pythonlanggraphpregel_messagesensure_message_ids
    Functionā—Since v1.2

    ensure_message_ids

    Copy
    ensure_message_ids(
        value: Any,
    ) -> None
    View source on GitHub

    Coerce message-like write values to typed BaseMessages with stable IDs.

    Called in put_writes() before DeltaChannel writes are submitted to the checkpointer. Without this the checkpoint may store raw dicts or id=None BaseMessages; every get_state() replay then produces a different UUID and the same message appears with a different ID in each LangSmith trace.

    Handles three input shapes:

    • BaseMessage objects: assign a UUID if id is None.
    • Dicts with a known "role" (OpenAI-style) or "type" (LangChain format) at the root level: stamp "id" into the dict in-place. The reducer's convert_to_messages call will forward the id to the resulting BaseMessage.
    • Lists of the above: apply the same logic to each element, replacing dict items with coerced BaseMessages so the shared list reference seen by checkpoint_pending_writes and the background thread both get typed messages.

    Mutating synchronously here (before the background thread is submitted) is safe: the serialised bytes always reflect the post-coercion state.