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.
Glossary
SELFIES

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.
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.
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.
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.
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.
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.
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.
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.
Integration with Deep Learning Frameworks
SELFIES is supported by a lightweight, open-source Python library (selfies) that provides:
selfies.encoder(): Convert SMILES to SELFIESselfies.decoder(): Convert SELFIES back to SMILESselfies.get_semantic_robustness(): Verify validity guaranteesselfies.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.
SELFIES vs. SMILES: A Technical Comparison
A feature-by-feature comparison of SELFIES and SMILES for molecular string representation in generative AI workflows.
| Feature | SELFIES | SMILES | DeepSMILES |
|---|---|---|---|
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 |
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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
SELFIES exists within a broader landscape of molecular string representations and generative chemistry tools. Understanding these related concepts is essential for selecting the right representation for your molecular machine learning pipeline.
DeepSMILES
A syntactic adaptation of SMILES designed to improve generative model performance by simplifying the grammar. DeepSMILES eliminates explicit ring closure digits and replaces them with a single ring closure symbol, reducing the complexity of the syntax tree. While this improves validity rates over standard SMILES, it still does not guarantee 100% robustness like SELFIES.
- Key innovation: Simplified ring and branching syntax
- Validity improvement: Reduces invalid outputs but not to zero
- Trade-off: Less human-readable than standard SMILES
Molecular Graphs
The native mathematical representation of molecules as graphs where atoms are nodes and bonds are edges. Graph Neural Networks (GNNs) operate directly on this representation, bypassing string encodings entirely. While SELFIES converts graphs to strings for sequence-based models, GNNs preserve topological information natively and achieve state-of-the-art results in property prediction.
- Advantage: No information loss from string conversion
- Key architectures: Message Passing Neural Networks, Graph Attention Networks
- Complementary: SELFIES can be used alongside graph representations in hybrid models
Molecular Fingerprints
Fixed-length binary or integer vectors encoding the presence or absence of specific substructural features. Unlike SELFIES, fingerprints are lossy representations optimized for similarity searching and quantitative structure-activity relationship (QSAR) modeling. Common types include ECFP4 (circular fingerprints based on Morgan algorithm) and MACCS keys (predefined structural keys).
- Dimensionality: Typically 1024–2048 bits
- Key use: Tanimoto similarity for virtual screening
- Contrast with SELFIES: Lossy vs. lossless; fixed-length vs. variable-length
Molecular Grammar
The formal syntactic rules that define how molecular string representations are constructed and parsed. SELFIES uses a context-free grammar where every production rule guarantees chemical validity — a property known as semantic closure. This contrasts with SMILES, whose grammar requires complex semantic checks to validate the resulting molecular graph.
- Key concept: Formal language theory applied to chemistry
- SELFIES grammar: ~100 production rules covering organic chemistry
- Benefit: Enables formal verification of generative model outputs

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us