Tree-Structured Code Generation for Complete LLM Output

Resolve Bottlenecks,
Find Innovative Solutions
Generate Solutions

Solution Overview

Problem

Generative machine learning models like LLMs often produce output that lacks definitive end points, leading to nonsensical, repetitive, or incomplete code due to token limitations and computational constraints, which affects the quality of code predictions.

Innovation Solution

The method involves training a machine learning model using a tree structure of code snippets to identify logical end tokens and trim output to ensure sensible and complete code generation, leveraging techniques such as abstract syntax trees and post-facto probability analysis to refine LLM outputs.

Engineering Contradictions & Design Principles

VSEngineering Contradiction Analysis

1Productivity

If LLM output is truncated due to token limits or computational constraints, then processing efficiency is improved, but code completeness and logical coherence deteriorate

Engineering Contradiction:
Improveprocessing efficiencyVSAvoidcode completeness
Core Design Contradiction:
ProductivityVSReliability

Solution Approach 1:

The system performs preliminary identification of logical end tokens in the training data before model generation. By pre-processing the code data to mark definitive endpoints (such as after complete statements, functions, or blocks), the model learns to anticipate where code should naturally end, enabling it to produce complete code segments even when truncated by token limits without requiring post-processing corrections

Inventive Principle:
Principle #10Preliminary action

2Speed

If sampling strategies are used to generate LLM output, then generation speed is improved, but the likelihood of missing end tokens increases

Engineering Contradiction:
Improvegeneration speedVSAvoidend token accuracy
Core Design Contradiction:
SpeedVSMeasurement precision

Solution Approach 1:

The system incorporates feedback mechanisms by training the model on data where end tokens are explicitly marked and by using the identified logical endpoints to guide generation. The model receives feedback during training about where code should end, allowing it to adjust its probability distributions to favor end tokens at appropriate locations, thereby maintaining end token accuracy even when using sampling strategies for fast generation

Inventive Principle:
Principle #23Feedback

3Adaptability or versatility

If LLM generates continuous output without definitive end tokens, then output length flexibility is improved, but code logical coherence and non-repetition deteriorate

Engineering Contradiction:
Improveoutput length flexibilityVSAvoidcode logical coherence
Core Design Contradiction:
Adaptability or versatilityVSReliability

Solution Approach 1:

The system performs preliminary analysis of code structure during training to identify logical endpoints before generation occurs. By pre-marking where code segments should end in the training data and teaching the model to recognize these patterns, the system enables the model to generate flexible-length output that naturally terminates at logically coherent points, preventing repetition and maintaining code integrity without rigid length constraints

Inventive Principle:
Principle #10Preliminary action

Data Source

PatentEP4589421A1Improved code generation using tree structure for training and trimming of generative model output
Publication Date: 2025.07.23 GOOGLE LLC
  • EP4589421A1 patent drawingFigure 1
  • EP4589421A1 patent drawingFigure 2
  • EP4589421A1 patent drawingFigure 3

AI summary

A method implemented using one or more processors comprises selecting a starting location in an original code snippet, processing the original code snippet to generate a tree representation of the original code snippet, identifying a subtree of the tree representation that contains the starting location in the original code snippet, identifying a ground truth portion of the original code snippet that corresponds to at least a portion of the subtree of the tree representation, and training a machine learning model to generate a predicted code snippet that corresponds to the portion of the subtree. The training includes processing a remainder of the original code snippet outside of the ground truth portion using the machine learning model.