Repository: vercel/ai

Issue #3775: Gemini: GenerateContentRequest.contents[...].parts: contents.parts must not be empty
Created: 2024-11-19T14:57:42Z
Description
⨯ Error [AI_APICallError]: * GenerateContentRequest.contents[2].parts: contents.parts must not be empty.


GenerateContentRequest.contents[5].parts: contents.parts must not be empty.


GenerateContentRequest.contents[8].parts: contents.parts must not be empty.


GenerateContentRequest.contents[11].parts: contents.parts must not be empty.


GenerateContentRequest.contents[14].parts: contents.parts must not be empty.
at  (/var/task/.next/server/app/(chat)/api/chat/route.js:36:4984)
at async em (/var/task/.next/server/app/(chat)/api/chat/route.js:36:2111)
at async ex.doStream (/var/task/.next/server/app/(chat)/api/chat/route.js:36:10963)
at async fn (/var/task/.next/server/app/(chat)/api/chat/route.js:44:45816)
at async (/var/task/.next/server/app/(chat)/api/chat/route.js:44:3458)
at async tc (/var/task/.next/server/app/(chat)/api/chat/route.js:44:1504)
at async s (/var/task/.next/server/app/(chat)/api/chat/route.js:44:44955)
at async fn (/var/task/.next/server/app/(chat)/api/chat/route.js:44:48173)
at async (/var/task/.next/server/app/(chat)/api/chat/route.js:44:3458) {
cause: undefined,
url: 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:streamGenerateContent?alt=sse',
requestBodyValues: [Object],
statusCode: 400,
responseHeaders: [Object],
responseBody: '{\n' +
'  "error": {\n' +
'    "code": 400,\n' +
'    "message": "* GenerateContentRequest.contents[2].parts: contents.parts must not be empty.\n* GenerateContentRequest.contents[5].parts: contents.parts must not be empty.\n* GenerateContentRequest.contents[8].parts: contents.parts must not be empty.\n* GenerateContentRequest.contents[11].parts: contents.parts must not be empty.\n* GenerateContentRequest.contents[14].parts: contents.parts must not be empty.\n",\n' +
'    "status": "INVALID_ARGUMENT"\n' +
'  }\n' +
'}\n',
isRetryable: false,
data: [Object]
}


Code example
No response
AI provider
@ai-sdk/google 0.0.23
Additional context
Idk whats wrong with this

Duplicate issue #4301: Bedrock Sonnet v2 and Vertex Gemini 2.0 flash: Cannot read properties of undefined (reading 'toolCalls')
Created: 2025-01-07T13:01:41Z
Description
When i try to use anthropic.claude-3-5-sonnet-20241022-v2:0 on amazon-bedrock, and gemini-2.0-flash-exp on Vertex im getting this error:
[500] Cannot read properties of undefined (reading 'toolCalls')
Code example
  const response = await streamText({
    model,
    system: 'You are a helpful assistant. You always try to help the user with their questions.',
    messages: messages as CoreMessage[],
    onFinish: (result) => {
      console.log(result)
    },
  })
AI provider
"ai": "4.0.27",  "@ai-sdk/amazon-bedrock": "1.0.6",     "@ai-sdk/anthropic": "1.0.6",     "@ai-sdk/google": "1.0.12",     "@ai-sdk/google-vertex": "2.0.13",     "@ai-sdk/openai": "1.0.13",     "@ai-sdk/vue": "1.0.7",
Additional context
No response

Closing comment (2025-01-08T20:43:13Z): I'm releasing 4.0.30 with a fix. The error masks another error that will be available in fullStream or if you enable error message forwarding / log it: https://sdk.vercel.ai/docs/ai-sdk-ui/chatbot#error-messages

Thanks! For me the issue was Full stream error: [Error [AI_APICallError]: * GenerateContentRequest.contents[2].parts: contents.parts must not be empty. similar to #3775. The first message worked fine, but when I sent a second message I saw the error because of empty tool messages from https://github.com/vercel/ai-chatbot/blob/c5a57fd7a84535db2e1262fc059a06f73cc85689/lib/utils.ts#L99
messages  [
  { role: 'user', content: 'test', toolInvocations: [] },
  {
    role: 'assistant',
    content: "Understood. I'm ready to help with your questions.\n",
    toolInvocations: []
  },
  { role: 'user', content: 'test' }
]
coreMessages [
  { role: 'user', content: 'test' },
  { role: 'assistant', content: [ [Object] ] },
  { role: 'tool', content: [] },
  { role: 'user', content: 'test' }
]
Gemini contents:
"contents": [
      {
        "role": "user",
        "parts": [
          {
            "text": "test"
          }
        ]
      },
      {
        "role": "model",
        "parts": [
          {
            "text": "Understood. I'm ready to help with your questions.\n"
          }
        ]
      },
      {
        "role": "user",
        "parts": []    <----- causing the error
      },
      {
        "role": "user",
        "parts": [
          {
            "text": "test"
          }
        ]
      }
    ],