ai icon indicating copy to clipboard operation
ai copied to clipboard

appendResponseMessages function Type error

Open tsuzaki430 opened this issue 1 year ago • 5 comments

Description

Hi , thank you for using ai sdk, I feel good DX developing generative AI application.

I use appendResponseMessages function because of convert ResponseMessage to Message. When I tried upgrade ai version 4.1.5 to 4.1.25 , I met the error. I tried serveral versions. [email protected] - @ai4.1.11 works expectedly. But [email protected] - ai@latest(4.1.25) those message happens. PR https://github.com/vercel/ai/pull/4670 may depends about it.

it happens streamText onFinish happens error (not Linter error).

unhandledRejection:  TypeError: Cannot read properties of undefined (reading 'role') at onFinish 
 ⨯ unhandledRejection:  TypeError: Cannot read properties of undefined (reading 'role')
    at onFinish (src\app\api\aichat\general\route.ts:241:52)
  239 |             // responseにAIからの回答messages:Message[]が入っている
  240 |             // const uiMessages=convertToUIMessages(response.messages as Message[])
> 241 |             const uiMessages=appendResponseMessages({
      |                                                    ^
  242 |               messages:[] as Message[],
  243 |               responseMessages:response.messages,
  244 |             });

The parameter messages is empty array because I need the response only.

const uiMessages=appendResponseMessages({
  messages:[] as Message[],
  responseMessages:response.messages,
});

I hope this issue will be useful.

Code example

No response

AI provider

@ai-sdk/azure 1.1.9

Additional context

No response

tsuzaki430 avatar Feb 08 '25 02:02 tsuzaki430

@tsuzaki430 you need to provide the input messages to appendResponseMessages. We reconcile messages and it may be that the last message gets modified (no new message added).

lgrammel avatar Feb 08 '25 08:02 lgrammel

@lgrammel Thank you for response and I fixed my code and this issue has solved. I close this issue and thank you.

tsuzaki430 avatar Feb 08 '25 09:02 tsuzaki430

@tsuzaki430 how did you fix your issue? I'm running into the same issue today when trying to call appendResponseMessages

const messages = appendResponseMessages({
        messages: existingMessages,
        responseMessages: response.messages,
      });

I get the following error:

TypeError: Cannot read properties of undefined (reading 'role')
    at appendResponseMessages (/Users/chris/projects/bfdb/node_modules/ai/core/prompt/append-response-messages.ts:44:48)

chris-keyplay avatar Jul 23 '25 19:07 chris-keyplay

Hello ,@chris-keyplay It looks a difficult trouble. The variable 'existingMessages' has 'role' property? And I thinks it's type should be Message[] type.

import {
  type Message,
} from 'ai';

tsuzaki430 avatar Jul 24 '25 00:07 tsuzaki430

Identify issue type

Identified as None
Confidence: High

Rationale

This is a usage/support issue resolved by supplying the required input messages, not a confirmed failure of documented SDK behavior.

Evidence

  • The reporter passed an empty messages array to appendResponseMessages while supplying response messages.
  • A maintainer explained that the input messages are required because the function reconciles or modifies the last message.
  • The reporter corrected their code and confirmed the issue was solved.
  • A later similar report lacks enough details to establish an SDK defect.

lgrammel avatar Jul 22 '26 16:07 lgrammel