
- Tools fail gracefully and return clear, structured error signals.
- The agent does not crash or leave sessions in a broken state.
- Users always receive a helpful next step (for example: “please restart the service”, “check this file”, or “contact IT”).
Always return structured results from tools so the LLM can interpret failures and produce helpful user-facing messages.
- Traditional apps throw exceptions, log them, and show a message. LLM-driven agents need machine-readable failure signals.
- Convert failures into a predictable structured object so the model can:
- Recognize failure as a structured signal (not a raw stack trace).
- Produce concise, user-friendly explanations and next steps.
- This reduces hallucinations, keeps sessions alive, and enables consistent user experience.
- Instead of letting an exception bubble up as an unstructured Python stack trace, catch exceptions and return a structured object with:
- status: “success” | “error”
- payload fields (e.g., “ticket”, “service”, “status_text”)
- error_message: user-facing text and optional short technical hint (exception type or brief details)
Example: create_ticket_impl (structured success return)
- Normalize the input, check a status store, and on unexpected errors return a structured error with a friendly message and a concise technical hint.
Avoid returning raw stack traces or long technical dumps to users. Return a short user-facing error_message and, if needed, a brief technical hint (exception type). Store full traces in logs/tracing for debugging.
- Tools return a structured “status”: “error” and a clear “error_message” so the LLM can:
- Produce a concise natural-language explanation (user-facing).
- Offer an actionable next step (suggest valid service names, propose opening a ticket, etc.).
- The agent session stays active; the LLM translates structured errors into helpful suggestions rather than crashing or exposing raw exceptions.
- In this example the tool returned an error indicating an unknown service ‘cause_internal_error’. The LLM examined that structured response and produced a helpful message listing available services and suggesting the user check for typos or try one of the known services.
- The ADK web UI traces each function invocation and its structured response. Use the trace tab to inspect the functionResponse and see the exact status and error_message returned by the tool.
- Convert unexpected exceptions into structured error responses (status + payload or error_message).
- Provide a short user-facing message and an optional brief technical hint (exception type).
- Keep tool responses consistent across your toolset so the LLM always has predictable signals.
- Log full traces and technical details to your tracing/log system (e.g., ADK traces) rather than returning them to the user.
- Add a lightweight evaluation loop to verify critical flows automatically (for example: tool returns for common inputs and handling for intentionally injected failures).
- Apply the same defensive pattern to all function tools so the agent always receives predictable, structured signals about success and failure.
- ADK traces and tooling in the ADK web UI.
- pydantic documentation: https://docs.pydantic.dev/
- Follow structured error patterns to improve LLM-agent resiliency and user experience.