ai-sdk-provider icon indicating copy to clipboard operation
ai-sdk-provider copied to clipboard

finish_reason of model response is null when used with tool call

Open fwang2002 opened this issue 3 months ago • 0 comments

I use the model google gemini flash 2.5, and prompt it to start a tool call for each request. but sometimes, it will return { finish_reason: "other" }(the ai-sdk response, the original value is null) and with no tool call.

I captured the original chunks returned from openrouter server, I am wondering whether there is some chunk lost in the process or it's a real model response.

Here is the good case and bad case of the last two chunks returned. The bad case has no tool call, though the text indicates it will, and the finish_reason is null.

  1. good case
{
  "success": true,
  "value": {
    "id": "gen-1756213145-OQL2hvhGYopRw4kKfXcx",
    "model": "google/gemini-2.5-flash",
    "provider": "Google",
    "choices": [
      {
        "delta": {
          "role": "assistant",
          "content": null,
          "tool_calls": [
            {
              "index": 0,
              "id": "tool_0_webSearch",
              "type": "function",
              "function": {
                "name": "webSearch",
                "arguments": "{\"query\":\"lastest news of AI\"}"
              }
            }
          ]
        },
        "logprobs": null,
        "finish_reason": "tool_calls",
        "index": 0
      }
    ]
  },
  "rawValue": {
    "id": "gen-1756213145-OQL2hvhGYopRw4kKfXcx",
    "provider": "Google",
    "model": "google/gemini-2.5-flash",
    "object": "chat.completion.chunk",
    "created": 1756213145,
    "choices": [
      {
        "index": 0,
        "delta": {
          "role": "assistant",
          "content": null,
          "tool_calls": [
            {
              "index": 0,
              "id": "tool_0_webSearch",
              "type": "function",
              "function": {
                "name": "webSearch",
                "arguments": "{\"query\":\"lastest news of AI\"}"
              }
            }
          ]
        },
        "finish_reason": "tool_calls",
        "native_finish_reason": "STOP",
        "logprobs": null
      }
    ]
  }
}

{
  "success": true,
  "value": {
    "id": "gen-1756213145-OQL2hvhGYopRw4kKfXcx",
    "model": "google/gemini-2.5-flash",
    "provider": "Google",
    "usage": {
      "prompt_tokens": 1339,
      "prompt_tokens_details": {
        "cached_tokens": 0
      },
      "completion_tokens": 157,
      "total_tokens": 1496
    },
    "choices": [
      {
        "delta": {
          "role": "assistant",
          "content": ""
        },
        "logprobs": null,
        "finish_reason": null,
        "index": 0
      }
    ]
  },
  "rawValue": {
    "id": "gen-1756213145-OQL2hvhGYopRw4kKfXcx",
    "provider": "Google",
    "model": "google/gemini-2.5-flash",
    "object": "chat.completion.chunk",
    "created": 1756213145,
    "choices": [
      {
        "index": 0,
        "delta": {
          "role": "assistant",
          "content": ""
        },
        "finish_reason": null,
        "native_finish_reason": null,
        "logprobs": null
      }
    ],
    "usage": {
      "prompt_tokens": 1339,
      "completion_tokens": 157,
      "total_tokens": 1496,
      "prompt_tokens_details": {
        "cached_tokens": 0
      }
    }
  }
}
  1. bad case
{
  "success": true,
  "value": {
    "id": "gen-1756213148-XMlNooUQJSAdMuHmDPFK",
    "model": "google/gemini-2.5-flash",
    "provider": "Google",
    "choices": [
      {
        "delta": {
          "role": "assistant",
          "content": "I will query recent news of AI\n",
          "reasoning": null,
          "reasoning_details": []
        },
        "logprobs": null,
        "finish_reason": null,
        "index": 0
      }
    ]
  },
  "rawValue": {
    "id": "gen-1756213148-XMlNooUQJSAdMuHmDPFK",
    "provider": "Google",
    "model": "google/gemini-2.5-flash",
    "object": "chat.completion.chunk",
    "created": 1756213148,
    "choices": [
      {
        "index": 0,
        "delta": {
          "role": "assistant",
          "content": "I will query recent news of AI\n",
          "reasoning": null,
          "reasoning_details": []
        },
        "finish_reason": null,
        "native_finish_reason": null,
        "logprobs": null
      }
    ]
  }
}

{
  "success": true,
  "value": {
    "id": "gen-1756213148-XMlNooUQJSAdMuHmDPFK",
    "model": "google/gemini-2.5-flash",
    "provider": "Google",
    "usage": {
      "prompt_tokens": 2590,
      "prompt_tokens_details": {
        "cached_tokens": 0
      },
      "completion_tokens": 87,
      "total_tokens": 2677
    },
    "choices": [
      {
        "delta": {
          "role": "assistant",
          "content": ""
        },
        "logprobs": null,
        "finish_reason": null,
        "index": 0
      }
    ]
  },
  "rawValue": {
    "id": "gen-1756213148-XMlNooUQJSAdMuHmDPFK",
    "provider": "Google",
    "model": "google/gemini-2.5-flash",
    "object": "chat.completion.chunk",
    "created": 1756213148,
    "choices": [
      {
        "index": 0,
        "delta": {
          "role": "assistant",
          "content": ""
        },
        "finish_reason": null,
        "native_finish_reason": null,
        "logprobs": null
      }
    ],
    "usage": {
      "prompt_tokens": 2590,
      "completion_tokens": 87,
      "total_tokens": 2677,
      "prompt_tokens_details": {
        "cached_tokens": 0
      }
    }
  }
}

fwang2002 avatar Aug 28 '25 08:08 fwang2002