langchain.js
    Preparing search index...

    Variable convertCompletionsMessageToBaseMessageConst

    convertCompletionsMessageToBaseMessage: BaseDynamicToolInput<
        {
            includeRawResponse?: boolean;
            message: OpenAIClient.Chat.Completions.ChatCompletionMessage;
            rawResponse: OpenAIClient.Chat.Completions.ChatCompletion;
        },
        BaseDynamicToolInput,
    > = ...

    Converts an OpenAI Chat Completions API message to a LangChain BaseMessage.

    This converter transforms messages from OpenAI's Chat Completions API format into LangChain's internal message representation, handling various message types and preserving metadata, tool calls, and other relevant information.

    The converter handles the following message roles:

    • assistant: Converted to AIMessage with support for tool calls, function calls, audio content, and multi-modal outputs
    • Other roles: Converted to generic ChatMessage

    For assistant messages, the converter:

    • Parses and validates tool calls, separating valid and invalid calls
    • Preserves function call information in additional_kwargs
    • Includes usage statistics and system fingerprint in response_metadata
    • Handles multi-modal content (text, images, audio)
    • Optionally includes the raw API response for debugging

    Conversion parameters

    The OpenAI chat completion message to convert

    The complete raw response from OpenAI's API, used to extract metadata like model name, usage statistics, and system fingerprint

    If true, includes the raw OpenAI response in the message's additional_kwargs under the __raw_response key. Useful for debugging or accessing provider-specific fields. Defaults to false.

    A LangChain BaseMessage instance:

    • AIMessage for assistant messages with tool calls, metadata, and content
    • ChatMessage for all other message types
    const baseMessage = convertCompletionsMessageToBaseMessage({
    message: {
    role: "assistant",
    content: "Hello! How can I help you?",
    tool_calls: [
    {
    id: "call_123",
    type: "function",
    function: { name: "get_weather", arguments: '{"location":"NYC"}' }
    }
    ]
    },
    rawResponse: completionResponse,
    includeRawResponse: true
    });
    // Returns an AIMessage with parsed tool calls and metadata

    If tool call parsing fails, the invalid tool call is captured in the invalid_tool_calls array rather than throwing an error