Sentence truncation fallback

From wenmin-wu/ds-skills · View interactive version

Sentence truncation fallback is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Truncate LLM output to exactly N sentences and fall back to a known-good baseline string when output is empty or too short

How to use this skill

Skill file

Sentence Truncation with Fallback

Overview

LLM outputs for structured tasks (prompt recovery, classification rationale, single-line answers) often include unwanted preamble, repetition, or rambling. Truncate to the first N sentences, then check length — if the result is too short or empty, substitute a well-scoring baseline string. This two-step post-processing ensures consistent submission quality.

Quick Start

import re

BASELINE = "Improve the following text using a more formal and academic tone while maintaining the original meaning"

def truncate_sentences(text, max_sentences=1):
    sentences = re.split(r'(?<=[.!?])\s+', text.strip())
    result = ' '.join(sentences[:max_sentences])
    return result

def postprocess(raw_output, min_length=15):
    cleaned = truncate_sentences(raw_output, max_sentences=1)
    cleaned = cleaned.strip().strip('"').strip("'")
    if len(cleaned) < min_length:
        return BASELINE
    return cleaned

predictions = [postprocess(output) for output in raw_outputs]

Workflow

  1. Split raw LLM output by sentence boundaries
  2. Keep only the first N sentences (typically 1 for single-prompt tasks)
  3. Strip quotes, whitespace, and formatting artifacts
  4. Check minimum length — outputs under threshold are likely garbage
  5. Replace short/empty outputs with a high-scoring baseline string

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.