|
|
Testing ADK Agents with Code Execution Built-in Tool
Author: Venkata Sudhakar
ShopMax India uses ADK agents with the Code Execution built-in tool to compute order totals with GST, generate discount breakdowns, and calculate EMI schedules on the fly. Testing agents that use code execution requires mocking the execution sandbox so tests do not run arbitrary code in production environments, while still verifying that the agent constructs correct code, passes the right inputs, and uses the output in its response.
When testing ADK agents with the Code Execution tool, replace the sandbox executor with a controlled mock that captures the code submitted by the agent and returns a deterministic result. Assert that the submitted code contains the expected logic, that the agent correctly incorporates the execution output into its reply, and that it handles execution errors without crashing. This approach lets you test the agent's reasoning about code without running untrusted code in your test environment.
The example below mocks the code execution sandbox for a ShopMax India GST calculator agent. The mock captures the submitted code and returns a fixed computed total. Tests verify the agent submits code referencing the correct base price and GST rate, uses the result in its response, and handles a sandbox error gracefully.
It gives the following output,
... (3 passed in 0.01s)
In production, ShopMax India should run code execution in a sandboxed environment with strict memory and time limits to prevent runaway computations. Never let the agent execute code that contains customer-supplied strings without sanitization - validate all inputs before interpolating them into generated code. Log every submitted code snippet with a session ID so you can audit what was computed for any disputed order total or EMI calculation.
|
|