Minimal MolJSON schema and RDKit conversion helpers.
Project description
MolJSON
This repo contains the MolJSON structured-output JSON schema and related scripts. The MolJSON schema was designed to enable Large Language Models to interpret and emit molecular structures with higher accuracy.
Installation
Install the latest release from PyPI:
python -m pip install moljson
For local development, install the repository in editable mode:
git clone https://github.com/oxpig/MolJSON.git
# or: git clone git@github.com:oxpig/MolJSON.git
cd MolJSON
python -m pip install -e .
Quick Start
from rdkit import Chem
from moljson import GetSchema, MolToJSON, MolFromJSON, CheckRoundTrip
# 1) Get MolJSON schema
schema = GetSchema()
# 2) RDKit -> MolJSON
mol = Chem.MolFromSmiles("c1c[nH]cc1")
moljson = MolToJSON(mol) # default atom IDs: C1, C2, N1, ...
# 3) MolJSON -> RDKit
mol2 = MolFromJSON(moljson)
# 4) Round trip check
ok, in_smiles, out_smiles, rt_json = CheckRoundTrip(mol)
print(ok, in_smiles, out_smiles)
Example Notebooks
A simple walkthrough of the MolJSON functions can be found in examples/walkthrough.ipynb. This shows:
- Loading and printing the schema
- RDKit -> MolJSON conversion
- MolJSON -> RDKit conversion
- Round-trip checks
For a minimal OpenAI API example see examples/openai_moljson_example.ipynb.
For a minimal Anthropic API example see examples/anthropic_moljson_example.ipynb.
OpenAI API Example
import json
from openai import OpenAI
from moljson import GetSchema
client = OpenAI() # uses OPENAI_API_KEY
schema = GetSchema()
response = client.responses.create(
model="gpt-5-nano",
reasoning={"effort": "low"},
input="Convert the molecule from SMILES to MolJSON: CCO",
text={
"verbosity": "low",
"format": {
"type": "json_schema",
"name": "MolJSON",
"strict": True,
"schema": schema,
},
},
store=False,
)
moljson = json.loads(response.output_text)
print(moljson)
Format Notes
By default MolToJSON will omit the charges and aromatic_n_h fields if they are not required for the molecule. If you want to keep these fields use MolToJSON(mol, include_empty_fields=True).
To ensure compatibility with both the OpenAI and Anthropic APIs, the MolJSON schema provided in this repo has been slightly modified. The original schema used in the paper can be found in schemas/paper_moljson.schema.json. The Anthropic API currently does not support minimum/maximum integer ranges, so the fields inside charges and aromatic_n_h now use an enumeration of integers. This is functionally equivalent and should not impact performance. Additionally, the original schema used in the paper allowed aromatic_n_h.hcount values up to 2; this has now been corrected so that only hcount = 1 is allowed.
Citation
If you use MolJSON in your work, please cite:
@article{runcie2026moljson,
title = {Molecular Representations for Large Language Models},
author = {Runcie, Nicholas T. and Imrie, Fergus and Deane, Charlotte M.},
year = {2026},
journal = {arXiv preprint arXiv:2605.01822},
doi = {10.48550/arXiv.2605.01822},
url = {http://arxiv.org/abs/2605.01822},
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file moljson-0.1.0.tar.gz.
File metadata
- Download URL: moljson-0.1.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce959d66c60c691b7c4acbfebbf977d3effb738a7f05f59b1b5f4f5df33fd5af
|
|
| MD5 |
b02b61077f02b73405bf53382ccf0e6b
|
|
| BLAKE2b-256 |
f78b1e1a79f207d7eb9407430a812a42642b2656c28faf1a1885c2ac8077b114
|
File details
Details for the file moljson-0.1.0-py3-none-any.whl.
File metadata
- Download URL: moljson-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5e0cfc31628b08925d689ab6639ca451233c8fcbbec9dc67efa20bbcc205b62
|
|
| MD5 |
c5fede0e9dd001786a9d5671fea426b8
|
|
| BLAKE2b-256 |
03b961076647b842ae822b1f9f85e531f5bf8220e9b21db31e31c938209a4d2e
|