langchain.js
    Preparing search index...

    Variable convertResponsesMessageToAIMessageConst

    convertResponsesMessageToAIMessage: BaseDynamicToolInput<
        ResponsesCreateInvoke
        | ResponsesParseInvoke,
        BaseDynamicToolInput,
    > = ...

    Converts an OpenAI Responses API response to a LangChain AIMessage.

    This converter processes the output from OpenAI's Responses API (both create and parse methods) and transforms it into a LangChain AIMessage object with all relevant metadata, tool calls, and content.

    The response object from OpenAI's Responses API. Can be either:

    • ResponsesCreateInvoke: Result from responses.create()
    • ResponsesParseInvoke: Result from responses.parse()

    An AIMessage containing:

    • id: The message ID from the response output
    • content: Array of message content blocks (text, images, etc.)
    • tool_calls: Array of successfully parsed tool calls
    • invalid_tool_calls: Array of tool calls that failed to parse
    • usage_metadata: Token usage information converted to LangChain format
    • additional_kwargs: Extra data including:
      • refusal: Refusal text if the model refused to respond
      • reasoning: Reasoning output for reasoning models
      • tool_outputs: Results from built-in tools (web search, file search, etc.)
      • parsed: Parsed structured output when using json_schema format
      • Function call ID mappings for tracking
    • response_metadata: Metadata about the response including model, timestamps, status, etc.

    Error if the response contains an error object. The error message and code are extracted from the response.error field.

    const response = await client.responses.create({
    model: "gpt-4",
    input: [{ type: "message", content: "Hello" }]
    });
    const message = convertResponsesMessageToAIMessage(response);
    console.log(message.content); // Message content
    console.log(message.tool_calls); // Any tool calls made

    The converter handles multiple output item types:

    • message: Text and structured content from the model
    • function_call: Tool/function calls that need to be executed
    • reasoning: Reasoning traces from reasoning models (o1, o3, etc.)
    • custom_tool_call: Custom tool invocations
    • Built-in tool outputs: web_search, file_search, code_interpreter, etc.

    Tool calls are parsed and validated. Invalid tool calls (malformed JSON, etc.) are captured in the invalid_tool_calls array rather than throwing errors.