langchain.js
    Preparing search index...

    Variable convertStandardContentMessageToCompletionsMessageConst

    convertStandardContentMessageToCompletionsMessage: BaseDynamicToolInput<
        { message: BaseDynamicToolInput; model?: string },
        OpenAIClient.Chat.Completions.ChatCompletionMessageParam,
    > = ...

    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.

    The converter performs the following transformations:

    • Maps LangChain message roles to OpenAI API roles (user, assistant, system, developer, tool, function)
    • For reasoning models, automatically converts "system" role to "developer" role
    • Filters content blocks based on message role (most roles only include text blocks)
    • For user messages, converts multi-modal content blocks (images, audio, files) to OpenAI format
    • Preserves tool call IDs for tool messages and function names for function messages

    Role-specific behavior:

    • developer: Returns only text content blocks (used for reasoning models)
    • system: Returns only text content blocks
    • assistant: Returns only text content blocks
    • tool: Returns only text content blocks with tool_call_id preserved
    • function: Returns text content blocks joined as a single string with function name
    • user (default): Returns multi-modal content including text, images, audio, and files

    Conversion parameters

    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:

    • Developer/System/Assistant: { role, content: TextBlock[] }
    • Tool: { role: "tool", tool_call_id, content: TextBlock[] }
    • Function: { role: "function", name, content: string }
    • User: { 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" } }
    // ]
    // }