Transforming Outputs for Next Steps
Raw outputs from one step rarely match exactly what the next step needs. Transformation bridges this gap, ensuring each step receives optimally formatted input.
Why Transformation Matters
Consider this scenario:
Step 1 produces:
{
"analysis": {
"sentiment": "negative",
"confidence": 0.89,
"issues": ["slow response", "incorrect information", "rude tone"],
"customer_history": { "tickets": 5, "ltv": 2500, "tenure_months": 24 }
}
}
Step 2 needs:
Customer Priority: VIP (24-month customer, $2,500 LTV)
Issue Summary: Customer experienced slow response, incorrect information, and rude tone
Sentiment: Negative (89% confidence)
Recommended Action: Prioritize resolution with personalized attention
Transformation converts between these formats while preserving meaning.
Types of Transformations
1. Format Conversion
Converting between data representations:
JSON to Natural Language:
// Input
{ "name": "John", "age": 30, "role": "Developer" }
// Output
"John is a 30-year-old Developer"
List to Prose:
// Input
["fast shipping", "good quality", "poor packaging"]
// Output
"Customers praised fast shipping and good quality, but noted concerns about poor packaging."
2. Filtering and Selection
Extracting only relevant parts:
3. Aggregation and Summarization
Condensing multiple pieces of information:
// Input: Multiple analysis results
[
{ "source": "twitter", "sentiment": 0.6, "volume": 500 },
{ "source": "facebook", "sentiment": 0.7, "volume": 300 },
{ "source": "reviews", "sentiment": 0.4, "volume": 150 }
]
// Output: Aggregated summary
{
"overall_sentiment": 0.58, // Weighted average
"total_mentions": 950,
"sentiment_trend": "mixed",
"primary_concern": "reviews showing lower sentiment"
}
4. Enrichment
Adding context or derived information:
// Input
{ "company": "ACME Corp", "revenue": 5000000 }
// Output (enriched)
{
"company": "ACME Corp",
"revenue": 5000000,
"revenue_formatted": "$5M",
"company_size": "mid-market", // Derived from revenue
"growth_benchmark": 0.15 // Added context
}
5. Restructuring
Reorganizing data for different purposes:
// Input: Flat list
[
{ "category": "bug", "priority": "high", "title": "Login fails" },
{ "category": "feature", "priority": "low", "title": "Dark mode" },
{ "category": "bug", "priority": "high", "title": "Crash on save" }
]
// Output: Grouped structure
{
"bugs": {
"high": ["Login fails", "Crash on save"],
"low": []
},
"features": {
"high": [],
"low": ["Dark mode"]
}
}
Transformation Strategies
Using AI for Complex Transformations
When transformations require understanding or judgment:
Programmatic Transformations
For simple, deterministic transformations, use code:
function transformForNextStep(step1Output) {
return {
summary: step1Output.analysis.summary,
priority: step1Output.analysis.score > 0.8 ? "high" : "normal",
formatted_date: new Date(step1Output.timestamp).toLocaleDateString(),
key_points: step1Output.analysis.findings.slice(0, 3)
};
}
Hybrid Approaches
Combine code and AI transformations:
// Step 1: AI generates raw analysis
const rawAnalysis = await runPrompt(step1Prompt);
// Programmatic: Extract and structure
const structured = parseJSON(rawAnalysis);
const filtered = structured.items.filter(i => i.priority === "high");
// Step 2: AI transforms to human-readable
const humanReadable = await runPrompt(
`Convert these high-priority items to a brief action list: ${JSON.stringify(filtered)}`
);
Building Transformation Prompts
When using AI for transformation, be explicit:
Template Structure
You are a data transformer. Your job is to convert input from Format A to Format B.
INPUT FORMAT:
[Describe the input structure]
OUTPUT FORMAT:
[Describe exactly what the output should look like]
RULES:
- [Specific transformation rules]
- [What to include/exclude]
- [How to handle edge cases]
INPUT:
[The actual data to transform]
Example: Customer Data Transformation
Handling Transformation Errors
Missing Fields
// Defensive transformation
function transform(input) {
return {
name: input.name || "Unknown",
score: input.score ?? 0, // Handle 0 as valid value
items: input.items || []
};
}
Invalid Data
// Validation during transformation
function transform(input) {
if (!input.sentiment || !["positive", "negative", "neutral"].includes(input.sentiment)) {
throw new TransformationError("Invalid sentiment value");
}
return {
sentiment: input.sentiment,
// ... rest of transformation
};
}
Format Mismatches
When AI outputs don't match expected format:
- Try to parse flexibly (e.g., extract JSON from markdown code blocks)
- Request clarification in a retry prompt
- Fall back to a default transformation
Exercise: Design a Transformation
Create a transformation for this scenario:
Step 1 Output: Detailed competitor analysis (multiple paragraphs) Step 2 Needs: Structured comparison table data
Key Takeaways
- Transformations bridge format gaps between chain steps
- Common transformations: format conversion, filtering, aggregation, enrichment, restructuring
- Use AI for complex, judgment-based transformations
- Use code for simple, deterministic transformations
- Build transformation prompts with explicit input/output formats and rules
- Handle transformation errors gracefully with validation and fallbacks
Next, we'll put these concepts together and build your first complete prompt chain.

