arXiv:2610.41511cs.AIOct 2026

Manuscript Offprint

arXiv:2610.41511

Specification-Guided Generation for Large Language Models

Alessandro Previti, Konstantinos Vandikas, Mario Alviano, Mats Carlsson, Albin Larsson Forsberg, Alexandros Nikou, Athanasios Karapantelakis, Aneta Vulgarakis

Abstract

Large language models (LLMs) can generate structured outputs, but existing constrained-decoding methods mainly enforce syntax and cannot directly enforce satisfaction of a formal specification. We introduce a neuro-symbolic, specification-guided decoding framework that combines LLM generation with symbolic reasoning. At each step, the framework refines the decoding grammar by filtering out values that cannot be extended to an output satisfying the specification. We present a forward-only variant that guarantees specification satisfaction by construction, and a backtracking variant that revises earlier feasible choices when their downstream consequences leave only values that the LLM itself judges unsuitable. The framework is model-agnostic, requires no retraining, and builds on existing grammar-constrained decoding mechanisms. Experiments on configuration and constraint-solving benchmarks show that the forward-only variant reliably produces specification-compliant outputs while reducing repeated LLM inference compared with rejection sampling and incurring modest symbolic-reasoning overhead. The backtracking variant further reduces cases in which the model is forced to select values that it has itself judged unsuitable, thereby substantially improving adherence to the LLM's stated interpretation of the prompt.

Introduction

The success of Large Language Models (LLMs) (Brown et al. 2020; Touvron et al. 2023; Anil et al. 2023; Guo et al. 2025) has motivated constrained-generation methods (Poesia et al. 2022; Beurer-Kellner, Fischer, and Vechev 2024; Park et al. 2024), which restrict decoding so that only tokens preserving syntactic validity are admissible. These methods guarantee conformity to regular expressions, context-free grammars, or structured schemas without model retraining, making them useful for applications such as API generation, information extraction, code synthesis, and tool invocation.

However, existing constrained-decoding methods are limited to syntactic structure and cannot directly enforce a formal specification expressed as logical constraints. Here, a formal specification is a mathematically precise description of the properties that every admissible output must satisfy. As a result, generated outputs may be syntactically valid while still

violating logical relationships and dependencies among variables. Such guarantees are increasingly important as LLMs are used for monitoring, configuration, and decision-making in domains including telecommunications, software systems, healthcare, and finance (Li et al. 2025b,a; Cai et al. 2025; Wu, Barrett, and Narodytska 2024; Singhal et al. 2023; Liu et al. 2023, 2024).

We introduce specification-guided decoding, which integrates a symbolic reasoner directly into LLM generation. Before each assignment, the reasoner computes the values that remain compatible with the specification and restricts decoding to those values. Consequently, every completed output satisfies the formal specification by construction. Unlike approaches that translate natural-language requirements into formal constraints (Ye et al. 2023; Wu, Barrett, and Narodytska 2024; Zhang et al. 2025; Pan et al. 2023), we assume that the specification is provided as input, analogously to the predefined grammar or schema required by structured-output tools. Our approach is therefore complementary to specification-synthesis methods, is model-agnostic, requires no retraining, and builds on existing constrained-decoding mechanisms.

The LLM and the symbolic reasoner have complementary roles. The specification captures hard requirements, while the prompt expresses the user's goals and preferences within the feasible space. The reasoner guarantees validity, whereas the LLM guides the choice among potentially many valid outputs toward one that better reflects the prompt. The LLM's preferences over feasible values are context-dependent and revealed only during autoregressive generation, so they cannot be fully formalized in advance. Moreover, the constrained variables may form only part of a broader grammar-guided output, and their preferred values may depend on surrounding content generated during decoding. Solving the specification independently would therefore neither preserve these context-dependent choices nor replace the overall generation process. Our procedure instead preserves the model's native generation process as much as possible: similarly to structured-output decoding, it intervenes only to exclude infeasible choices while otherwise following the model's conditional distribution.

Figure 1 illustrates the distinction. Existing schema-

*Corresponding author

class LaptopConfig(BaseModel):
    CPU: Literal['i5', 'i7', 'i9']
    RAM: Literal['ram8', 'ram16', 'ram32']
    GPU: Literal['integrated', 'dedicated']
    HARD_DISK: Literal[256, 512, 1024]
    BATTERY: Literal['standard', 'extended']

Constraints:
1. If GPU is Dedicated, Battery must be Extended.
2. If CPU is an i7 or i9, RAM must be at least 16GB.
3. If RAM is 16GB or more, Battery must be Extended.

Figure 1: A JSON schema expressed as a Pydantic model, with additional relational constraints.

constrained tools (Lundberg, Ribeiro et al. 2023; Gerganov 2023; Willard and Louf 2023) can ensure that each laptop parameter takes an allowed value, but cannot enforce dependencies among parameters. For example, CPU = i7, RAM = ram8, GPU = dedicated, HARD_DISK = 512, and BATTERY = standard is syntactically valid, yet violates both the CPU–RAM and GPU–battery constraints. Our method prevents such assignments while retaining the LLM's ability to interpret a high-level request, such as generating a gaming laptop configuration.

The same setting arises in information extraction and structured decision-making tasks such as product configuration, software feature selection, scheduling, routing, and package management, where outputs must satisfy relationships among variables.

Main Contributions We make the following contributions:

  • We introduce a model-agnostic specification-guided decoding framework that integrates symbolic reasoning into LLM generation without retraining.

  • We develop a forward-only procedure that restricts each assignment to feasible values, guaranteeing specification satisfaction by construction without post-hoc validation or trial-and-error generation.

  • We introduce a backtracking extension that revises earlier assignments when their consequences conflict with the model's interpretation of the prompt, while preserving specification compliance.

  • We evaluate both methods on configuration and constraint-solving benchmarks. Forward decoding reduces repeated LLM inference with modest reasoning overhead, while backtracking resolves many prompt conflicts and improves adherence to the model's stated preferences.

References (29)