Skip to content
RehearsalDocs

Python SDK

The Python client in the rehearsal-kit package. Every resource and method, with its arguments and the API operation it calls.

The rehearsal-kit package is a Python client as well as a command. It needs Python 3.11 or later.

pip install rehearsal-kit
from rehearsal.sdk import Rehearsal

rh = Rehearsal()
run = rh.evaluations.create("<wv_id>", ["<profile_id>"], splits=["dev"], repeats=2)
for event in rh.runs.events(run["run_id"]):
    print(event["type"], event["summary"])
print(rh.runs.get(run["run_id"])["summary"])

Every method returns the JSON of the API's answer, as a dict or a list. The client adds no types of its own, so a field that the API adds is there at once.

The client

Rehearsal(api_key=None, base_url=None, timeout=60)
ArgumentMeaning
api_keyThe key. Without it: the environment variable REHEARSAL_API_KEY, then the sign-in that rehearsal login saved
base_urlThe server. Without it: REHEARSAL_URL, then the saved sign-in, then https://rehearsalkit.xyz
timeoutSeconds to wait for one request

With no key anywhere, the constructor raises ValueError.

Errors

A request that the server refuses raises RehearsalError. It has two fields: status, the HTTP status, and detail, the server's message. Errors lists them.

from rehearsal.sdk.client import RehearsalError

try:
    rh.evaluations.create("<wv_id>", ["<profile_id>"])
except RehearsalError as error:
    if error.status == 402:  # the plan does not cover it: do not retry
        print(error.detail)
    else:
        raise

Any operation

Three methods reach every operation of the REST API, for the ones below that have no method of their own.

MethodDoes
rh.get(path, params=None)A GET. Returns the JSON
rh.post(path, body)A POST with a JSON body. Returns the JSON
rh.stream(path, cursor=0)Reads a stream of events, and reconnects from the last event after a lost connection
usage = rh.get("/v1/usage")
job = rh.post("/v1/improvements", {"profile_id": "<profile_id>", "run_id": "<run_id>"})

rh.applications

rh.applications.create

rh.applications.create(name, source, images=None, policies='')

Adds an application. source is {"kind": "git", "url": ..., "commit": ...}, and images maps each service to a pinned image.

Calls POST /v1/applications.

rh.applications.import_catalog

rh.applications.import_catalog()

Adds the eight sample applications. Returns them as a list.

Calls POST /v1/applications/import-catalog.

rh.applications.list

rh.applications.list()

The applications of the project.

Calls GET /v1/applications.

rh.applications.get

rh.applications.get(app_id)

One application.

Calls GET /v1/applications/<app_id>.

rh.applications.upload_source

rh.applications.upload_source(app_id, archive)

Uploads a .tar.gz of the source, for an application added with the source kind upload.

Calls POST /v1/applications/<app_id>/source.

rh.worlds

rh.worlds.build

rh.worlds.build(application_id, **options)

Starts a build. It spends model budget. Options are the fields of the request body, for example scenario_target=8 and budget_usd=6. Returns job_id and world_version_id.

Calls POST /v1/world-builds.

rh.worlds.make_agent

rh.worlds.make_agent(world_version_id, name='', rounds=3, repeats=1)

Agent Factory: drafts an agent from a published world, hardens it with the fix loop and proves it on held-out jobs. Returns job_id and episodes; the finished job's result has every score and artifact_id, the zip to download.

Agent Factory: drafts an agent from the world, hardens it with the fix loop and proves it on held-out jobs. The finished job's result has every score and artifact_id, a zip with a runnable agent and its proof report.

Calls POST /v1/factory.

rh.worlds.list

rh.worlds.list()

The worlds, each with its versions.

Calls GET /v1/worlds.

rh.worlds.report_incident

rh.worlds.report_incident(wv_id, incident)

Turns a production incident (what was asked, what the agent did, what should have happened) into a job in this world. The job is validated like any other and, once valid, joins the dev split, so later evaluations and gates check for it. Returns the job to follow.

Turns something an agent got wrong in production into a validated job in this world (a job; poll it). Describe what was asked, what the agent did and what should have happened; a transcript is best.

Calls POST /v1/world-versions/<wv_id>/incidents.

rh.worlds.library

rh.worlds.library()

Ready-made worlds from the world library: the latest published version of each, with its job counts and tools. Any workspace can evaluate an agent on them, or make one, without building a world.

Ready-made worlds any workspace can rehearse an agent on, or make an agent for, without building one.

Calls GET /v1/library.

rh.worlds.version

rh.worlds.version(wv_id)

One world version.

Calls GET /v1/world-versions/<wv_id>.

rh.worlds.scenarios

rh.worlds.scenarios(wv_id)

The jobs of a world version.

Calls GET /v1/world-versions/<wv_id>/scenarios.

