TL;DR
This paper introduces STED (Semantic Tree Edit Distance) and a consistency scoring framework to evaluate the reliability of structured data generated by Large Language Models (LLMs). Their method accurately distinguishes between functionally equivalent variations and critical structural errors in outputs like JSON, outperforming existing metrics and enabling better model selection and prompt engineering for production LLM applications.
What Problem Does This Paper Address?
LLMs are increasingly used to generate structured data (e.g., JSON for APIs, data extraction). However, ensuring the consistency and reliability of these outputs is a major hurdle. Traditional evaluation methods struggle because they either:
- Focus too much on exact matches: This fails to recognize semantically equivalent variations (e.g., "user_name" vs. "userName", different key orders in JSON objects).
- Ignore semantic meaning: They treat structural differences as critical errors, even when the data's meaning is preserved.
- Are order-sensitive: This is problematic for formats like JSON, where key order is irrelevant to structure and meaning.
These limitations lead to false negatives (flagging valid outputs as errors) or false positives (missing actual inconsistencies), causing issues in production systems. The paper aims to bridge this gap by providing a metric that understands both the structure and semantics of structured data.
Methods
The core of the paper is the STED (Semantic Tree Edit Distance) framework, which has two main components:
- Semantic Tree Edit Distance (STED): This is a novel similarity metric that treats structured data (like JSON) as trees. It extends traditional Tree Edit Distance (TED) by incorporating semantic understanding.
- Tree Representation: Structured data is converted into a tree format where nodes represent keys, values, and structural elements, preserving hierarchical relationships.
- Semantic Update Operations: STED defines "Insert," "Delete," and "Update" operations for tree nodes. The "Update" operation is key, as it considers both structural similarity (using embeddings) and semantic similarity of values. It also uses an Order-Invariant Matching approach (Hungarian algorithm) to ensure that the order of elements in arrays or keys in objects doesn't affect the similarity score.
- Multi-Level Similarity: It combines structural, key, value, and type similarities with configurable weights.
- Consistency Scoring Framework: This component aggregates multiple STED measurements from repeated LLM generations for the same input.
- Pairwise Similarity: STED calculates a similarity score between any two generated structured outputs.
- Global Consistency Score: These pairwise scores are then aggregated into a single score (0-1) that quantifies the LLM's overall reliability for a given task. This score is designed to be interpretable for production deployment.
Datasets
- Synthetic Datasets: The authors created a dataset of 2,400 synthetic test cases by systematically varying 80 base JSON samples. These variations fall into three categories:
- Schema Variation: Changes affecting parseability (e.g., flattening nested structures, changing field names).
- Expression Variation: Lexical changes preserving meaning (e.g., synonyms, paraphrasing).
-
Semantic Variation: Fundamental content changes altering meaning. These datasets are designed to test the metric's sensitivity to different types of variations.
-
LLM Benchmarking: The framework was used to evaluate six LLMs (Claude 3.7 Sonnet, Claude 3.5 Sonnet V2, Claude 3.5 Haiku, Claude 3 Haiku, Llama-3.3-70B, and Nova Pro) on structured output generation tasks.
Key Insights and Ideas
- Semantic Equivalence vs. Structural Integrity: The most crucial insight is the need to differentiate between variations that preserve meaning and structure (e.g., renaming a field semantically) and those that break the structure or meaning. Traditional metrics often fail at this distinction.
- JSON's Order-Agnostic Nature: The paper highlights how standard NLP metrics (like BERTScore) are ill-suited for JSON due to their inherent order sensitivity, leading to inaccurate similarity assessments. STED's order-invariant matching is a direct solution.
- Temperature's Impact on Consistency: LLM temperature settings significantly affect output consistency. Higher temperatures generally lead to more diverse but less consistent outputs, especially for semantic content.
- Structural Consistency is More Robust: Across various models and temperatures, structural consistency tends to be more stable than semantic consistency, suggesting that LLMs are generally better at maintaining format than precise content.
- Model Specialization: Different LLMs exhibit distinct consistency profiles. Some, like Claude-3.7-Sonnet, are highly consistent across the board, while others require careful tuning of temperature or prompts to achieve acceptable reliability.
Key Results
- STED Superiority: STED achieved superior performance on synthetic datasets, correctly assigning near-perfect similarity (0.86-0.90) to semantically equivalent variations and zero similarity to structural breaks, outperforming TED, BERTScore, and DeepDiff.
- Model Performance Variation: Benchmarking six LLMs revealed significant differences in consistency. Claude-3.7-Sonnet showed exceptional consistency, maintaining high structural reliability even at high temperatures (T=0.9). In contrast, Claude-3-Haiku and Nova-Pro showed substantial degradation.
- Temperature Effects: Most models showed declining consistency with increasing temperature. Claude-3.5-Haiku was the most sensitive (46% decline), while Claude-3.7-Sonnet was the most stable (19% decline).
- Schema Restructuring: STED demonstrated better robustness (-5.2% degradation) to field name changes compared to TED (-19.4%), while BERTScore showed exceptional robustness (-0.8%) due to its semantic understanding.
Limitations and Future Work
- JSON Focus: The current framework is primarily demonstrated for JSON. Future work could extend it to other structured formats like XML and HTML.
- Semantic Metric Limitations: The semantic similarity metrics used might not capture all domain-specific equivalences.
- Computational Cost: Evaluating consistency requires multiple generations and comparisons, which can be computationally expensive. Optimization of the evaluation process is suggested.
- Model and Sample Size: The evaluation focused on six models and a specific set of 75 base samples. Expanding the model coverage and dataset diversity would be beneficial.
- Broader Format Support: The authors suggest extending the framework to other structured formats and exploring more efficient evaluation methods.
Summary updated