google / generative-ai-docs

Documentation for Google's Gen AI site - including the Gemini API and Gemma
https://ai.google.dev
Apache License 2.0
1.62k stars 569 forks source link

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

Open jelni opened 8 months ago

jelni commented 8 months ago

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

xslingcn commented 7 months ago

I have the same issue. Any updates?

jelni commented 6 months ago

@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)

zycheiheihei commented 6 months ago

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

DillionApple commented 3 months ago

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