Reprocessing Invalid Outputs #2970
-
I am working on integrating Google ADK into Mongo databases to serve as a real time analytics agent. Most of the time, the application works fine, but in some cases, the agent attempts to run Python code directly in the context like "Print(default_api." and this results in a Malformed Function Call and the processing stops. Despite attempts to suppress this via prompting, I am unable to get it to stop. When I send a follow on user message like "Can you please try again?" the agent handles the issue and proceeds. Separately - after a long running mongo aggregation, the model just returns an empty response. Same as before, I am able to send a follow up message and the chat corrects itself. In both cases I am able to detect the issue using the "after_model_callback", but I am unable to figure out how I can reject the output/ask it to reprocess programatically. Any thoughts on how I can address either issue? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: You can use the Hello! Thanks for reaching out with this interesting question. It's a great use case for handling model inconsistencies programmatically. You are on the right track with using the Replacing Invalid OutputAs you've noted, you can detect both the malformed function call and the empty response in Here is a conceptual example of how you could implement this: from adk.llm import LlmResponse
def my_after_model_callback(llm_response: LlmResponse) -> LlmResponse | None:
response_content = llm_response.content
# Check for empty or malformed response
is_empty = not response_content
is_malformed = "Print(default_api." in response_content # Example check
if is_empty or is_malformed:
# Replace the bad response with a specific, structured message
# that your application can understand.
return LlmResponse(
content={
"error": "model_output_error",
"message": "The model returned an invalid response. Triggering a retry."
}
)
# If the response is valid, do nothing and let ADK proceed.
return None
# Then, register your callback when running the agent:
# --- Register it during Agent creation ---
my_agent = LlmAgent(
name="MyCallbackAgent",
model="gemini-2.0-flash", # Or your desired model
instruction="Be helpful.",
# Other agent parameters...
after_model_callback=my_after_model_callback # Pass the function here
) Triggering the RetryThe The most effective solution is to handle the retry logic in your client application—the code that calls
Other Considerations
I hope this helps you build a more robust solution! [1] https://google.github.io/adk-docs/callbacks/ |
Beta Was this translation helpful? Give feedback.
-
thanks this is tremendously helpful - for right now I am in the prototyping stage and using the "adk web" runtime. Any idea how I would incorporate
when running this way? |
Beta Was this translation helpful? Give feedback.
Response from ADK Answering Agent (experimental, answer may be inaccurate)
TLDR: You can use the
after_model_callback
to detect and replace the invalid output with a custom message. While you can't trigger a model re-run directly from the callback in the same turn, your client application can detect this custom message and programmatically send a follow-up prompt (like 'Please try again') to initiate the retry.Hello! Thanks for reaching out with this interesting question. It's a great use case for handling model inconsistencies programmatically.
You are on the right track with using the
after_model_callback
. This callback is the perfect place to inspect the model's output before it's pr…