arXiv:2610.29749cs.SEOct 2026

Manuscript Offprint

arXiv:2610.29749

Metamodel-Guided LLM Decoding for Consistent DSL Generation

Boqi Chen, Aren A. Babikian, José Antonio Hernández López

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Boqi Chen ID^{ID}, Aren A. Babikian ID^{ID}, José Antonio Hernández López ID^{ID}

Abstract—Large language models (LLMs) are increasingly used to generate software engineering artifacts written in domain-specific languages (DSLs). Correct DSL instances usually must comply with not only the concrete syntax but also the abstract syntax defined by a metamodel augmented with semantic well-formedness constraints. Existing constrained decoding approaches typically guarantee compliance only with concrete syntax or with a limited set of semantic constraints expressed over the generated text, making metamodel-level constraint consistency difficult to enforce during generation. We present MeMoDecode: a decoding framework that enforces a DSL's concrete syntax, abstract syntax, and well-formedness constraints jointly. Specifically, whenever the LLM generates a complete metamodel concept, MeMoDecode incrementally maps the current output to a partial model and verifies whether it can still be completed into a consistent instance. We combine MeMoDecode with a simple retry mechanism that resumes decoding from the most recent completable snapshot whenever an inconsistency is detected. Under a sound backend verifier, MeMoDecode never performs worse than grammar-constrained decoding on alignment metrics satisfying natural assumptions. Across four DSLs and six open-weight LLMs ranging from 0.6B to 70B parameters, MeMoDecode achieves the highest consistency, improving consistency by 23% on average over unconstrained decoding and by 8% on average over grammar-constrained decoding. It also improves benchmark performance while incurring only modest runtime overhead. These results show that metamodel-guided decoding is an effective approach for enforcing DSL consistency during decoding across diverse DSLs and LLM scales.

Index Terms—constrained decoding, domain-specific languages, large language models, metamodels

I. INTRODUCTION

Context. Modern software engineering (SE) relies on a wide range of artifacts, from architecture design and configuration to query languages and flowcharts, many of which are expressed in domain-specific languages (DSLs) tailored to a particular task or domain [1]. As large language models (LLMs) are increasingly used to automate SE activities, a growing portion of these artifacts is generated, either fully or partially, by LLMs, from natural language specifications [2], [3]. Beyond traditional SE workflows, DSL artifacts also serve as structured interfaces in LLM-based agentic systems, supporting multi-agent communication [4]–[6] and tool use [7]–[9].

In SE, especially in model-driven engineering, DSL instances are often used as machine-processable artifacts for downstream automation, analysis, model transformation, and code generation [10]–[12]. As a result, a DSL is typically characterized by three complementary aspects: its concrete syntax, usually defined by a grammar; its abstract syntax, captured by a metamodel; and a set of semantic well-formedness constraints over that metamodel, with execution semantics defined on top of them [13]–[15]. These aspects form a hierarchy of correctness. A generated instance may satisfy the grammar while still violating the metamodel or its constraints, preventing meaningful analysis, transformation, or execution. A DSL instance is consistent if it conforms to both the metamodel and the well-formedness constraints [16]. However, LLMs provide no guarantee that generated DSL instances satisfy these specifications. They may produce outputs that violate the grammar, the metamodel, or the well-formedness constraints, motivating the need for verification techniques [17]–[19].

A natural way to improve the reliability of generated DSL instances is post-hoc verification, where a complete instance is first generated and then checked or repaired [20]–[23]. However, these approaches discover violations only after a full generation. As a result, they may waste substantial decoding effort and require additional repair steps. Moreover, as LLM generation is conditioned on existing prefixes, when the generated instance contains early structural errors, subsequent parts of the output are generated from an already invalid context, potentially hurting the overall quality [24], [25].

These limitations motivate decoding-time approaches that prevent invalid outputs before they are completed. Grammar-constrained decoding masks, at each decoding step, tokens that would violate the grammar, thereby guaranteeing syntactic validity [26]–[31]. However, operating only on concrete syntax, it provides no guarantee that the generated DSL instance conforms to the metamodel or the associated constraints.

Recent approaches further incorporate semantic constraint verification into the decoding process [32]–[34]. For example, IterGen [35] augments grammar-constrained decoding with semantic checks. However, these checks are typically expressed over the generated text rather than the underlying structured model. Consequently, verification remains tied to a particular textual representation. More importantly, it limits the ability to reason about properties that are naturally defined over objects and references in the metamodel, such as multiplicity constraints, referential integrity, and transitive relationships. As a result, decoding-time verification and guidance of metamodel-level constraint consistency over the underlying structured representation remain underexplored [19].

Approach. In this paper, we present MeMoDecode (MetaModel-guided Decoding), a decoding framework that

Boqi Chen is with the University of Ottawa, Ottawa, Canada (email: boqi.chen@uottawa.ca)

Aren A. Babikian is with McGill University, Montreal, Canada (email: aren.babikian@mcgill.ca)

José Antonio Hernández López is with the University of Murcia, Murcia, Spain (email: joseantonio.hernandez6@um.es) simultaneously enforces a DSL's concrete syntax, abstract syntax, and semantic well-formedness constraints. The key idea is to lift verification from the generated text to the underlying model representation. As decoding proceeds, MeMoDecode incrementally maps the generated text to a partial model and, whenever the LLM completes a metamodel concept, verifies whether that partial model remains satisfiable with respect to the metamodel and its well-formedness constraints, that is, whether it can still be completed into a consistent instance. If not, the violation is irreparable, and MeMoDecode can immediately backtrack and retry during decoding rather than relying on post-hoc regeneration of the entire output.

Contribution. Because verification is performed over the model representation rather than the concrete-syntax text, MeMoDecode can enforce structured metamodel-level properties that are difficult or impossible to express using grammar-based approaches. Furthermore, since constraints are specified directly over the metamodel, the verification process is independent of the concrete syntax of the DSL and can be applied across different textual representations. More specifically, this paper makes the following contributions:

• We present MeMoDecode, a decoding framework that simultaneously enforces a DSL's concrete syntax, abstract syntax, and semantic well-formedness constraints by verifying whether partially generated outputs can still be completed into consistent instances and retrying otherwise.

• We establish a worst-case guarantee: under a sound verifier and natural assumptions on the alignment metric, MeMoDecode never produces an output that is less aligned than the one generated by grammar-constrained decoding under identical decoding conditions.

• We implement MeMoDecode by combining grammar-constrained decoding [27] with the Refinery verifier [36] and a lightweight decoding-time retry mechanism.

• We conduct a systematic evaluation across four DSLs and six open-weight LLMs ranging from 0.6B to 70B parameters. The results show that MeMoDecode consistently improves DSL consistency, increases alignment with the ground truth, and incurs only modest runtime overhead.

Added value. MeMoDecode demonstrates that reasoning over the underlying model representation is key to ensuring consistency of a DSL instance. By lifting verification to the metamodel level, it enables the use of established partial modeling and constraint-solving techniques within the decoding process, yielding consistency guarantees independent of the textual representation. Since verification is decoupled from the underlying LLM, MeMoDecode can benefit directly from future advances in both LLMs and backend verifiers.

References (67)