Check finishReason when a Gemini response has empty text
When a Gemini API response has no text, read promptFeedback.blockReason, then candidates[0].finishReason, to find the cause. Includes a classifier.
If candidates is missing, or a candidate has no content.parts, there is no text for response.text to return. The reason is recorded in one of two places in the response JSON.
[1] Check promptFeedback first. If the prompt itself was blocked, blockReason is set and no candidate is generated.
{ "promptFeedback": { "blockReason": "PROHIBITED_CONTENT" } }Values the API reference defines for blockReason (GenerateContentResponse):
| Value | Reference description |
|---|---|
SAFETY | Prompt blocked for safety reasons; inspect safetyRatings for the category |
BLOCKLIST | Prompt contains terms from the terminology blocklist |
PROHIBITED_CONTENT | Prompt blocked due to prohibited content |
OTHER | Unknown reason |
IMAGE_SAFETY | Candidates blocked due to unsafe image generation content |
[2] If there is no blockReason, read candidates[0].finishReason, the reason the model stopped generating. When a response is cut off mid-generation, the candidate carries a finishReason and no content:
{ "candidates": [ { "finishReason": "SAFETY", "safetyRatings": [ { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "HIGH" } ] } ]}Values you are likely to meet when the text is empty (all from the FinishReason enum in the reference):
| Value | Reference description |
|---|---|
STOP | Natural stop point of the model or provided stop sequence |
MAX_TOKENS | The maximum number of tokens specified in the request was reached |
SAFETY | The response candidate content was flagged for safety reasons |
RECITATION | The response candidate content was flagged for recitation reasons |
LANGUAGE | The response candidate content was flagged for using an unsupported language |
BLOCKLIST | Generation stopped because the content contains forbidden terms |
PROHIBITED_CONTENT | Generation stopped for potentially containing prohibited content |
SPII | Generation stopped because the content potentially contains Sensitive Personally Identifiable Information |
MALFORMED_FUNCTION_CALL | The function call generated by the model is invalid |
OTHER | Unknown reason |
finishMessage is populated only when finishReason is set and gives more detail. Log it when present.
[3] Write a function that reads both places. This version works on the raw JSON (REST).
import json, sys def why_empty(resp): pf = resp.get("promptFeedback") or {} if pf.get("blockReason"): return f"prompt blocked: blockReason={pf['blockReason']}" cands = resp.get("candidates") or [] if not cands: return "no candidates and no blockReason" c = cands[0] parts = (c.get("content") or {}).get("parts") or [] text = "".join(p.get("text", "") for p in parts if not p.get("thought")) fr = c.get("finishReason") if text and fr == "STOP": return "ok" msg = c.get("finishMessage") out = f"finishReason={fr}" + (f" ({msg})" if msg else "") if text: out += f" with partial text ({len(text)} chars)" return out for path in sys.argv[1:]: print(f"{path}: {why_empty(json.load(open(path)))}")Parts marked thought are thought summaries, so the code leaves them out of the text.
[4] Run it on seven sample JSON files, no API key needed. File names describe the case.
$ python3 -I why_empty.py *.jsona-prompt-blocked.json: prompt blocked: blockReason=PROHIBITED_CONTENTb-response-safety.json: finishReason=SAFETYc-ok.json: okd-max-tokens.json: finishReason=MAX_TOKENSe-recitation.json: finishReason=RECITATIONf-partial.json: finishReason=MAX_TOKENS with partial text (14 chars)g-empty.json: no candidates and no blockReasonMAX_TOKENS has nothing to do with safety filters. Raise maxOutputTokens; the same value shows up when some text was already produced (f-partial). If both places are empty, as in g-empty, the response holds nothing to tell the causes apart. Resend the same request and see whether it repeats.
[5] If the cause is SAFETY, look at the safety settings. The current safety settings doc says categories you don't set use the default threshold, which is Off for Gemini 2.5 and later models. To set one yourself, pick per category from off, block_only_high, block_medium_and_above or block_low_and_above (Safety settings).
[6] Some blocks stay even with thresholds relaxed. The safety settings doc says built-in protections against core harms, such as content that endangers child safety, are always blocked and cannot be adjusted. For those, the only fix is to change the prompt content. The doc also says blocking is based on probability, not on severity of harm.