rh.worlds.validate

rh.worlds.validate(wv_id)

Re-checks a published world. It spends model budget. Returns a job.

Calls POST /v1/world-versions/<wv_id>/validate.

rh.worlds.bundle

rh.worlds.bundle(wv_id, include_images=False)

Exports a world version as a bundle file. Returns a job.

Calls POST /v1/world-versions/<wv_id>/bundle.

rh.jobs

rh.jobs.get

rh.jobs.get(job_id)

One server job: status, spent_usd, completed_phases, result, error.

Calls GET /v1/jobs/<job_id>.

rh.jobs.command

rh.jobs.command(job_id, command)

pause, resume, stop or retry.

Calls POST /v1/jobs/<job_id>/commands.

rh.jobs.events

rh.jobs.events(job_id, cursor=0)

The job's events, one at a time, until the job ends. It reconnects after a lost connection.

Calls GET /v1/jobs/<job_id>/events.

rh.jobs.wait

rh.jobs.wait(job_id, timeout_s=21600, poll_s=3)

Asks every few seconds until the job has ended, and returns it. Raises TimeoutError after timeout_s.

rh.profiles

rh.profiles.create

rh.profiles.create(name, system_prompt='', **fields)

Creates an agent profile. More fields go as keywords, for example model=... or driver="external".

Calls POST /v1/agent-profiles.

rh.profiles.list

rh.profiles.list()

The agent profiles, every version.

Calls GET /v1/agent-profiles.

rh.profiles.autofix

rh.profiles.autofix(profile_id, run_id, rounds=3, repeats=1)

Starts the fix loop on a finished run: rounds of propose and rehearse on train and dev, then the best version against the original on held-out jobs. Returns job_id and episodes; the finished job's result has every round.

Starts the fix loop: propose a version, rehearse it on train and dev, keep it only if it verifies more, and prove the best one against the original on held-out jobs. Returns the job; its result has every round.

Calls POST /v1/autofix.

rh.evaluations

rh.evaluations.create

rh.evaluations.create(world_version_id, profile_ids, splits=None, repeats=None, **fields)

Starts a run. It spends model budget. Returns run_id and episodes.

Without splits or repeats, the server runs every job twice with the default stress layer on.

Calls POST /v1/evaluations.

rh.evaluations.compare

rh.evaluations.compare(run_a, run_b=None)

Compares the agents in one run, or two runs on the same world version.

Calls GET /v1/compare.

rh.runs

rh.runs.get

rh.runs.get(run_id)

One run, with its status and summary.

Calls GET /v1/runs/<run_id>.

rh.runs.episodes

rh.runs.episodes(run_id)

The episodes of a run.

Calls GET /v1/runs/<run_id>/episodes.

rh.runs.failures

rh.runs.failures(run_id)

Why episodes failed, per agent: named causes, each with its count, what it means, its usual fix, example episodes and the checks that failed. Needs evaluator access.

Why episodes failed, per agent: named causes, each with its count, its usual fix and example episodes.

Calls GET /v1/runs/<run_id>/failures.

rh.runs.episode

rh.runs.episode(ep_id)

One episode in full.

Calls GET /v1/episodes/<ep_id>.

rh.runs.events

rh.runs.events(run_id, cursor=0)

The run's events, one at a time, until the run ends.

Calls GET /v1/runs/<run_id>/events.

rh.runs.intervene

rh.runs.intervene(ep_id, **body)

Changes a running episode by hand. The keywords are the fields of the request body, for example kind="customer_message", text=....

Calls POST /v1/episodes/<ep_id>/interventions.

rh.runs.command

rh.runs.command(run_id, command)

pause, resume or stop, for every part of the run.

Calls POST /v1/runs/<run_id>/commands.

rh.external

Drive an episode whose profile uses driver="external".

rh.external.observation

rh.external.observation(ep_id)

The turn that waits for your agent in an episode, if one does.

Calls GET /v1/episodes/<ep_id>/observation.

rh.external.act

rh.external.act(ep_id, actor, tool, args, idempotency_key, wait_s=120)

Sends one action and waits for its result, which it returns. Raises TimeoutError after wait_s; ask again with the same key.

Calls POST /v1/episodes/<ep_id>/actions, then GET /v1/episodes/<ep_id>/actions/<idempotency_key>.

The saved sign-in

rehearsal login writes the server and the key to one file, which only your user can read. The client reads it when the environment has no key.

FunctionDoes
rehearsal.sdk.client.config_path()Where the file is: ~/.config/rehearsal/config.json, or the path in REHEARSAL_CONFIG
rehearsal.sdk.client.saved_login()The saved url and api_key, or an empty dict
Generated from the code of rehearsal-kit 0.1.4.

Was this page helpful?

On this page

Was this page helpful?