ai icon indicating copy to clipboard operation
ai copied to clipboard

pruneMessages with "before-last-message" or "before-last-n-message" not pruning anything

Open colecorrente opened this issue 1 year ago • 2 comments

Description

Here is an example test ive written

import { type ModelMessage, pruneMessages } from "ai";

const toolCallMessagesFixture: ModelMessage[] = [
  {
    role: "user",
    content: [
      {
        type: "text",
        text: "ask me a question",
      },
    ],
  },
  {
    role: "assistant",
    content: [
      {
        type: "text",
        text: "What can i help you with",
      },
      {
        type: "tool-call",
        toolCallId: "toolu_01P9s4havAQSjDmS4eWT1N2V",
        toolName: "AskUserQuestion",
        input: {
          question: "What would you like help with today?",
          options: ["Tool 1 Option 1", "Tool 1 Option 2", "Tool 1 Option 3"],
        },
      },
    ],
  },
  {
    role: "tool",
    content: [
      {
        type: "tool-result",
        toolCallId: "toolu_01P9s4havAQSjDmS4eWT1N2V",
        toolName: "AskUserQuestion",
        output: { type: "text", value: "Something else" },
      },
    ],
  },
  {
    role: "assistant",
    content: [
      {
        type: "tool-call",
        toolCallId: "toolu_01TMAuwWKLmBoQtx7K88dxsQ",
        toolName: "AskUserQuestion",
        input: {
          question: "Ok what else?",
          options: ["Tool 2 Option 1", "Tool 2 Option 2", "Tool 2 Option 3"],
        },
      },
    ],
  },
  {
    role: "tool",
    content: [
      {
        type: "tool-result",
        toolCallId: "toolu_01TMAuwWKLmBoQtx7K88dxsQ",
        toolName: "AskUserQuestion",
        output: {
          type: "text",
          value: "Other - I'll describe it",
        },
      },
    ],
  },
  {
    role: "assistant",
    content: [
      {
        type: "text",
        text: "What would you like to discuss or work on?",
      },
    ],
  },
  {
    role: "user",
    content: [
      {
        type: "text",
        text: "never mind. lets end this conversation",
      },
    ],
  },
  {
    role: "assistant",
    content: [
      {
        type: "text",
        text: "ok, have a nice day",
      },
    ],
  },
  {
    role: "user",
    content: [
      {
        type: "text",
        text: "thank you",
      },
    ],
  },
];

test("pruneMessages", () => {
  const pruned = pruneMessages({
    messages: toolCallMessagesFixture,
    toolCalls: "before-last-message",
  });
  expect(pruned).toMatchInlineSnapshot(`
[
  {
    "content": [
      {
        "text": "ask me a question",
        "type": "text",
      },
    ],
    "role": "user",
  },
  {
    "content": [
      {
        "text": "What can i help you with",
        "type": "text",
      },
    ],
    "role": "assistant",
  },
  {
    "content": [
      {
        "text": "What would you like to discuss or work on?",
        "type": "text",
      },
    ],
    "role": "assistant",
  },
  {
    "content": [
      {
        "text": "never mind. lets end this conversation",
        "type": "text",
      },
    ],
    "role": "user",
  },
  {
    "content": [
      {
        "text": "ok, have a nice day",
        "type": "text",
      },
    ],
    "role": "assistant",
  },
  {
    "content": [
      {
        "text": "thank you",
        "type": "text",
      },
    ],
    "role": "user",
  },
]
`);
});

This test fails. The tool calls are all before the last message, so i would expect them all to be pruned. Changing "before-last-message" to "all" exhibits the behavior i would expect, but of course that doesnt work in practice because i dont want all tool call trimmed always, only the ones before the last message. Same issue with using "before-last-2-messages" exists.

I this a bug, or am i using this wrong or have wrong expectations?

AI SDK Version

"ai": "5.0.98",

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

colecorrente avatar Nov 21 '25 00:11 colecorrente

I've just noticed this behaviour too

Phiph avatar Nov 23 '25 16:11 Phiph

Identify issue type

Identified as Bug
Confidence: High

Rationale

The issue reports a concrete failure of existing pruneMessages options to perform their expected pruning, supported by reproducible code and corroboration.

Evidence

  • With AI SDK 5.0.98, pruneMessages({ toolCalls: "before-last-message" }) leaves earlier tool calls and results unpruned in the provided reproducible test.
  • Using toolCalls: "all" produces the expected pruning, while "before-last-2-messages" reportedly has the same failure.
  • A second user confirmed observing the behavior.

lgrammel avatar Jul 22 '26 12:07 lgrammel