Automatically optimize AI prompts with OpenAI using OPRO & DSPy methodology
Ready for 3.0Ready for 3.0. This template uses no nodes that n8n 3.0 removes. No removed nodes, versions or helpers found. Static check, last run 2026-12-01.
What this workflow does
This AI workflow has 12 nodes and runs manually, when you click “Execute workflow”. Along the way it connects OpenAI Chat Model.
How it runs
Trigger: Manually, when you click “Execute workflow”.
- Define Initial Prompt & Test Data (Edit Fields (Set)): sets 3 fields: initial_prompt, test_input, ground_truth
- Manage Loop & State (Code): runs custom JavaScript code
- Check Loop Condition (If): checks a condition and branches into true / false
- AI Response Generator (AI Agent): runs an AI agent, with model: OpenAI Chat Model
- AI Response Evaluator (AI Agent): runs an AI agent, with model: OpenAI Chat Model; output parser: Evaluator Output Parser
- AI Prompt Optimizer (AI Agent): runs an AI agent, with model: OpenAI Chat Model; output parser: Optimizer Output Parser
- Update Prompt & Loop Count (Edit Fields (Set)): sets 3 fields: next_loop_count, improved_prompt, change_log
Services it uses
Before you upgrade to n8n 3.0
Nothing in this workflow is removed in n8n 3.0, so it should import and run after the upgrade.
Nodes used (8)
- Manual Trigger
n8n-nodes-base.manualTrigger - Edit Fields (Set)
n8n-nodes-base.set - AI Agent
@n8n/n8n-nodes-langchain.agent - Code
n8n-nodes-base.code - OpenAI Chat Model
@n8n/n8n-nodes-langchain.lmChatOpenAi - If
n8n-nodes-base.if - No Operation, do nothing
n8n-nodes-base.noOp - Structured Output Parser
@n8n/n8n-nodes-langchain.outputParserStructured
From the author
This workflow implements cutting-edge concepts from Google DeepMind's OPRO (Optimization by PROmpting) and Stanford's DSPy to automatically refine AI prompts. It iteratively generates, evaluates, and optimizes responses against a ground truth, allowing you to…