ConstThe converter performs the following transformations:
Role-specific behavior:
The LangChain BaseMessage to convert. Must have contentBlocks property containing an array of standard content blocks (text, image, audio, file, etc.)
Optional model name. Used to determine if special role mapping is needed (e.g., "system" -> "developer" for reasoning models like o1)
An OpenAI ChatCompletionMessageParam object formatted for the Chat Completions API. The structure varies by role:
{ role, content: TextBlock[] }{ role: "tool", tool_call_id, content: TextBlock[] }{ role: "function", name, content: string }{ role: "user", content: Array<TextPart | ImagePart | AudioPart | FilePart> }Simple text message:
const message = new HumanMessage({
content: [{ type: "text", text: "Hello!" }]
});
const param = convertStandardContentMessageToCompletionsMessage({ message });
// Returns: { role: "user", content: [{ type: "text", text: "Hello!" }] }
Multi-modal user message with image:
const message = new HumanMessage({
content: [
{ type: "text", text: "What's in this image?" },
{ type: "image", url: "https://example.com/image.jpg" }
]
});
const param = convertStandardContentMessageToCompletionsMessage({ message });
// Returns: {
// role: "user",
// content: [
// { type: "text", text: "What's in this image?" },
// { type: "image_url", image_url: { url: "https://example.com/image.jpg" } }
// ]
// }
Converts a LangChain BaseMessage with standard content blocks to an OpenAI Chat Completions API message parameter.
This converter transforms LangChain's standardized message format (using contentBlocks) into the format expected by OpenAI's Chat Completions API. It handles role mapping, content filtering, and multi-modal content conversion for various message types.