Converting LangChain Messages to API Format with convert_message_to_dict

Posted: Nov 10, 2024.

When working with chat models and APIs in LangChain, you often need to convert message objects into specific formats that APIs can understand. The convert_message_to_dict function helps bridge this gap by transforming LangChain message objects into API-compatible dictionaries.

What is convert_message_to_dict?

convert_message_to_dict is a utility function that takes a LangChain BaseMessage object and converts it into a dictionary format that can be used when making API calls. This is particularly useful when you need to transform LangChain's internal message representations into formats that external APIs expect.

Reference

ParameterTypeDescription
messageBaseMessageThe LangChain message object to convert
returndictA dictionary containing the message data in API format

How to Use convert_message_to_dict

Let's look at different ways to use this utility function.

Basic Message Conversion

Here's a simple example of converting a single message:

from langchain_core.messages import AIMessage
from langchain_community.adapters.openai import convert_message_to_dict

# Create a message
message = AIMessage(content="Hello, this is an AI response")

# Convert to dictionary format
message_dict = convert_message_to_dict(message)
print(message_dict)
# Output: {"role": "assistant", "content": "Hello, this is an AI response"}

Converting Messages for API Requests

When preparing messages for API requests, you might need to convert multiple messages and combine them with system messages:

from langchain_core.messages import AIMessage, SystemMessage
from langchain_community.adapters.openai import convert_message_to_dict

# Create messages
system_msg = SystemMessage(content="You are a helpful assistant")
ai_msg = AIMessage(content="I can help with that!")

# Convert messages and prepare for API
messages = [
    {"role": "system", "content": system_msg.content},
    convert_message_to_dict(ai_msg)
]

print(messages)
# Output: [
#   {"role": "system", "content": "You are a helpful assistant"},
#   {"role": "assistant", "content": "I can help with that!"}
# ]

Processing Multiple Messages

When you have a collection of messages, you can process them in batch:

from langchain_core.messages import AIMessage
from langchain_community.adapters.openai import convert_message_to_dict

# Create multiple messages
messages = [
    AIMessage(content="First response"),
    AIMessage(content="Second response"),
    AIMessage(content="Third response")
]

# Convert all messages to dictionaries
converted_messages = [convert_message_to_dict(msg) for msg in messages]

# Add system message and prepare final format
system_message = {"role": "system", "content": "Process multiple responses"}
final_messages = [[system_message, msg] for msg in converted_messages]

print(final_messages[0])
# Output: [
#   {"role": "system", "content": "Process multiple responses"},
#   {"role": "assistant", "content": "First response"}
# ]

This utility function is particularly useful when you're working with external APIs that expect messages in a specific format, or when you're preparing data for model fine-tuning. It handles the conversion of LangChain's message objects into standardized dictionary formats that can be easily serialized and sent to APIs.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs