WithSpy is an InteractionSpec wrapper that temporarily patches a target
function β identified by its Python import path β with a MagicMock while
the wrapped interaction generator runs. After each interaction completes, the
mockβs call history (call_count, call_args, call_args_list, mock_calls)
is injected into Interaction.metadata under the target key, and the mock is
reset before the next interaction.
The typical use case is verifying that an agent passes the right arguments
to an internal call β for example, confirming that a database query triggered by
a tool call used the correct filter parameters.
We have an order-support agent. When a user asks about their orders, the agent
calls fetch_orders to retrieve them from the database and then formats a
reply. We want to verify two things:
The agentβs final answer mentions the order count.
fetch_orders was called with the correct customer_id and status
filter β i.e., the agent didnβt accidentally query the wrong customer or
drop the status filter.
target is the Python import path that mock.patch will use to replace
fetch_orders for the duration of the interaction. Use the same dotted path
you would pass to unittest.mock.patch.
from giskard.checks import Scenario, Interact, WithSpy, FnCheck
interaction_spec =Interact(
inputs="What are my shipped orders?",
outputs=order_support_agent,
)
spied_spec =WithSpy(
interaction_generator=interaction_spec,
target="__main__.fetch_orders",# Python import path β same as mock.patch
)
Output
Thank you for using Giskard open-source! π’ π
Giskard Enterprise adds deeper agent scans, audit reports
with remediation guidance, test review interfaces
for root-cause analysis & human feedback integration,
and team collaboration β with flexible pricing.
Learn more: https://giskard.ai
Use .add_interaction() instead of .interact() when passing a WithSpy (or
any raw InteractionSpec). The scenario check verifies the agentβs output;
the spy check is done separately after run() using spy_data.