Skip to main content
The Netra SDK exposes a simulation client that lets you:
  • Run simulations - Execute multi-turn conversations against your AI agent
  • Define tasks - Create custom task implementations to wrap your agent
  • Handle file attachments - Receive base64-encoded files from dataset items in your task
  • Use lifecycle hooks - Run setup/teardown scripts around scenarios (before_all, before_each, before, after, after_each, after_all)
  • Control concurrency - Manage parallel execution for throughput
This page shows how to use Netra.simulation to run multi-turn simulations and test your AI agents programmatically.

Getting Started

The simulation client is available on the main Netra entry point after initialization.

run_simulation

Execute a multi-turn conversation simulation against a dataset. Your task function is called repeatedly for each turn until the conversation completes.

Parameters

Response


BaseTask

Create a custom task by inheriting from the BaseTask abstract base class. Your implementation wraps your AI agent and handles the conversation flow.

Implementation Requirements


Hooks (Pre/Post Scripts)

Multi-turn datasets often share state across scenarios. Rather than merging scenarios or forcing sequential execution, use hooks to set up and tear down state around scenario runs. Hooks run entirely on the SDK side — script code never leaves your environment. Lightweight metadata (function name and description) is sent to the backend so the Netra UI can show which hooks are configured. In the Netra dashboard (multi-turn Scenario Run DetailsConversation tab), configured hooks appear in collapsible Pre-script / Post-script panels with each hook’s name and description. Item run status badges include Prescript Failed and Postscript Failed.

Hook points

Context passing

setup_context is the merge of before_all + before_each + item before. It is passed to BaseTask.run, item after, and after_each. If a before hook fails mid-way, teardown still receives the furthest successfully built setup_context (so after_each can clean up what before_each created). after_all receives only the run-level shared_context, but its results include setup/first-turn failures as well as conversation failures. Prefer before_each / after_each when the same setup/teardown applies to every scenario. Use item-keyed before / after for scenario-specific logic.

SimulationHooks

All hooks can be sync or async. Set a .description property on each hook function so the Netra UI can display it (truncated to 200 characters):

Example

Keys in before / after must be real dataset_item_id values from your dataset. A prescript_failed item is terminal (eval suppressed); other scenarios continue. A postscript_failed item is also terminal, but eval is not suppressed — conversation evaluations remain valid. Wrap teardown in try/except so cleanup errors do not mark otherwise-successful items postscript_failed.

File Handling

Dataset items can include file attachments. When present, the simulation framework automatically detects the required files for a turn and delivers it to your task’s run() method as a list of ProcessedFile objects.

ProcessedFile

Each file delivered to your task is a ProcessedFile instance with the following fields:

Complete Example

Complete Example with Hooks

Next Steps

Last modified on August 4, 2026