Gemini Finish Reasons Need a Typed Delivery Policy
Gemini GenerateContent candidates expose finishReason to explain why token generation stopped. A production gateway should inspect every candidate, treat STOP as completed only after content validation, classify MAX_TOKENS and safety-related reasons separately, quarantine malformed function calls and unexpected tool calls, and fail closed on unknown enum values.

Gemini returns finishReason on each generated candidate to explain why token generation stopped. The field is output-only, and an empty value means generation has not stopped. A gateway that records only HTTP 200 loses the difference between a natural stop, a token limit, a safety stop, an invalid function call, and a future enum value the parser has never seen.
I want candidate delivery controlled by a typed policy after the response arrives. Transport success opens the inspection step, while the terminal metadata and content checks complete it.
TL;DR
- Inspect
finishReasonon every Gemini candidate before delivery or tool execution. - Treat
STOPas eligible for completion only after route-specific content validation. - Classify token limits, safety stops, tool-call failures, and unknown values as separate outcomes.
- Preserve the raw enum, candidate index, response ID, model version, and policy decision in the audit record.
Finish reason belongs to each candidate
Google AI Studio's GenerateContent API reference defines finishReason on a response candidate. It describes the value as the reason the model stopped generating tokens and says an empty value means the model has not stopped. Candidate content and finish metadata therefore need to be evaluated together.
Iterate over the candidates array by index. For each entry, capture content presence, part types, finishReason, finishMessage when present, safety ratings, citation or grounding metadata needed by the route, and token count fields exposed by the selected API version. Preserve the enclosing response correlation fields and model version.
Do not copy the first candidate's reason onto the whole response. Multiple candidates can carry different content and terminal metadata. If the application selects one candidate, record the selection rule and selected index.
The AI audit log schema provides the common identity, route, request, and policy fields around this candidate-level extension.
STOP still requires content validation
The API reference defines STOP as a natural model stop point or a supplied stop sequence. That reason makes the candidate eligible for normal route validation. It does not prove that the returned content satisfies the application's schema or business contract.
A text route can require a non-empty text part. Structured output needs to pass the declared schema. Function calls need a known name, validated arguments, and a matching tool policy. Citation-bearing research can proceed only with the evidence fields promised by its contract.
Keep model completion and application acceptance as separate states. Use fields such as provider_finish_reason: STOP and application_disposition: accepted or quarantined. This avoids teaching operators that every natural stop produced a usable result.
The per-route AI policies guide explains how the authenticated route selects those content requirements. Finish-reason handling should be another versioned rule in that policy.
Token limits create an incomplete result
The current enum defines MAX_TOKENS when the request's maximum token count is reached. Treat the candidate as incomplete unless the route contract explicitly accepts bounded partial text. Structured output and function-call arguments should usually enter quarantine because truncation can remove closing syntax or required fields.
Google also documents CONTINUATION for generation that stopped at a per-request token boundary and can continue using an opaque continuation token. The reference notes that the request's maximum output token setting is cumulative across continuation hops and must increase monotonically or be omitted for continuation.
Continuation is a new HTTP operation. Give it another gateway decision, deadline, attempt ID, and cumulative budget. Preserve the original candidate and continuation token under restricted access. Limit the number of hops so one accepted request cannot become an open-ended generation loop.
A white terminal card showing MAX_TOKENS in amber should remain amber after the partial text renders. Color-coding the presence of text as green hides the terminal reason engineers need.
Safety and policy reasons need their own branch
The GenerateContent enum includes SAFETY, RECITATION, BLOCKLIST, PROHIBITED_CONTENT, SPII, and image-specific safety or prohibited-content reasons. These values describe distinct provider classifications. Preserve the exact reason instead of mapping all of them to blocked at ingestion.
Google's safety settings guide explains configurable safety categories and thresholds, along with built-in protections that remain enforced. Route policy should record the request settings that were sent and the candidate reason that returned. The user-facing response can use a smaller stable set of messages without erasing the provider evidence.
Avoid automatic retries for a deterministic content stop. Repeating the same prompt under the same model and settings can spend more tokens without changing the underlying policy. An application may offer an edited request, but that request enters a new decision and retains linkage to the original outcome.
My opinion is that collapsing SPII and RECITATION into one generic safety error is lazy telemetry. The on-call response and the product correction are different.
Tool and malformed states must fail closed
The enum covers malformed and unexpected function calls, excessive consecutive tool calls, missing thought signatures, and malformed responses. Each documented value should stop automatic tool execution. Preserve the exact raw enum, candidate, and finish message, then route the result to the named recovery or review path.
A malformed function call cannot be repaired by guessing missing arguments. An unexpected tool call indicates that the model emitted a tool call when the request enabled no tools. Too many consecutive tool calls signals an execution boundary rather than permission to reset the counter. Missing thought signatures and malformed responses likewise require protocol-aware handling.
Unknown enum values deserve the same fail-closed posture. Store the raw string and provider response digest, mark the parser version, and hold the candidate. Update the allowlist only after the team reads the current Google API contract and defines a route disposition.
The LLM API parameter allowlisting guide covers request controls. Response enum allowlisting applies the same discipline after generation.
Streaming must wait for the terminal candidate state
A streaming client receives partial content before all terminal metadata is known. Treat those chunks as provisional. The client can display them under a visible streaming state, but downstream commits and tool execution wait for the assembled candidate and terminal finishReason.
Record bytes or parts delivered before a blocking reason. If a stream ends without terminal metadata, classify the outcome as incomplete transport evidence rather than inventing STOP. A retry after partial delivery needs route-specific handling because a second generation may differ and can duplicate text already shown.
The response policy should also detect contradictions. An empty finish reason paired with a supposedly finalized candidate needs review. So does a STOP candidate that fails its declared schema. Preserve what Google returned and what the application observed as separate fields.
The safe idempotent LLM retries guide provides the operation-level rules for ambiguous transport outcomes and repeated effects.
A fixture should exercise known and unknown enums
Build a controllable Gemini-compatible stub behind the deployed HTTP route. Return one candidate with STOP and valid content, then another with STOP and invalid structured output. The first should pass content validation. The second should remain quarantined despite its natural stop.
Return MAX_TOKENS with partial JSON, SAFETY with no deliverable content, MALFORMED_FUNCTION_CALL with invalid arguments, and UNEXPECTED_TOOL_CALL on a route with no tools. Confirm that none triggers automatic tool execution or a downstream write.
Next, stream two text chunks and terminate with a blocking reason. The collector should retain the partial-delivery count and final classification. End another stream without a finish reason and verify the operation enters an incomplete state.
Finally, return VENDOR_NEW_REASON. The parser should preserve the raw value, stop delivery, and produce a review event. A deployment that silently treats an unknown enum as OTHER has erased the signal the fixture was built to test.
DeepInspect
DeepInspect can inspect Gemini responses for authenticated user or agent HTTP traffic deliberately routed through its proxy. It can enforce a finish-reason allowlist, bind candidate outcomes to caller identity and route policy, block disallowed responses, and write a per-decision record at the HTTP boundary.
The application owns candidate selection, schema parsing, continuation orchestration, provisional stream display, tool execution, and user-facing recovery. Direct Gemini calls and local actions that bypass the proxy remain outside DeepInspect's enforcement and evidence.
Book a demo today.
Frequently asked questions
- Does HTTP 200 mean a Gemini candidate completed successfully?
HTTP 200 describes the exchange, while candidate handling still requires
finishReasoninspection and route-specific content validation. A response can contain token-limited, safety-stopped, malformed, or otherwise unusable candidate output.- Is STOP enough to accept structured output?
STOPreports a natural model stop or configured stop sequence. Parse and validate the returned structure against the route's schema before acceptance. Keep provider completion separate from application disposition.- Should MAX_TOKENS output be shown to users?
Route policy decides. Conversational text may be shown as visibly partial, while structured output, function arguments, and high-impact workflow results should enter quarantine unless the route explicitly supports partial delivery.
- What should happen with a new finishReason value?
Fail closed, preserve the raw enum and response evidence, and review the current Google contract. Add an allowlisted disposition through a versioned policy update rather than guessing that the new value is equivalent to
STOPorOTHER.