opentargets-py
Modern Python client for the Open Targets Platform GraphQL API — with sync and async clients, a --json CLI, and an MCP server so AI agents can query it directly.
Disclaimer: Unofficial, community-maintained. Not affiliated with or endorsed by the Open Targets consortium. For the official platform, visit platform.opentargets.org.
The official opentargets package was deprecated when Open Targets migrated to GraphQL in 2021 and has since been removed from PyPI. This library fills that gap — it is the only Python SDK targeting the current GraphQL API.
Installation
pip install opentargets-py # core SDK (sync + async)
pip install opentargets-py[pandas] # adds DataFrame output
pip install opentargets-py[cli] # adds the `opentargets` CLI
pip install opentargets-py[mcp] # adds the `opentargets-mcp` MCP server
pip install opentargets-py[all] # everything
Quick start
Sync
from opentargets import OpenTargetsClient
with OpenTargetsClient() as client:
target = client.get_target("EGFR")
print(target.approved_name) # epidermal growth factor receptor
associations = client.get_target_associations("EGFR", limit=10)
for a in associations:
print(a.disease_name, round(a.score, 3))
Async (concurrent fan-out)
import asyncio
from opentargets import AsyncOpenTargetsClient
async def main():
async with AsyncOpenTargetsClient() as client:
targets = await asyncio.gather(
client.get_target("EGFR"),
client.get_target("BRAF"),
client.get_target("KRAS"),
)
for t in targets:
print(t.approved_symbol, t.biotype)
asyncio.run(main())
CLI
opentargets target EGFR # rich table
opentargets target EGFR --json # machine-parseable
opentargets targets EGFR BRAF KRAS --json # batch
opentargets disease EFO_0000311 --targets --json | jq '.targets[0]'
opentargets search "lung cancer" --json
Every subcommand supports --json, so the CLI is usable from shell scripts, agents, and notebooks alike.
MCP server (Claude Desktop, Cursor, etc.)
pip install opentargets-py[mcp] # requires Python 3.10+
Then add to your claude_desktop_config.json (macOS path:
~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"opentargets": {
"command": "opentargets-mcp"
}
}
}
Now you can ask Claude things like "What diseases is EGFR associated with?" and it will call the SDK directly. Exposed tools: get_target_info, find_target_associations, get_target_drugs, get_target_tractability, get_target_safety, get_target_expression, get_target_constraint, get_disease_info, find_disease_targets, get_drug_info, get_drug_indications, search_open_targets.
Features
- Sync + async clients —
OpenTargetsClientandAsyncOpenTargetsClientwith full feature parity - 15 endpoint methods — targets, diseases, drugs, associations, tractability, safety liabilities, baseline expression, genetic constraint, ChEMBL crosslinks, search
- Symbol resolution — pass
"EGFR"instead of"ENSG00000146648" - Auto-pagination — fetches all pages transparently
- Caching — in-memory
TTLCacheby default; opt intoDiskCache(SQLite, survives process restarts) for repeated runs - Configurable retries —
RetryConfig(max_retries=..., base_delay=..., retryable_statuses=..., respect_retry_after=...) - Pandas integration —
as_dataframe=Trueon list-returning methods - Typed — full Pydantic v2 models,
py.typedmarker,mypy --strictcompliant - CLI —
opentargetswith--jsonon every command, rich tables for humans - MCP server —
opentargets-mcp, drop-in for Claude Desktop / Cursor / any MCP host - Minimal core — just
httpx+pydantic; everything else is an opt-in extra
Configuration
Disk cache (persist across runs)
from opentargets import OpenTargetsClient, DiskCache
cache = DiskCache(path="~/.cache/opentargets.db", ttl=86400) # 1 day
with OpenTargetsClient(cache=cache) as client:
target = client.get_target("EGFR") # first run: API
target = client.get_target("EGFR") # second run (even new process): cache hit
Custom retry policy
from opentargets import OpenTargetsClient, RetryConfig
retry = RetryConfig(max_retries=5, base_delay=2.0, max_delay=120.0)
client = OpenTargetsClient(retry_config=retry)
For AI agents
This package was designed to be usable by both humans and LLM-based agents. Three deliberate affordances:
- MCP server (
opentargets-mcp) — wraps every public method as an MCP tool with LLM-friendly docstrings. Configure once and the model calls Open Targets directly. See MCP setup above. - CLI with
--json— every subcommand emits clean JSON, so agents can shell out and parse the result withjqorjson.loads. - Typed Pydantic responses — every method returns a model with
.model_dump()/.model_dump_json(), so programmatic agents get structured data without parsing.
See llms.txt for a skimmable one-page overview of the full API surface, optimized for LLM ingestion.
More examples
See the examples/ directory:
basic_usage.py— targets, diseases, drugs, searchbatch_query.py— batch fetch + DataFrame outputnetwork_analysis.py— build a target–disease network
API reference
See docs/api-reference.md. All public methods on OpenTargetsClient (and the matching AsyncOpenTargetsClient) have docstrings with Args, Returns, and Example blocks.
Contributing
- Fork the repo and create a feature branch.
- Install dev dependencies:
pip install -e ".[dev,all]" - Run tests:
pytest - Run linting + formatting:
ruff check src tests && ruff format src tests - Type check:
mypy src/opentargets - Open a pull request.
License
Apache 2.0 — see LICENSE.
Metadata
Release files for opentargets-py 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| opentargets_py-0.2.1.tar.gz | 55.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| opentargets_py-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 94.7 kB
Release files / opentargets_py-0.2.1.tar.gz
| Download URL | opentargets_py-0.2.1.tar.gz |
|---|---|
| Size | 55.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d6a5963c72dc5ebabaa6ec6b59b6b37e87512ad7291020d8d81e84965b0d82ee
|
|
BLAKE2b-256 checksum How to use checksums |
3bc71f38c97cc3cd878aacbbfc16d4b33da50eb25a6451d8673e29c755e003df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.8
|
Release files / opentargets_py-0.2.1-py3-none-any.whl
| Download URL | opentargets_py-0.2.1-py3-none-any.whl |
|---|---|
| Size | 39.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb625f49e362d57ca9ba0dc8fba30afe1452fa14acf08f7f8f448c9f50d79bc9
|
|
BLAKE2b-256 checksum How to use checksums |
5fa69a829641f68075fadc0bf49a26c8a1ffd65906f4e4022453a99c4ee3f0e0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.8
|
Security for opentargets-py 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.