Repository navigation
Description
Description
When SetModelResponseTool raises a ValidationError (e.g., LLM passes null for required string fields), the ReflectAndRetryToolPlugin correctly catches the error via on_tool_error_callback and returns a ToolFailureResponse. However, get_structured_model_response() in _output_schema_processor.py only checks the tool name (set_model_response) without inspecting whether the response is an error or a valid result. This causes the error response to be treated as the final model output, completely bypassing the plugin's retry mechanism.
Steps to Reproduce
- Create an
LlmAgentwithoutput_schema(e.g., requiredstrfields) and at least one tool - Use a non-Vertex AI model (e.g., LiteLLM) so
can_use_output_schema_with_tools()returnsFalseandSetModelResponseToolis injected - Enable
ReflectAndRetryToolPluginwithmax_retries=3 - Trigger the agent where the LLM calls
set_model_responsewithnullfor a required string field
Expected Behavior
ReflectAndRetryToolPlugin should retry the set_model_response call up to max_retries times, giving the LLM a chance to correct its output.
Actual Behavior
The ToolFailureResponse (with response_type: "ERROR_HANDLED_BY_REFLECT_AND_RETRY_PLUGIN") is immediately treated as the final model response. No retry occurs.
Root Cause
In base_llm_flow.py (lines ~649-659 and ~695-705):
yield function_response_event
if json_response := _output_schema_processor.get_structured_model_response(
function_response_event
):
final_event = _output_schema_processor.create_final_model_response_event(
invocation_context, json_response
)
yield final_event # Error response becomes final outputAnd in _output_schema_processor.py (lines 111-114):
for func_response in function_response_event.get_function_responses():
if func_response.name == 'set_model_response':
return json.dumps(func_response.response, ensure_ascii=False) # No validationget_structured_model_response() matches only on func_response.name == 'set_model_response' and returns the response as-is, without checking if it's a valid schema result or an error/reflection response from a plugin.
Suggested Fix
get_structured_model_response() should validate the response against the agent's output_schema before treating it as the final output. If validation fails (e.g., the response is a ToolFailureResponse from a plugin, not actual schema data), return None so the LLM loop continues and the model can retry.
def get_structured_model_response(
function_response_event: Event,
output_schema: type[BaseModel] | None = None,
) -> str | None:
if (
not function_response_event
or not function_response_event.get_function_responses()
):
return None
for func_response in function_response_event.get_function_responses():
if func_response.name == 'set_model_response':
response = func_response.response
# Validate against output_schema before accepting as final response
if output_schema and isinstance(response, dict):
try:
output_schema.model_validate(response)
except ValidationError:
return None # Not valid schema data; let the LLM loop continue
return json.dumps(response, ensure_ascii=False)
return NoneThis approach is plugin-agnostic — it doesn't need to know about ReflectAndRetryToolPlugin or any specific error response format. Any response that doesn't match the output_schema (error responses, reflection guidance, etc.) is naturally skipped.
Note on throw_exception_if_retry_exceeded=False
When ReflectAndRetryToolPlugin is configured with throw_exception_if_retry_exceeded=False and max retries are exceeded, the plugin returns a "do not use this tool again" guidance message instead of raising an exception. With this fix, that message would also fail schema validation and return None, causing the LLM loop to continue. However, since output_schema requires the LLM to eventually call set_model_response, this creates a contradictory situation where the plugin tells the LLM not to use the tool it must use. This is a pre-existing design tension between set_model_response and throw_exception_if_retry_exceeded=False, not introduced by this fix. The default setting (throw_exception_if_retry_exceeded=True) handles this correctly by raising an exception to stop the loop.
Environment
google-adk: 1.23.0+- Model: Gemini 2.0 Flash via LiteLLM (non-Vertex AI)
- Python: 3.11
Activity
surajksharma07 commentedon Feb 18, 2026
@chaerrypick01 confirmed this in
get_structured_model_response()(_output_schema_processor.py:112), which matches only onfunc_response.name == 'set_model_response'and returns the response as-is, so aToolFailureResponsefrom the plugin gets blindly promoted to final model output before any retry can happen.The fix is to add a guard before the
return json.dumps(...)line: checkif isinstance(response, dict) and response.get("response_type") == "ERROR_HANDLED_BY_REFLECT_AND_RETRY_PLUGIN": return None— this causes the flow to skip creating the final event and lets the LLM loop continue for retry.Your schema-validation approach (
output_schema.model_validate(response)) also works and is more plugin-agnostic,but it requires updating both call sites in
base_llm_flow.py(~lines 1023 and 1069) to passinvocation_context.agent.output_schemaas well.Either way, not fixed in v1.25.0 — raising this for a patch.
surajksharma07 commentedon Feb 18, 2026
@seanzhou1023 Please have a look into it.
GWeale commentedon Aug 6, 2026
the fix #6368 is on main but not in any release yet, so upgrading today will still reproduce it. it ships in the next release.