Skip to content

BUG: AI_TypeValidationError when tool_calls[].index is not defined. #7238

@cdxker

Description

@cdxker

Description

Using SambaNova's Llama-4-Maverick-17B-128E-Instruct returns a tool call without the index parameter.

I believe that tool_calls[].index should be an optional parameter to handle support for more models.

Example code of error

const sambanova = createOpenAI({
    apiKey: env.ASSISTANT_SAMBANOVA_API_KEY,
    baseURL: 'https://api.sambanova.ai/v1',
    headers: HEADERS
})
const model = sambanova("Llama-4-Maverick-17B-128E-Instruct");
      
const stream = createDataStream({
  execute: (dataStream) => {
    const result = streamText({
      model,
      messages: combinedMessages,
      maxSteps: 3,
      onChunk: ({ chunk }) => {
       
      },
      onFinish: async (result) => {
        dataStream.writeData(
          JSON.stringify({
            type: 'finish',
            threadId: currentThreadId,
          })
        );
      },
      onError: async (error) => {
        getLogger().error(
          {
            message: error.error,
          },
          'Error while streaming message'
        );
      },
      tools: {
        search: tool({
          description: `Use hybrid search`,
          parameters: z.object({
            query: z.string().describe('A query to search the content with.'),
          }),
          execute: async ({ query }) => {
            return query;
          },
        }),
      },
    });
    result.mergeIntoDataStream(dataStream);
  },
});

The models raw output.

{
    "choices": [
        {
            "delta": {
                "content": null,
                "role": "assistant",
                "tool_calls": [
                    {
                        "function": {
                            "arguments": "{\"query\":\"getting started\"}",
                            "name": "search"
                        },
                        "id": "call_64d008280bf8423e9f",
                        "type": "function"
                    }
                ]
            },
            "finish_reason": "tool_calls",
            "index": 0,
            "logprobs": null
        }
    ],
    "created": 1752261507.3932047,
    "id": "ba06feb9-6791-449f-a4fb-d974748590f4",
    "model": "Llama-4-Maverick-17B-128E-Instruct",
    "object": "chat.completion.chunk",
    "system_fingerprint": "fastcoe"
}

This call returns an error in the onError callback.

{
      "name": "AI_TypeValidationError",
      "cause": {
        "issues": [
          {
            "code": "invalid_union",
            "unionErrors": [
              {
                "issues": [
                  {
                    "code": "invalid_type",
                    "expected": "number",
                    "received": "undefined",
                    "path": [
                      "choices",
                      0,
                      "delta",
                      "tool_calls",
                      0,
                      "index"
                    ],
                    "message": "Required"
                  },
                  {
                    "code": "invalid_type",
                    "expected": "number",
                    "received": "undefined",
                    "path": [
                      "choices",
                      0,
                      "delta",
                      "tool_calls",
                      1,
                      "index"
                    ],
                    "message": "Required"
                  },
                  {
                    "code": "invalid_type",
                    "expected": "number",
                    "received": "undefined",
                    "path": [
                      "choices",
                      0,
                      "delta",
                      "tool_calls",
                      2,
                      "index"
                    ],
                    "message": "Required"
                  }
                ],
                "name": "ZodError"
              },
              {
                "issues": [
                  {
                    "code": "invalid_type",
                    "expected": "object",
                    "received": "undefined",
                    "path": [
                      "error"
                    ],
                    "message": "Required"
                  }
                ],
                "name": "ZodError"
              }
            ],
            "path": [],
            "message": "Invalid input"
          }
        ],
        "name": "ZodError"
      },
      "value": {
        "choices": [
          {
            "delta": {
              "content": null,
              "role": "assistant",
              "tool_calls": [
                {
                  "function": {
                    "arguments": "{\"query\":\"getting started with docs\"}",
                    "name": "search"
                  },
                  "id": "call_40f7adedb11f4fe9a2",
                  "type": "function"
                },
                {
                  "function": {
                    "arguments": "{\"query\":\"docs guide\"}",
                    "name": "search"
                  },
                  "id": "call_34619b0385134fe185",
                  "type": "function"
                },
                {
                  "function": {
                    "arguments": "{\"query\":\"documentation guide\"}",
                    "name": "search"
                  },
                  "id": "call_19f41bcc1eea43aab3",
                  "type": "function"
                }
              ]
            },
            "finish_reason": "tool_calls",
            "index": 0,
            "logprobs": null
          }
        ],
        "created": 1752262213.163156,
        "id": "0f79c8fb-282e-4069-8c88-00860bc47e06",
        "model": "Llama-4-Maverick-17B-128E-Instruct",
        "object": "chat.completion.chunk",
        "system_fingerprint": "fastcoe"
      }
    }

AI SDK Version

  • ai: "^4.3.4"
  • @ai-sdk/openai: ^1.3.22

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy