generative-ai-docs icon indicating copy to clipboard operation
generative-ai-docs copied to clipboard

Missing `content` field when `finishReason` is other than `STOP`

Open jelni opened this issue 1 year ago • 4 comments

Description of the bug:

when the Candidate.finishReason response field is not set to STOP, the Candidate.content is missing.

example reproduction

curl --request POST \
  --header "Content-Type: application/json" \
  --data '{"contents":[{"parts":[{"text":"hi!"}]}],"generationConfig":{"maxOutputTokens":1}}' \
  https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=YOUR_API_KEY

Actual vs expected behavior:

the response to this request is currently

{
  "candidates": [
    {
      "finishReason": "MAX_TOKENS",
      "index": 0,
      "safetyRatings": [...]
    }
  ],
  "promptFeedback": {
    "safetyRatings": [...]
  }
}

the candidates[0].content field is expected to contain the first generated token (as maxOutputTokens was set to 1), but is missing

Any other information you'd like to share?

this started happening between 2024-01-12 and 2024-01-14 on the v1beta API version

jelni avatar Jan 22 '24 23:01 jelni

I have the same issue. Any updates?

xslingcn avatar Mar 08 '24 14:03 xslingcn

@xslingcn you could use the streamGenerateContent endpoint and concatenate all returned parts (excluding the last one, which seems to be affected by the issue i described)

jelni avatar Mar 25 '24 19:03 jelni

I also have the same issue. This makes the query from API very unstable, frequently resulting in null response. :(

zycheiheihei avatar Apr 07 '24 11:04 zycheiheihei

same here. My response is: { "candidates": [ { "finishReason": "OTHER", "index": 0 } ], "usageMetadata": { "promptTokenCount": 171, "totalTokenCount": 171 } }

DillionApple avatar Jun 28 '24 07:06 DillionApple