Skip to content

Fix LLM output format inconsistency through prompt engineeringΒ #48

@cubxxw

Description

@cubxxw

πŸ› Problem Description

LLM outputs are generating inconsistent JSON formats, causing frontend display issues:

Current Problematic Formats

// Format 1: recommendations wrapper (current issue)
{"recommendations": [{"type": "deepen", "title": "...", "description": "..."}]}

// Format 2: options wrapper  
{"type": "deepen", "options": [{"title": "...", "description": "..."}]}

// Format 3: direct array
[{"type": "deepen", "title": "...", "description": "..."}]

Expected Format

{"type": "deepen", "content": "...", "describe": "..."}
{"type": "next", "content": "...", "describe": "..."}

πŸ” Root Cause Analysis

Technical Analysis Confirms:

  • βœ… Parsing logic works correctly (tested with all formats)
  • βœ… Data flow from splitContentAndOptions β†’ mergeOptions β†’ setOptions is functional
  • ❌ Root Issue: Inconsistent LLM output formats requiring continuous technical fixes

πŸ“‹ Proposed Solution: Prompt Engineering Standardization

Strategy: Fix at the Source

Rather than maintaining complex parsing logic for every possible format variation, standardize the LLM output through improved prompt engineering.

Implementation Plan

1. Update nextStepJsonl.system.zh.j2

  • Add strict format constraints
  • Remove ambiguous instructions
  • Provide clear positive/negative examples
  • Enforce field name consistency

2. Enhanced Validation

  • Add prompt validation examples
  • Test with multiple LLM models
  • Ensure consistent output quality

3. Gradual Rollout

  • Test new prompt extensively
  • Maintain backward compatibility during transition
  • Monitor success rates

βœ… Expected Benefits

  • Stability: Eliminate format inconsistency at source
  • Maintainability: Reduce technical debt from format-specific parsing
  • Performance: Simpler parsing logic = faster processing
  • Reliability: Predictable output format = fewer edge cases

πŸ”§ Technical Implementation

Files to Update

  • src/prompt/nextStepJsonl.system.zh.j2 - Primary prompt template
  • src/prompt/nextStepJsonl.system.en.j2 - English equivalent
  • Add validation examples and test cases

Validation Criteria

  • βœ… LLM outputs exactly 6 lines of valid JSONL
  • βœ… Each line is a complete JSON object with correct fields
  • βœ… No wrapper objects or arrays
  • βœ… Consistent field names (content/describe, not title/description)

🎯 Success Metrics

  • Zero format parsing failures after implementation
  • Consistent 6 options output per generation
  • No need for format-specific technical patches

πŸ“… Timeline

  • Phase 1: Update prompt templates (2-3 hours)
  • Phase 2: Extensive testing with multiple models (1 day)
  • Phase 3: Production deployment and monitoring (1 day)

πŸ”— Related Issues

This addresses the recurring pattern of LLM format inconsistencies and provides a sustainable long-term solution.


Priority: High
Type: Enhancement/Bug Fix
Component: Prompt Engineering, LLM Integration

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions