T5 Task Formatting › Parsing Generated Outputs

Michael BrenndoerferOctober 15, 20253 min read
Language AI HandbookAll chapters
Part 25 of 67Encoder-Decoder Models
Chapter 179T5 Task Formatting › Parsing Generated Outputs
Reading section 10 of 15 · Parsing Generated Outputs · 3 minSection 10 of 15 · 3 min

Book contents

Section 10 of 15 · 3 min read

T5 Task Formatting › Parsing Generated OutputsOne section · Sections in this chapter10 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Generating text is only half the problem. For tasks with structured outputs, you need to parse the model's generations back into usable data. The model produces strings, but your application likely needs Python objects, database entries, or API responses. This parsing step bridges the gap between the model's text-based interface and the structured world of software systems.

Reliable output parsing is one of the most practically important aspects of deploying text-to-text models. Model outputs will occasionally deviate from expected formats, especially for novel inputs that differ from training data. A well-designed parser handles variations gracefully, normalizes outputs where possible, and fails in controlled, informative ways when the output is truly unparseable. The goal is not perfect parsing, but graceful degradation.

In[34]:
Code
import re
from typing import Any, Dict, List, Tuple


class T5OutputParser:
    """Parse T5 generated outputs back into structured data."""

    @staticmethod
    def parse_classification(output: str, valid_labels: List[str]) -> str:
        """Parse classification output, handling slight variations."""
        output = output.strip().lower()
        for label in valid_labels:
            if label.lower() in output:
                return label
        return output  # Return raw if no match

    @staticmethod
    def parse_entities(output: str) -> List[Tuple[str, str]]:
        """Parse entity list format: 'type1: entity1, type2: entity2'"""
        if not output.strip():
            return []

        entities = []
        for part in output.split(", "):
            if ": " in part:
                entity_type, entity_text = part.split(": ", 1)
                entities.append((entity_type.strip(), entity_text.strip()))

        return entities

    @staticmethod
    def parse_inline_entities(output: str) -> List[Dict[str, Any]]:
        """Parse inline markup format: '[TYPE entity text]'"""
        pattern = r"\[(\w+)\s+([^\]]+)\]"
        matches = re.findall(pattern, output)
        return [{"type": m[0], "text": m[1]} for m in matches]

    @staticmethod
    def parse_similarity_score(output: str) -> float:
        """Parse numeric similarity score."""
        try:
            # Handle outputs like "4.2" or "score: 4.2"
            numbers = re.findall(r"\d+\.?\d*", output)
            if numbers:
                return float(numbers[0])
        except ValueError:
            pass
        return 0.0
In[35]:
Code
# Test the parsers
parser = T5OutputParser()

# Classification parsing
sentiment_output = "positive"
parsed_sentiment = parser.parse_classification(
    sentiment_output, ["positive", "negative"]
)

# Entity parsing
ner_output = (
    "person: Marie Curie, location: Paris, organization: Sorbonne University"
)
parsed_entities = parser.parse_entities(ner_output)

# Similarity parsing
similarity_output = "3.8"
parsed_score = parser.parse_similarity_score(similarity_output)
Out[36]:
Console
Sentiment: 'positive' -> positive

Entities: 'person: Marie Curie, location: Paris, organization: Sorbonne University'
  - person: Marie Curie
  - location: Paris
  - organization: Sorbonne University

Similarity score: '3.8' -> 3.8

The parsers successfully convert raw text outputs back into structured Python data. Classification outputs map to discrete labels, entity strings become lists of (type, text) tuples, and numeric scores parse to floats. Notice that parse_classification uses substring matching rather than exact matching: it checks whether any valid label appears in the output rather than requiring an exact string match. This handles common variations like leading or trailing whitespace, extra qualifying words ("the sentiment is positive"), or minor phrasing differences. The tradeoff is that it might incorrectly match if one label is a substring of another. Designing parsing logic requires thinking carefully about what kinds of variation your model is likely to produce for your specific training data.