Abstract
Large Language Models (LLMs) often struggle with structured Domain-Specific Languages (DSLs) like Ansible YAML or Bash, frequently hallucinating invalid syntax or flags. We present DocCGen, a neuro-symbolic framework that leverages library documentation to constrain LLM decoding. By combining information retrieval with constrained generation, DocCGen ensures that generated code adheres to strict schema rules, significantly improving performance on both in-domain and out-of-domain tasks.
Introduction
While LLMs excel at general-purpose programming, they often fail when tasked with structured DSLs. These languages require adherence to strict schemas and specific keyword dependencies. In enterprise environments, a single hallucinated flag in a Bash command or a misplaced key in a YAML file can lead to critical system failures. Existing methods like fine-tuning or standard Retrieval-Augmented Generation (RAG) are insufficient as models may still ignore retrieved documentation, leading to the generation of non-existent flags or incorrect structures.
Methodology: The DocCGen Framework
DocCGen addresses these challenges through a two-phase process: Information Retrieval (IR) and Constrained Generation.
- Information Retrieval (IR): Given a user query, the system utilizes a retriever (such as BM25 or ColBERTv2) to identify the most relevant library documentation and schema rules from a pre-indexed pool.
- Constrained Generation: This phase employs Constrained Decoding (CD) to guide the LLM. The system creates a template based on the retrieved documentation. As the model generates code, specific "trigger signals" (such as a hyphen in Bash or indentation in YAML) prompt a lookup of valid keywords or flags allowed by the schema at that position. The system then applies logit masking, setting the probability of invalid tokens to negative infinity, thereby forcing the model to select only schema-valid options.
Experimental Setup and Datasets
The framework was evaluated on two diverse structured languages:
- Ansible YAML: A new benchmark dataset curated from Google BigQuery and Ansible Galaxy, containing over 18,000 samples across 2,500+ modules.
- Bash Command: Utilized TLDR and NL2Bash datasets, augmented with detailed schema information scraped from Linux man-pages.
Evaluation included both In-Domain (ID) and Out-of-Domain (OOD) settings to test the model's ability to handle entirely unseen libraries, a critical requirement for enterprise software automation.
Results and Discussion
DocCGen demonstrated substantial improvements across all metrics, particularly in challenging OOD scenarios where models have no prior exposure to the specific library syntax.
As shown in the experimental results, StarCoder2 7B's "Schema Correct" score for Ansible YAML (OOD) increased from a baseline of 5.16% to 57.00% when using the DocCGen framework. Similarly, Bash Command OOD performance saw significant gains in Token F1 scores, outperforming previous state-of-the-art methods. These results highlight that documentation serves as a reliable "source of truth" for low-resource DSLs. By providing the schema at inference time, DocCGen allows models to generate valid code for tools they did not encounter during pre-training.
Conclusion and Future Work
DocCGen provides a robust path toward reliable AI-generated infrastructure-as-code by bridging the gap between neural intent understanding and symbolic structural correctness. While the system currently faces a retrieval bottleneck—where incorrect documentation leads to incorrect constraints—and requires some manual schema preparation, it drastically reduces syntactic hallucinations. Future research will focus on automating schema extraction directly from raw documentation using LLMs to further scale the system's applicability.
Summary updated