Skip to main content
This page lists exactly the stability contract: the names in plurai.__all__ and in plurai.observability.__all__. Anything else you can import is implementation detail and may change between versions.

Writing simulate.py

mock

Registers a mock handler for one tool, with the descriptor the backend uses to generate the mock environment.
Returns a decorator that registers the handler. Arguments
  • tool_name (str): the tool’s name, as in plurai.config.yaml.
  • descriptor (dict | None): the tool’s descriptor. description and args are filled from the handler’s docstring and signature when omitted.
  • input_schema, output_schema (keyword): a Pydantic model for typed calls. Detected from a single Pydantic-typed parameter and a Pydantic return annotation when omitted; pass None to opt out.
  • **desc_kwargs: descriptor fields as keyword arguments.

observe

Registers an observer for a tool that runs for real, recording its result into the world.
Returns a decorator that registers the observer. Arguments
  • tool_name (str): the tool’s name.
  • capture (Callable | None): an optional capture function.

MockToolNode

A drop-in replacement for a LangGraph ToolNode that routes each tool call by its mode.
Returns a node with invoke and ainvoke, whose ToolMessages match ToolNode’s (name, tool_call_id, status, artifact, content). A real or observe result is rendered as JSON. A failed call is a message with status="error", not a raised exception. Calls run one after another, and injected state, store or runtime arguments are not supplied. Arguments
  • tools (list): the agent’s tools.
  • mock_config (dict[str, str] | None): tool name to mode, "mock" (default), "observe" or "real".
  • name (str, keyword): the node name. Default "tools".
  • messages_key (str, keyword): the state key holding messages. Default "messages".

mock_wrap

Wraps one tool callable so its calls route through the mock environment.
Returns the wrapped callable. It keeps the tool’s name, description and args_schema. Arguments
  • func (Callable): the tool.
  • mode (str): "mock" (default), "observe" or "real" (returns the tool unchanged).
  • failure (Callable[[MockToolError], Any] | None): for a tool whose error_channel is returns, builds the failure value the real tool returns. A tool declared raises raises MockToolError instead.

apply_mocks

Wraps every named function on a module in place with mock_wrap.
Returns list[str]: the names actually patched. A tool built inside a factory closure or exported under another name is not reachable and not patched. Arguments
  • module: the module that defines the tools.
  • mock_config (dict[str, str]): tool name to mode.

MockToolError

The exception a failed mocked call raises.
Attributes
  • message (str): the failure text.
  • case (str): which kind of failure: error_message, engine_refusal, handler_error or drifted_status_with_content.
  • response: the response as served.

mock_result

Turns a mock response into what your agent sees: the tool’s value, or a raised MockToolError.
Returns the tool’s value: content of a success, the error map of a failure the tool reports through its value, or the response whole when it has no standard shape. Raises MockToolError for any other failure. Arguments
  • response: the mock response.
  • channel ("raises" | "returns" | None): the tool’s declared error_channel. raises turns a reported failure into a raised MockToolError.

configure

Connects this process to a mock service, or hosts one in-process for local development.
Returns None. In connect mode it raises when the mock is unreachable or unhealthy; it never falls back to a local mock. Arguments
  • service_url (str | None): the mock service URL. Optional in local mode, where the host and port are not honored.
  • mode ("connect" | "local", keyword): default "connect".
  • mock_service_url (str | None, keyword): an alias of service_url. Pass one of the two.

SimulationContext

The scenario’s context, available in mock handlers, replacement nodes and your own code.
Attributes
  • agent_context (dict): the scenario’s context values.
  • world_state: the world proxy. See world_state.

get_simulation_context

Returns the current SimulationContext.
Returns SimulationContext.

world_state

The scenario’s world, read and written through the mock service.
Methods
  • get_entity(entity_id): one entity, or None.
  • query(entity_type, **filters): entities of a type matching exact field values.
  • apply(changes): write {entity_id: {fields}}. Each new entity carries _entity_type.
  • entities: a mapping of every entity. It is a Mapping, not a dict.
  • generate_uuid(): a new id.

world_state_snapshot

Returns the whole world as a plain dict, in one call.
Returns dict[str, dict]: entity id to entity. Free inside a mock handler; one HTTP call elsewhere. {} when no mock client is configured. Arguments
  • session_id (str | None): default the active session. Raises RuntimeError when none is active.

active_session_id

Returns the id of the session active in this context.
Returns str | None. None outside a scenario.

mock_service_url

Returns the mock service URL this process is configured against.
Returns str | None. None before configure.

file_row

The world_snapshot() row for one file your agent produced or was handed.
Returns dict with _entity_type, name, suffix, size_bytes, sha256 and, for a previewed file, text_preview (at most 4,000 characters, with _preview_truncated when cut). Spreadsheet formulas appear as their formula text with the value the file saved; nothing is calculated. A file too large or too slow to read is _unreadable with a reason. Arguments
  • path (str | PathLike): the file.
  • entity_type (str, keyword): the row’s type.
  • preview (bool, keyword): True (default) for a file your agent produced. False for a file handed to it, which adds content_type instead of a preview.

store_record

The record of your harness’s own store that a seeded world row stands for.
Returns dict: the row without _-prefixed keys and without the backend’s id copy. A row with no _entity_type, or of an undeclared type, raises HarnessFault. Arguments
  • row (Mapping): a seeded row.
  • entity_types (Mapping | None, keyword): default world_state.entity_types from the config beside the harness.

model_input_context

