﻿# Giskard WithSpy: verify the looked-up Python call, not just the final answer

Use Giskard WithSpy to inspect internal-call arguments with the correct patch namespace and per-interaction metadata, separating observed calls from response correctness.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-giskard-withspy-import-namespace-interaction-calls/
Language: en

## Specify the internal behavior to observe

An agent can mention the right customer or dataset while sending the wrong filter to an internal lookup. Giskard's [WithSpy guide](https://docs.giskard.ai/oss/checks/how-to/spy-on-calls) describes observing a function during an interaction and recording its call history. Define the expected dependency and arguments separately from the final-answer requirement. This makes it possible to detect a valid-looking response produced through an unintended internal request rather than treating fluent output as evidence that application logic was followed.

## Patch the namespace the code uses

The [Python mock reference](https://docs.python.org/3/library/unittest.mock.html) explains that patching must target where an object is looked up. A function imported into another module can be bound under that module's name. Patching only its original definition may therefore miss the call under test. Inspect the application's import boundary and verify the spy on a known invocation before interpreting an empty history as proof that the agent did not use the dependency.

## Preserve interaction-level history

The Giskard guide records call information in interaction metadata under the target key and resets the mock before the next interaction. Interpret that scope explicitly. A final interaction's call count is not automatically a cumulative count for the entire conversation. If the requirement spans several turns, inspect the relevant interactions and combine evidence according to the declared rule. Keep argument history attached to the turn that produced it rather than joining everything through the last displayed answer.

## Check calls and output independently

The [library overview](https://docs.giskard.ai/oss) describes scenarios and checks as behavioral requirements. A correct internal argument can coexist with a wrong answer, and a correct answer can coexist with an unintended call. Test both properties where needed. Also preserve the observation boundary: an in-process Python spy does not independently prove what a remote dependency ultimately executed. External outcomes require their own evidence when the claim extends beyond the observed function invocation.

## Verify a deliberately wrong invocation

Use a benign fixture with distinct expected and unexpected filter arguments, then inspect recorded calls and the final output. Include a later interaction with no call to confirm reset behavior. The guides to [trace execution evidence](/journal/fg2-promptfoo-trace-assertions-span-presence-selection/) and [feedback target identity](/journal/fg2-phoenix-feedback-rest-otel-id-graphql-global-id/) provide related controls. Accept the test when namespace, interaction scope and argument assertions explain the result, preserving the difference between an agent saying it performed the right action and the application making the intended internal call.
