Skip to main content

ChemGlyph

Publication-quality chemical structure and reaction rendering for AI agents. ChemGlyph is the KaTeX of chemistry: a rendering layer, a validation layer, and an MCP interface on top of RDKit.

CI

Install

pip install chemglyph

Render a molecule

import chemglyph

result = chemglyph.render_molecule("c1ccccc1")  # benzene
open("benzene.svg", "w").write(result.data)

render_molecule takes SMILES, InChI, or molblock and returns SVG (or PNG) plus canonical_smiles, mol_formula, mol_weight, and warnings.

Styles

Three styles, same molecule (benzoic acid, caffeine, (S)-ibuprofen):

ChemGlyph style gallery

chemglyph.render_molecule(smiles, style="acs")  # black/white, ACS journal
chemglyph.render_molecule(smiles, style="modern")  # colored heteroatoms, screens
chemglyph.render_molecule(smiles, style="textbook-cn")  # bold monochrome, textbook

All styles default to a transparent background (transparent=True) and support fmt="png".

Reactions

spec = {
    "steps": [
        {
            "reactants": ["OC(=O)c1ccccc1O", "CC(=O)OC(C)=O"],
            "products": ["CC(=O)Oc1ccccc1C(=O)O", "CC(=O)O"],
            "conditions": {"above": "H₂SO₄ (cat.)", "below": "rt, 15 min"},
            "yield": "89%",
            "arrow": "forward",
        }
    ],
    "style": "modern",
}
svg = chemglyph.render_reaction(spec)

Conditions are pre-formatted Unicode text, so pass H₂SO₄, not H2SO4. ChemGlyph does not parse formulas out of text. The full schema (multi-step chains, equilibrium and retro arrows, line wrapping) is in docs/reaction_schema.md.

The aspirin demo writes a two-step route:

python examples/aspirin_synthesis.py  # writes examples/aspirin_synthesis.svg

Validation

validate_structure reports parse errors and applies four quick fixes: unmatched brackets and ring closures (reported, not guessed), kekulization failures of lowercase aromatic atoms, and nitrogen valence errors via a formal [N+]. Anything else passes RDKit's message through unchanged.

report = chemglyph.validate_structure("c1cccc1")
report.fixes[0].description  # 'lowercase aromatic atoms could not be kekulized...'
report.fixes[0].fixed_smiles  # 'C1CCCC1'

Naming

chemglyph.parse_name("aspirin")  # 'CC(=O)Oc1ccccc1C(=O)O'

English IUPAC and common names resolve offline through OPSIN (pip install 'chemglyph[opsin]', plus a Java runtime). Chinese names use the built-in dictionary, and the library API accepts a translator callable for names that are not in it:

chemglyph.parse_name("阿司匹林")  # 'CC(=O)Oc1ccccc1C(=O)O'
chemglyph.parse_name("六甲基苯", translator=to_english)

ChemGlyph itself never calls an online service, including for translation.

MCP server

Run the bundled console script (stdio transport):

chemglyph-mcp

Claude Desktop registration (macOS: ~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "chemglyph": {
      "command": "chemglyph-mcp"
    }
  }
}
Tool Use it when Returns
render_molecule the user asks to draw one structure from SMILES/InChI/molblock SVG or PNG image plus formula, MW, warnings
render_reaction the user asks for a reaction or synthesis route reaction SVG image
validate_structure a SMILES may be malformed and you need a repair validation report JSON
parse_name the user gives a name like "aspirin" instead of SMILES canonical SMILES or an error

Benchmarks

benchmarks/ holds the fixed 20-molecule blind test and a generator that writes shuffled, numbered PNG/SVG figures plus answer_key.json:

python benchmarks/generate_blind_test.py --seed 1234

Pass criteria: two or three chemical practitioners blind-pick the figures they would publish; ChemGlyph passes at 40% or higher. Ferrocene and the free-base porphyrin are excluded from the denominator and recorded as known limitations. The procedure is documented in benchmarks/RUNBOOK.md.

ChemGlyph vs RDKit default

Blind test vs ChemDraw: pending. The image above compares ChemGlyph modern with RDKit's stock output; ChemDraw panels are added by hand during the review, and the image is regenerated afterwards.

Roadmap

  • v0.2: Chinese naming (built-in dictionary plus translator hook), down-arrow line wrapping, arrow column alignment, cropped fragments. All shipped.
  • Next: mechanism (electron-pushing) arrows, see docs/progress/v02-research.md.
  • Later: a larger Chinese dictionary as an optional data extra.

Non-goals

No structure editor GUI (Ketcher/ChemDraw competition), no 3D visualization, no retrosynthesis or property prediction, no online database queries, and no automatic mechanism generation. The full list is in the project specification.

Development

python -m venv .venv
.venv/bin/pip install -e ".[dev]"
.venv/bin/ruff check . && .venv/bin/ruff format . && .venv/bin/pytest

Python 3.11+, RDKit 2024.9+, MIT license. All errors derive from chemglyph.errors.ChemGlyphError.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chemglyph-0.1.1.tar.gz (278.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chemglyph-0.1.1-py3-none-any.whl (28.3 kB view details)

Uploaded Python 3

File details

Details for the file chemglyph-0.1.1.tar.gz.

File metadata

  • Download URL: chemglyph-0.1.1.tar.gz
  • Upload date:
  • Size: 278.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.9

File hashes

Hashes for chemglyph-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f4a88060d51e03e60ce703ff0c27c1ff25d357396ddc5da5f7069604c456178d
MD5 9894a9d8420ad6ec467c702d331d28f6
BLAKE2b-256 fd061d3ddbd11cf2fbf41ce15a337fad812774f0d0b5ea0234e3f225483ce672

See more details on using hashes here.

File details

Details for the file chemglyph-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: chemglyph-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 28.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.9

File hashes

Hashes for chemglyph-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 39408dd4887b36b1e3df213ab0bfedd0f4ec6ce47f7143b4c108553718fb3be6
MD5 5a7377c6259daa429365ed1e254657cd
BLAKE2b-256 3d92ecda2f131d1348952609ef2f01dbdaa2df1cc31e016212037ac86bfba0a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page