The input_context entries for one turn: what your agent’s model call received that the conversation does not carry.
Returns list[dict[str, str]]: {"source", "content"} per injected text, in model-input order. Agent-side messages, a leading system prompt, blank text and text the conversation already carries are left out. Arguments
  • model_input: the messages the model call received.
  • conversation: the messages the harness handed the agent.
  • source (str, keyword): the read the entries came from.

HarnessFault

Raised by simulate.py for a refusal the harness makes, so the scenario is not scored against your agent.
Recorded as not_valid. Recorded when constructed, so an agent tool that catches it does not hide it.

AgentFault

Raised by a harness on your agent’s behalf, when the agent’s own code failed where no Python frame shows it.
Raised from invoke_agent, recorded as agent_raised, a failed trial your agent owns. A provider failure and an earlier HarnessFault in the turn still win.

Host effects

For an agent built on claude_agent_sdk, these confine each scenario’s Claude Code CLI session.

Running simulations

These REST client types live in plurai.orchestrator, not the package root.

Orchestrator

Drives one scenario end to end: opens the conversation, runs your harness in a child process, plays the turns, and finalizes for scoring.
Returns from run: a ThreadResult. Arguments (constructor, keyword-only)
  • simulate_path (Path): your harness.
  • settings: the SDK’s settings object.
  • observability (ObservabilitySink): the recording sink; see create_observability_sink. The orchestrator never closes it.
  • client (OrchestratorClient): the backend client.
  • shape ("dialog" | "pipeline" | None): a debugging override; None lets the bench’s declaration decide.
Arguments (run, keyword-only): scenario_set_id, scenario_id, eval_set_id, max_turns (default 5), agent_version, mock_service_url, scenario_time_budget, and the required redrive_available and allow_turn_timeout_redrive.

OrchestratorClient

The async client for the backend’s simulation API.
Arguments
  • base_url (str): the backend base URL.
  • timeout (float): read timeout in seconds. Default 1200.
  • api_key (str | None): the bearer token.
  • retry: the retry policy. Default: the SDK’s.

ThreadResult

The outcome of one Orchestrator.run().
Fields include thread_id, turns, finalize (the scoring response), user_messages, chatbot_messages, tool_events, transcript_turns, ended_early and ended_early_cause, and resume_seed. summary() returns a short description.

OrchestratorAdapterError

Raised when your harness’s invoke_agent, seed_state or invoke_pipeline hits a misuse the runner can detect.
A RuntimeError. Attributes: child_exc_class (the original exception class in your harness), timed_out, thread_id, raised_at ("init" or "finalize"). The original exception is __cause__.

GraphRecursionWrap

An OrchestratorAdapterError raised when your LangGraph agent hit its recursion limit.
Attributes: cause_class ("local_seed", "remote_mock" or "unknown", best-effort), thread_id, turn_idx. It counts as your agent’s failed trial only when the agent was not silent before it crashed.

Tool and state contract

ToolDescriptor

The schema of one tools.<name>.descriptor block in plurai.config.yaml.
Fields include description (required), original_module, original_function, reads_from, writes_to, stateful, args, returns, agent_usage and business_rules. A descriptor whose writes_to is set must declare post-conditions.

Mock service

MockServiceClient

The HTTP client for a mock service, local or remote.
Arguments
  • base_url (str): the mock service URL.

MockService

The mock engine: world state, events and your registered handlers, served over HTTP. configure(mode="local") builds one for you.
Arguments: port (default 8199), host (default 127.0.0.1), and optional handlers, observers, descriptors, schemas. LocalMockService is a deprecated alias.

start_session

Starts a simulation session against the configured mock service and makes it the active one.
Returns the server’s response (dict). Concurrent scenarios are isolated per asyncio task. Arguments: session_id (str), initial_world_state (dict), agent_context (dict | None).

get_full_state

Returns the current world state of a session.
Arguments: session_id (str | None), default the active session.

get_events

Returns the recorded tool events of a session.
Arguments: session_id (str | None), default the active session.

Code action channel

For an agent that calls a vendor SDK or CLI from code it runs (for example a skill snippet), these route those calls to the mock environment. The surface is projected from the mock tools in plurai.config.yaml.

Version

__version__

The installed SDK version.

plurai.observability

Client-side recording of Runs to one observability platform. See Trace sources for configuration.

create_observability_sink

Builds the one active sink from settings.
Returns ObservabilitySink: the sink whose <SINK>__ENABLED flag is set, or a NoOpSink when none is. Enabling more than one sink raises ValueError at startup. Arguments: settings, the SDK’s settings object.

ObservabilitySink

The protocol every sink satisfies: tracing a Run and syncing its dataset. A malformed sink fails at startup.

NoOpSink

The sink in use when none is configured. Every method does nothing.

LangSmithSink, LangfuseSink, ArizeSink, BraintrustSink

The four sinks, each built from its config.

LangSmithSinkConfig, LangfuseSinkConfig, ArizeSinkConfig, BraintrustSinkConfig

Each sink’s settings, read from the matching <SINK>__<FIELD> environment variables.

LangSmithRunContext, LangfuseRunContext, ArizeRunContext, BraintrustRunContext

The per-scenario context each sink’s prepare_run returns and its later calls take. You do not build these yourself.

RollupMetric

One rollup feedback row a sink posts per scenario. Fields: key (str), score (float), comment (str), value (float | str).

swallowable_error_types

Builds the tuple of exception classes a sink may swallow, from (module_path, class_name) pairs that resolve.
Returns tuple[type[BaseException], ...], or fallback (default (OSError,)) when nothing resolves.