Fix #434: Prevent TGDOptimizer template contamination with optimization instructions #447
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix #434: Prevent TGDOptimizer template contamination with optimization instructions
Problem
TGDOptimizer's
TEXT_GRAD_DESC_TEMPLATE
causes optimization instructions to leak into optimized prompt content, contaminating prompts with phrases like "when steps exceed 3" that don't belong in the target content. This makes the optimizer unsuitable for production use.Root Cause
The template mixed optimization meta-instructions directly with the content to be optimized in the same context, causing the LLM to blend optimization instructions with the actual prompt content:
Before (Problematic):
Solution
<OPTIMIZATION_CONTEXT>
section for meta-instructions about iteration strategy<TARGET_CONTENT_TO_OPTIMIZE>
section to isolate content to be optimized<CRITICAL_INSTRUCTION>
section with explicit contamination preventionAfter (Fixed):
Examples Fixed
Before Contamination:
"You are a helpful assistant"
"You are a helpful assistant, but when steps exceed 3, prioritize rapid updates"
After Fix:
"You are a helpful assistant"
"You are a helpful and empathetic assistant who provides thoughtful support"
Benefits
Testing
Impact
This fix resolves a critical issue that made TGDOptimizer unreliable for production use. Users can now optimize prompts without worrying about contamination with internal AdalFlow optimization concepts.
Type of Change
Fixes #434