LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Llms
  • Profile
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangChain
UniversalHubNodeLoadSerializableEncoder BackedFile SystemIn Memory
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsLlmsProfileLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingHashJson PatchJson SchemaMathSsrfStreamTestingTiktokenTypesVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScript@langchain/coremessagesMessageStructure
Interfaceā—Since v1.0

MessageStructure

Core interface that defines the structure of messages.

Copy
interface MessageStructure

Example

Copy
// Basic message structure with just content blocks
interface SimpleMessageStructure extends MessageStructure {
  content: {
    human: ContentBlock.Text;
    // allows for text + reasoning blocks in ai messages
    ai: ContentBlock.Text | ContentBlock.Reasoning;
  }
}

// Message structure with tools and properties
interface AdvancedMessageStructure extends MessageStructure {
  tools: {
    calculator: MessageToolDefinition<
      { operation: string; numbers: number[] },
      number
    >;
  };
  content: {
    // allows for text + image blocks in human messages
    human: ContentBlock.Text | ContentBlock.Multimodal.Image;
    // only allows for text blocks in ai messages
    ai: ContentBlock.Text;
  };
  properties: {
    // pins properties to ai messages
    ai: {
      response_metadata: {
        confidence: number;
        model: string;
      };
    };
  }
}

// Using with $MergeMessageStructure to combine structures
// The resulting type when passed into BaseMessage will have a calculator tool,
// allow for text + image blocks in human messages,
// and text + reasoning blocks + additional arbitrary properties in ai messages.
type CombinedStructure = $MergeMessageStructure<
  SimpleMessageStructure,
  AdvancedMessageStructure
>;

// Using in a Message object
const message: Message<CombinedStructure> = {
  id: "msg-123",
  type: "human",
  content: [
    { type: "text", text: "Hello!" }
    { type: "image", mimeType: "image/jpeg", url: "https://example.com/image.jpg" }
    // this block will throw an error because it's not defined in the structure
    { type: "reasoning", reasoning: "The answer is 42" }
  ]
};

Properties

property
content: Partial<__type>

Array of content blocks that make up the message content

property
outputVersion: MessageOutputVersion
property
properties: Partial<__type>

Optional mapping of message types to arbitrary property objects. Allows attaching custom metadata or other information to specific message types.

property
tools: TTools

Optional set of tool definitions that can be used in messages. Each tool is defined with input/output types and can be referenced in tool messages.

View source on GitHub