Skip to content

Conversation

pxkundu
Copy link
Contributor

@pxkundu pxkundu commented Sep 15, 2025

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):

You are {{steps}} steps since your last improvement.
Update the value more rapidly when steps are larger than 3.
{{variable_and_peers_info}}

Solution

  • Separated optimization context from target content using structured sections
  • Added <OPTIMIZATION_CONTEXT> section for meta-instructions about iteration strategy
  • Added <TARGET_CONTENT_TO_OPTIMIZE> section to isolate content to be optimized
  • Added <CRITICAL_INSTRUCTION> section with explicit contamination prevention
  • Used clear XML-like boundaries to prevent context bleeding between sections
  • Maintained full backward compatibility with existing template variables

After (Fixed):

<OPTIMIZATION_CONTEXT>
Current optimization iteration: {{steps}} steps since your last improvement.
Optimization strategy: Use more aggressive updates after 3 iterations without improvement.
</OPTIMIZATION_CONTEXT>

<TARGET_CONTENT_TO_OPTIMIZE>
{{variable_and_peers_info}}
</TARGET_CONTENT_TO_OPTIMIZE>

<CRITICAL_INSTRUCTION>
IMPORTANT: Optimize ONLY the content in the TARGET_CONTENT_TO_OPTIMIZE section above.
Do NOT include any references to optimization steps, iterations, or meta-instructions in your response.
</CRITICAL_INSTRUCTION>

Examples Fixed

Before Contamination:

  • Original: "You are a helpful assistant"
  • Contaminated Result: "You are a helpful assistant, but when steps exceed 3, prioritize rapid updates"

After Fix:

  • Original: "You are a helpful assistant"
  • Clean Result: "You are a helpful and empathetic assistant who provides thoughtful support"

Benefits

  • 🎯 Clean Output: Optimized prompts contain only improved content, no optimization metadata
  • 🔒 Production Ready: TGDOptimizer can now be used reliably in production systems
  • 📋 Clear Separation: Distinct sections prevent context bleeding between instructions and content
  • 🔄 Backward Compatible: All existing template variables and functionality preserved
  • 🛡️ Explicit Prevention: Strong instructions prevent contamination patterns

Testing

  • ✅ Verified template structure correctly separates sections
  • ✅ Confirmed rendering works with all template variables
  • ✅ Validated contamination prevention instructions are included
  • ✅ Tested backward compatibility with existing functionality
  • ✅ Verified integration with TGDOptimizer imports and initialization

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

  • Bug fix (non-breaking change which fixes an issue)
  • Improves production readiness for prompt optimization

Fixes #434

…orm compatibility

- Replace subprocess.call(['wget', ...]) with urllib.request.urlretrieve()
- Fix dataset download failure on Windows and minimal Docker images
- Add improved error handling with specific HTTP status codes
- Ensure directory creation before download
- Maintain backward compatibility and all existing functionality

Resolves: 'FileNotFoundError: The system cannot find the file specified'
on Windows when downloading BigBenchHard datasets.
…rser mixing

- Replace problematic instance variable assignment with dynamic parser selection
- Fix issue where self.response_parser persisted across calls causing mode confusion
- Add type-specific logic to distinguish Response, AsyncIterable, and Iterable objects
- Exclude basic types (str, bytes, dict) from streaming detection
- Ensure correct parser is always selected based on completion type

Resolves: OpenAI client getting 'stuck' in streaming or non-streaming mode
after switching between stream=True and stream=False calls.
… optimization instructions

- Separate optimization context from target content in TEXT_GRAD_DESC_TEMPLATE
- Replace problematic mixed instructions with structured sections
- Add OPTIMIZATION_CONTEXT section for meta-instructions about iteration strategy
- Add TARGET_CONTENT_TO_OPTIMIZE section to isolate content to be optimized
- Add CRITICAL_INSTRUCTION section with explicit contamination prevention
- Use clear XML-like boundaries to prevent context bleeding between sections
- Maintain full backward compatibility with existing template variables

Resolves: TGDOptimizer contaminating prompts with phrases like 'when steps exceed 3'
that don't belong in optimized content, making the optimizer unsuitable for production.
…rrors; Tests: align OpenAI parser tests with dynamic parser selection; CI: add pytest-mock and lancedb to test group
… instances

- Fix Agent initialization in open_ai_test.py by adding required model_client and model_kwargs parameters
- Fix setup_env() calls in test files to handle missing .env file gracefully
- Add pytest.mark.asyncio decorators to async test functions
- Fix import path for AutoApprovalHandler in test_permission_system.py
- Use fake API keys for testing to avoid environment variable requirements

All tests now pass: 542 passed, 2 skipped, 11 warnings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TGDOptimizer Internal Instructions Contaminating Optimized Prompts
1 participant