Inferensys

Glossary

SELFIES

SELFIES (SELF-referencIng Embedded Strings) is a 100% robust molecular string representation where every syntactically valid string corresponds to a valid molecular graph, designed for generative models.
ML engineer managing model versions on laptop, version history visible, technical Git-like workflow.
MOLECULAR REPRESENTATION

What is SELFIES?

A 100% robust string-based molecular representation designed for generative deep learning models, where every syntactically valid string maps to a valid molecular graph.

SELFIES (SELF-referencIng Embedded Strings) is a molecular string representation engineered to guarantee 100% syntactic and semantic validity. Unlike SMILES, where random character mutations frequently produce invalid chemical graphs, every arbitrary sequence of SELFIES symbols—derived from a formal context-free grammar—decodes unambiguously into a valid molecule. This property makes SELFIES ideal for generative models like variational autoencoders and language models, which can freely explore the latent space without generating chemically nonsensical outputs.

The representation uses a derivation system where each symbol either encodes an atom, bond, or ring feature, or acts as a branching specifier. A dedicated [nop] (no operation) token allows the string to pad to arbitrary lengths without altering the molecular graph, enabling fixed-length inputs for neural networks. SELFIES inherently handles valence constraints, aromaticity, and explicit hydrogens, ensuring that decoded structures are always physically plausible and semantically consistent with the encoded molecule.

100% ROBUST MOLECULAR REPRESENTATION

Key Features of SELFIES

SELF-referencIng Embedded Strings (SELFIES) is a string-based molecular representation where every syntactically valid string corresponds to a valid molecular graph, eliminating invalid outputs entirely from generative models.

01

Guaranteed Semantic Validity

The defining property of SELFIES is 100% robustness: any string generated by a model is guaranteed to decode into a chemically valid molecular graph. This is achieved through a formal grammar where each token either adds an atom, adds a bond, or branches, and the derivation rules inherently enforce valence constraints. Unlike SMILES, where a single misplaced character produces an invalid string, SELFIES eliminates the need for post-hoc validity filters or rejection sampling during generation.

02

Self-Referencing Derivation Rules

SELFIES uses a context-free grammar with self-referencing constraints. The derivation state tracks the most recently derived atom, and bond tokens explicitly reference this atom to form connections. Key structural tokens include:

  • [Ring] and [Branch] tokens for cyclic structures and branching points
  • [=O] and [#N] for double and triple bonds to the last atom
  • [C], [N], [O] for atom specification This design ensures that bond formation always respects the current valence state of the referenced atom.
03

Designed for Generative Models

SELFIES was explicitly created to solve the invalid output problem in deep generative models like variational autoencoders (VAEs), generative adversarial networks (GANs), and language models applied to molecular design. In SMILES-based generation, up to 90% of sampled strings can be invalid, wasting compute and complicating training. SELFIES constrains the output space to only valid graphs, allowing models to explore chemical space more efficiently and enabling gradient-based optimization directly in string space without validity penalties.

04

Bijective Mapping to Molecular Graphs

There exists a surjective mapping from SELFIES strings to molecular graphs: every SELFIES decodes to exactly one graph, and every graph has at least one SELFIES representation. This property is critical for machine learning because it means the representation space has no "dead zones" of invalid strings. The encoding algorithm converts a graph into a SELFIES string by traversing the graph in a deterministic order, while the decoding algorithm reconstructs the graph by sequentially applying derivation rules. This formal guarantee distinguishes SELFIES from all predecessor string representations.

05

Comparison to SMILES and DeepSMILES

SELFIES addresses fundamental limitations of prior representations:

  • SMILES: Context-sensitive grammar with no validity guarantee; requires complex parsing and ring closure digit matching
  • DeepSMILES: Simplifies ring and branch syntax but remains non-robust; invalid strings still possible
  • SELFIES: Context-free grammar with formal validity guarantee; slightly longer strings than SMILES but zero invalid outputs
  • InChI: Hierarchical but not designed for generative modeling; lacks sequential token-by-token construction The trade-off is string length—SELFIES strings are typically 10-20% longer than equivalent SMILES—but this is negligible compared to the elimination of invalid sampling.
06

Integration with Deep Learning Frameworks

SELFIES is supported by a lightweight, open-source Python library (selfies) that provides:

  • selfies.encoder(): Convert SMILES to SELFIES
  • selfies.decoder(): Convert SELFIES back to SMILES
  • selfies.get_semantic_robustness(): Verify validity guarantees
  • selfies.split_selfies(): Tokenize SELFIES into constituent symbols for model input The library integrates seamlessly with DeepChem, RDKit, and standard PyTorch/TensorFlow pipelines. The token vocabulary is finite and known in advance, simplifying one-hot encoding or learned embedding layers for sequence models.
STRING REPRESENTATION ROBUSTNESS

SELFIES vs. SMILES: A Technical Comparison

A feature-by-feature comparison of SELFIES and SMILES for molecular string representation in generative AI workflows.

FeatureSELFIESSMILESDeepSMILES

100% Syntactic Validity

Semantic Validity Guarantee

Closed-Form Grammar

Human Readability

Canonicalization Support

String Length (avg. vs. SMILES)

1.5-2.5x longer

Baseline

1.1-1.4x longer

Branching Representation

Explicit bracket symbols

Parentheses

Ring-and-branch syntax

Ring Closure Encoding

Symbol-based state

Numeric ring indices

Numeric ring indices

Mutational Robustness

High (any mutation = valid molecule)

Low (most mutations = invalid)

Medium (fewer invalid mutations)

Generative Model Compatibility

VAE Latent Space Smoothness

Superior

Moderate

Improved over SMILES

Bidirectional Conversion to Graph

Stereochemistry Support

Adoption in Production Pipelines

Growing (2020+)

Ubiquitous (1988+)

Niche (2019+)

Standard Library Support (RDKit/CDK)

Partial (via conversion)

Full native

Minimal

SELFIES DEEP DIVE

Frequently Asked Questions

Clear answers to the most common technical questions about SELF-referencIng Embedded Strings and their role in robust generative chemistry.

A SELFIES (SELF-referencIng Embedded Strings) string is a 100% robust molecular representation where every syntactically valid string corresponds to a valid molecular graph, eliminating the syntactic fragility inherent in SMILES. The fundamental difference lies in the derivation rules: SMILES uses a context-free grammar that can produce invalid strings (e.g., unclosed rings or mismatched branches), requiring complex parsing and sanitization. SELFIES enforces semantic validity by design through a formal grammar where each symbol derives a specific graph operation. A SELFIES string is constructed by a sequence of tokens, each representing either an atom, a bond, a ring connection, or a branch. The derivation process maintains a derivation state that tracks the current atom index, ensuring that every ring closure or branch attachment references an existing atom. This guarantees that any random sequence of SELFIES tokens—such as those generated by a Variational Autoencoder (VAE) or Generative Adversarial Network (GAN)—unambiguously decodes to a valid molecular graph without post-hoc correction, making it ideal for de novo drug design and molecular optimization.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.