fix(models): surface error when model returns STOP with empty content#5636
Open
Oppong08 wants to merge 2 commits intogoogle:mainfrom
Open
fix(models): surface error when model returns STOP with empty content#5636Oppong08 wants to merge 2 commits intogoogle:mainfrom
Oppong08 wants to merge 2 commits intogoogle:mainfrom
Conversation
Tighten LlmResponse.create() so a Gemini candidate with empty parts and finish_reason=STOP no longer passes through as a successful empty response. It now routes to the error branch with error_code='MODEL_RETURNED_NO_CONTENT' and a descriptive error_message, so callers see an actionable error event instead of a silent empty final agent output. Reproduces against gemini-2.5-flash-lite when the second turn after a tool call returns zero output tokens. Also broadens the skip-empty guard in BaseLlmFlow._postprocess_async to treat Content(parts=[]) as no-content (defense in depth) and updates the two existing tests that codified the old behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tighten LlmResponse.create() so a Gemini candidate with empty parts and finish_reason=STOP no longer passes through as a successful empty response. It now routes to the error branch with error_code='MODEL_RETURNED_NO_CONTENT' and a descriptive error_message, so callers see an actionable error event instead of a silent empty final agent output. Reproduces against gemini-2.5-flash-lite when the second turn after a tool call returns zero output tokens.
Also broadens the skip-empty guard in BaseLlmFlow._postprocess_async to treat Content(parts=[]) as no-content (defense in depth) and updates the two existing tests that codified the old behavior.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
With
gemini-2.5-flash-liteand anLlmAgentthat calls a tool, the run can sometimes terminate withfinal_output: "".The reported flow is:
function_call, such as apython_executortool call.Content(role="model", parts=[])withfinish_reason=STOPand zero output tokens.This happened because
LlmResponse.create()acceptedfinish_reason=STOPas a successful response even whencontent.partswas empty. In addition, the skip-empty guard inBaseLlmFlow._postprocess_asynconly checked whetherllm_response.contentexisted, so aContentobject withparts=[]could still pass through as a final response.Solution:
This PR tightens
LlmResponse.create()so a Gemini candidate with empty parts andfinish_reason=STOPno longer passes through as a successful empty response.Instead, it routes to the error branch with:
error_code="MODEL_RETURNED_NO_CONTENT"error_messageThis gives callers an actionable error event instead of a silent empty final agent output.
This PR also broadens the skip-empty guard in
BaseLlmFlow._postprocess_asyncto treatContent(parts=[])as no content unless an error is present. This acts as defense in depth and prevents empty content objects from being emitted as meaningful final responses.This approach was preferred over adding retry behavior because it keeps the change small, avoids extra latency/cost, and surfaces the underlying model behavior clearly to callers. Non-
STOPempty responses, such asMAX_TOKENSorSAFETY, continue to preserve their existingfinish_reasonas the error code.Testing Plan
Unit Tests:
Added/updated coverage includes:
LlmResponse.create()returnserror_code="MODEL_RETURNED_NO_CONTENT"when a candidate hasfinish_reason=STOPwith empty parts.LlmResponse.create()returns the same no-content error when candidate content is missing withfinish_reason=STOP.finish_reason=STOPstill succeeds.STOPempty responses preserve their existing finish reason as the error code.BaseLlmFlowsurfaces an error event for the post-tool empty response case instead of emitting a silent empty final event.Passed locally: