Page options
Biocompile Python SDK for the public API
Last updated
On this page
The Biocompile Python SDK is a small client for the public API, published as the biocompile
package. It exchanges your credential for access tokens and renews them, retries what is safe
to retry, pages through lists and returns the API’s JSON as plain Python dictionaries. It needs
Python 3.10 or later and has one dependency, httpx.
Install
Install it with pip:
pip install biocompile
Create a client
The client reads its credential from the environment, so no secret appears in your code:
from biocompile import Client
# Reads BIOCOMPILE_PAT, or BIOCOMPILE_CLIENT_ID and BIOCOMPILE_CLIENT_SECRET
# for a service account, and BIOCOMPILE_ORGANIZATION_ID.
client = Client.from_env()
for organization in client.organizations.iter():
print(organization["_id"], organization["name"])
BIOCOMPILE_ORGANIZATION_ID sets the organization that methods use when you pass None. A
service account client is always pinned to its organization. Keyword arguments to Client.from_env
override the environment.
The client never sends your long-lived credential to the API. It exchanges it on the first
call, caches the access token and exchanges again about a minute before it expires. Credentials
print as ***, and no error message contains them.
Resources and scopes
Each area of the API is one attribute of the client. Methods that work in an organization take
its ID first, and None uses the client’s organization:
| Attribute | Main methods | Scopes |
|---|---|---|
client.organizations, client.projects | list, iter, get, create, update, archive | projects:read, projects:write |
client.hypotheses, client.observations, client.entities | list, iter, get | entities:read |
client.workbenches | list, open, get, update, list_drafts, draft, discard_draft, prepare_contribution, contribute | entities:read, entities:write, contributions:write |
list_files, upload_file, replace_file, update_file, delete_file, download_file | files:read, files:write | |
client.contributions, client.revisions | iter, get, list; client.projects.version | entities:read |
client.files | get, download (published files by identifier) | files:read |
client.computes | list, get, versions | runs:read |
client.executables, client.plans | catalog, get, create_plan, invoke, approve_spending | runs:read, runs:execute, spending:approve |
client.runs | iter, get, logs, download_artifact, cancel | runs:read, runs:execute |
client.conversations | create, send, get_turn, send_and_wait, respond | agent:converse |
client.webhooks | create, list, update, delete, rotate_secret, test, redeliver | webhooks:manage |
Research projects and Executables are addressed by their slug, not their _id. A listed
project carries both, so pass project["slug"] (rp_...) to client.projects.get,
update and archive, and an Executable’s slug to client.executables.get and invoke.
An _id answers 404, and the SDK’s NotFound message then says to use the slug.
Page through lists
Use the iter methods. The lists page in different ways: runs and Executables by skip,
conversations and contributions by next_cursor, research records by pagination.next_cursor.
iter follows each one to the end, and keeps the page size within the
list’s maximum:
for run in client.runs.iter(None, status="completed"):
print(run["slug"], run["status"])
list returns a single page exactly as the API sent it. Its limit has a maximum (100 for
conversations and contributions, 500 for most other lists), and a larger value raises
ValidationError.
Change a project
Changes to research records and files are drafts in a workbench. Contributing them merges them into the project at once, recorded as your decision:
workbench = client.workbenches.open(None, project_slug="rp_a7x3", name="Import from the March screen")
wbk = workbench["workbench_id"]
drafted = client.workbenches.draft(
None, wbk,
[{"operation": "create", "collection": "hypotheses",
"payload": {"statement": "MAPK14 inhibition reduces IL-6 release in THP-1 cells."},
"rationale": "Seen in three independent plate runs."}],
idempotency_key="march-screen-batch-1",
)
vcf = client.workbenches.upload_file(None, wbk, "variants_strain_K12-04.vcf", folder="/variants")
include = [drafted["drafts"][0]["ref"], vcf["ref"]]
prepared = client.workbenches.prepare_contribution(None, wbk, include=include)
result = client.workbenches.contribute(
None, wbk,
title="Import from the March screen", summary="Three hypotheses from the kinase screen.",
include=include, expected_project_version=prepared["project_version"],
)
print(result["project_version"], result["url"])
Contributing needs contributions:write, which only a person’s own credential carries. If the
project took another contribution since you prepared, contribute raises Conflict: prepare
again. Published files download by identifier with client.files.download(None, "fil_cd34", "variants_copy.vcf"). Downloads are written to a temporary file and renamed only when
complete.
Talk to the assistant
send_and_wait sends a message and polls the turn until the assistant has answered:
conversation = client.conversations.create(None, title="Weekly literature scan")
turn = client.conversations.send_and_wait(
None, conversation["conversation_id"],
"Summarize the papers published this week on MAPK14 inhibitors in inflammation.",
idempotency_key="weekly-scan-2026-w44",
timeout=900,
)
for event in turn["events"]:
for part in event["parts"]:
if part["type"] == "text":
print(event["author"], part["text"])
A turn that waits for an answer keeps polling until it continues, unless you pass
return_on_requires_action=True. After timeout seconds the call raises TurnTimeout, with
the last state on .turn.
Answer what the assistant waits for with respond, unless required_action["type"] is
respond_in_app. Then a person answers in Biocompile:
turn = client.conversations.send_and_wait(
None, conversation["conversation_id"], "Plan a docking run",
return_on_requires_action=True,
)
action = turn["required_action"]
if action and action["type"] == "plan_approval":
client.conversations.respond(
None, conversation["conversation_id"], turn["turn_id"],
action_id=action["action_id"], decision="approve",
)
A checkpoint is answered once, so respond is never retried. Pass include_tool_io=True to
get or get_turn to read each tool call’s arguments and result, with secrets redacted.
Verify webhooks
verify_signature checks a delivery against your whsec_ secret and returns the event:
from biocompile import WebhookVerificationError
from biocompile.webhooks import verify_signature
def receive(headers, raw_body: bytes) -> int:
try:
event = verify_signature(WEBHOOK_SECRET, headers, raw_body)
except WebhookVerificationError:
return 400
enqueue(headers["webhook-id"], event)
return 204
Errors and retries
Every error derives from biocompile.BiocompileError. HTTP errors carry the API’s status,
code, detail and retry_after:
| Exception | When |
|---|---|
AuthenticationError | 401, or the token endpoint refused the credential |
PermissionDenied | 403, with the missing scopes on missing_scopes |
NotFound | 404 |
RateLimited | 429 after the retries ran out |
ServiceUnavailable | 503 after the retries ran out |
TransportError | No answer at all, such as a refused connection |
The client retries 429 and 503 answers up to three times, waiting for Retry-After. It
retries a POST only when the server refused it before doing anything, or when
workbenches.draft or conversations.send carries an idempotency key. On
executables.invoke and plans.invoke the idempotency_key is part of the request body; the
client does not resend those calls, so reuse the same key if you resend one yourself. client.rate_limit holds the rate limit headers of the latest
response.
Common questions
Which version of the Biocompile SDK does pip install?
The SDK is in alpha, so all its releases are pre-releases (such as 0.1.0a2). While no stable
release exists, pip install biocompile installs the newest of them. Once a stable release is
out, add --pre to get an alpha. Pin the version in production scripts, because method names
can still change before 1.0.
Does the Biocompile SDK support async code?
Not yet. The client is synchronous. In async code, run calls in a worker thread, for example
with asyncio.to_thread.
Is the Biocompile SDK open source?
The package is published under the Apache License 2.0, and its source is included in the package on PyPI.
Related
Was this page helpful?
Something wrong or missing?