AutoDSPy: Automating Modular Prompt Design with Reinforcement Learning for Small and Large Language Models

Nafew Azim, Abrar Ur Alam, Hasan Bin Omar, Abdullah Mohammad Muntasir Adnan Jami, Jawad Ibn Ahad, Muhammad Rafsan Kabir, Md. Ismail Hossain, Fuad Rahman, Mohammad Ruhul Amin, Shafin Rahman, Nabeel Mohammed

✅ Accepted at EMNLP 2025 Industry Track · Pages 2881–2896 · November 4–9, 2025, Suzhou, China

📄 Paper: ACL Anthology · PDF
💻 Code: github.com/nafew-azim/AUTODSPy · DOI: 10.5281/zenodo.17276875


Abstract

Large Language Models (LLMs) excel at complex reasoning tasks, yet their performance hinges on the quality of their prompts and pipeline structures. Manual prompt design — as used in frameworks like DSPy — poses significant limitations: it is time-intensive, demands substantial expertise, and lacks scalability. To overcome these challenges, we introduce AutoDSPy, the first framework to fully automate DSPy pipeline construction using reinforcement learning (RL). AutoDSPy leverages an RL-tuned policy network to dynamically select optimal reasoning modules — such as Chain-of-Thought for logical tasks or ReAct for tool integration — along with input-output signatures and execution strategies, entirely eliminating the need for manual configuration. Experimental results on the GSM8K and HotPotQA benchmarks demonstrate that AutoDSPy outperforms traditional DSPy baselines, achieving accuracy gains of up to 4.3% while reducing inference time, even with smaller models like GPT-2 (127M). By integrating RL-based automation, AutoDSPy enhances both efficiency and accessibility, simplifying the development of structured, high-performing LLM solutions and enabling scalability across a wide range of tasks.


Introduction

LLMs have showcased extraordinary abilities in generating human-like text and tackling complex reasoning tasks, yet their effectiveness hinges heavily on how prompts are crafted and pipelines configured. Manual prompt engineering is a labor-intensive process, often requiring extensive trial-and-error, deep expertise, and significant time investment — sometimes yielding suboptimal results.

AutoDSPy vs DSPy vs LLM-Driven Pipeline Comparison

Figure 1: Comparison of pipeline construction methods. (a) Traditional DSPy requires manual, static configuration of modules and signatures — done entirely by the user. (b) LLM-driven refinement iteratively improves prompts using a pretrained LLM (e.g., PromptWizard). (c) AutoDSPy (Ours) uses reinforcement learning to train a finetuned policy network that builds optimized pipelines based on execution feedback — fully automated end-to-end.

DSPy introduced a structured, modular framework for building LLM pipelines using three components: signatures (typed I/O mappings), modules (reasoning strategies: ChainOfThought, ReAct, ProgramOfThought), and teleprompters (demonstration-based optimizers). However, DSPy still requires manual selection of modules, specification of signatures, and choice of optimization strategies — constraining scalability in dynamic or heterogeneous task environments.

AutoDSPy addresses these limitations by automating pipeline synthesis entirely through a policy network (PN) π_θ.


🎯 Key Contributions

  • AutoDSPy Framework — The first fully automated extension of DSPy, using RL to eliminate manual pipeline configuration.
  • Policy Network Design — An RL-tuned LLM that dynamically selects modules, signatures, and strategies for task-adaptive pipeline synthesis.
  • Three RL Strategies — REINFORCE (◇), PPO (♡), and GRPO (♣) — each offering distinct efficiency-accuracy trade-offs.
  • Small-Model Competitiveness — GPT-2 (127M) with AutoDSPy outperforms all DSPy baselines on GSM8K, demonstrating accessibility without high-compute policy models.
  • Empirical Superiority — Proven gains in accuracy and efficiency on GSM8K and HotPotQA, surpassing manual DSPy while preserving modularity and flexibility.

Methodology

Problem Formulation

A pipeline P of length L is defined as a sequence of module-signature pairs optimized under a selected teleprompter:

\[P = \bigl((M_0, s_0),\, (M_1, s_1),\, \ldots,\, (M_{L-1}, s_{L-1});\, t\bigr)\]

where each M_k ∈ M (reasoning module), s_k ∈ S (signature), and t ∈ T (teleprompter). The pipeline executes as:

\[o_0 = M_0^{s_0}(x), \quad o_1 = M_1^{s_1}(o_0), \quad \ldots \quad o_{L-1} = M_{L-1}^{s_{L-1}}(o_{L-2})\]

The goal is to learn a Policy Network π_θ that maps input x to an optimal pipeline P, maximizing:

\[\theta^* = \arg\max_\theta\; \mathbb{E}_{(x,y)\sim\mathcal{D}}\left[\mathbb{E}_{P\sim\pi_\theta(\cdot|x)}\bigl[R(o_{L-1}, y)\bigr]\right]\]

Binary reward R = 1{o_{L-1} = y} with semantic fallback via LLM assessment for partial correctness.

Policy Network

The Policy Network (PN) π_θ is a finetuned LLM that translates input prompts into pipelines. Seven transformer-based LLMs tested as policy networks: GPT-2-127M, LLaMA-3.2-1B, Gemma-3-1B, DeepSeek-R1-1.5B, Qwen-2.5-1.5B, Mistral-v0.1-7B, Phi-4-14B.

RL Algorithms

  1. REINFORCE (◇) — Stochastic gradient ascent with EMA baseline: \(\mathcal{L} = -\sum_t \log \pi_\theta(a_t | s_t) \cdot (r - b); \quad b \leftarrow 0.9b + 0.1r\)

  2. PPO (♡) — Stabilizes training via clipped objectives and entropy regularization with advantage estimates A_t.

  3. GRPO (♣) — Reduces variance by normalizing rewards within prompt-specific groups: \(\bar{r}_i = \frac{r_i - \text{mean}(r)}{\text{std}(r)}, \quad A_i = \bar{r}_i - \bar{r}\)

AutoDSPy Detailed Workflow

AutoDSPy Detailed Workflow

Figure 2: AutoDSPy detailed workflow. Training (outside dashed region): randomly selects (x,y) from D; PN π_θ generates several sample pipelines P from DSPy’s pool of modules M, signatures S, and teleprompters T; reward R(o_{L-1}, y) = 1{o_{L-1}=y} computed on final output; loss L computed via one of three RL algorithms (REINFORCE/GRPO/PPO); backpropagation updates θ ← θ − η∇θL. Repeat for N=200 episodes. Inference (dashed region): user prompt → PN generates one pipeline → LLM executes it.

Hyperparameters (Table 3)

ParameterValue
Learning Rate1 × 10⁻⁴
Gamma0.99
Lambda0.95
Clipping Epsilon0.2
Beta (Entropy Coefficient)0.01
Episodes200
K (GRPO Group Size)5

Experimental Setup

  • Datasets: GSM8K (1,300 test samples, mathematical reasoning); HotPotQA (1,000 test samples, multi-hop QA)
  • Training: 200 training samples, 200 episodes
  • Inference LMs: LLaMA-3.1-8B and Qwen-2.5-14B
  • DSPy baselines: Predict, CoT, MIPROv2*, BetterTogether* (* used with DSPy-CoT pipeline)
  • Hardware: NVIDIA T4 GPU, RTX 4090, TPU VM v3-8

📊 Results

Table 1: Main Performance — GSM8K & HotPotQA (Accuracy % / Inference Time s)

Bold Red = best; Blue = second best. No teleprompters used for AutoDSPy.

MethodRLLLaMA-3.1-8B GSM8KLLaMA-3.1-8B HotPotQAQwen-2.5-14B GSM8KQwen-2.5-14B HotPotQA
DSPy Baselines     
Predict70.6 / 6.470.5 / 7.471.3 / 6.271.1 / 7.1
CoT79.9 / 11.172.3 / 8.580.1 / 10.873.5 / 8.1
MIPROv2 *76.1 / 13.929.6 / 9.276.9 / 13.230.6 / 9.1
BetterTogether *79.8 / 9.231.6 / 4.680.4 / 9.031.8 / 4.3
GPT-2-127M76.8 / 5.174.2 / 6.177.2 / 4.974.8 / 5.9
 57.7 / 7.975.9 / 8.158.1 / 7.576.5 / 7.9
 82.4 / 8.173.9 / 4.283.0 / 8.074.3 / 4.0
LLaMA-3.2-1B75.5 / 7.473.0 / 6.476.2 / 7.273.8 / 6.2
 79.7 / 8.173.9 / 7.480.2 / 7.974.1 / 7.2
 79.5 / 8.974.0 / 10.580.4 / 8.774.9 / 10.0
Gemma-3-1B77.0 / 6.074.0 / 6.577.5 / 5.874.5 / 6.2
 78.5 / 7.874.5 / 7.579.0 / 7.675.0 / 7.3
 80.2 / 8.575.0 / 5.080.7 / 8.375.5 / 4.8
DeepSeek-R1-1.5B80.6 / 7.773.1 / 8.081.1 / 7.574.0 / 7.8
 79.7 / 9.873.0 / 8.580.0 / 9.573.7 / 8.1
 69.2 / 10.576.6 / 13.270.1 / 10.177.2 / 13.0
Qwen-2.5-1.5B78.0 / 7.075.5 / 7.078.5 / 6.876.0 / 6.8
 80.0 / 8.274.1 / 8.580.5 / 8.076.5 / 8.3
 70.3 / 9.076.0 / 6.071.4 / 8.876.7 / 5.8
Mistral-v0.1-7B77.5 / 7.575.0 / 7.278.0 / 7.375.5 / 7.0
 79.0 / 8.575.5 / 8.079.5 / 8.376.0 / 7.8
 79.9 / 9.575.5 / 6.582.0 / 9.377.0 / 6.3
Phi-4-14B76.5 / 6.573.5 / 6.877.0 / 6.374.0 / 6.6
 78.0 / 8.074.0 / 7.878.5 / 7.874.5 / 7.6
 80.5 / 8.874.5 / 5.581.0 / 8.675.0 / 5.3

Key Findings

  • GRPO (♣) best overall: GPT-2-127M GRPO achieves 82.4% GSM8K (vs. DSPy-CoT 79.9%), reducing inference from 11.1s → 8.1s. A 127M policy model beats manual expert design.
  • GRPO (♣) best HotPotQA: DeepSeek-R1-1.5B GRPO achieves 77.2% HotPotQA (vs. DSPy-CoT 73.5%) — +3.7% multi-hop gain.
  • REINFORCE (◇) fastest: 5.1s on GSM8K while maintaining competitive accuracy — ideal for latency-critical deployment.
  • PPO (♡) balanced: 57.7% → 76.6% on GSM8K with 300 episodes, showing strong policy optimization under more training.
  • MIPROv2 collapses on HotPotQA: Only 29.6% — multi-hop reasoning degrades with this teleprompter despite 76.1% on GSM8K.
  • BetterTogether failure on HotPotQA: 31.8% vs. 80.4% on GSM8K — teleprompters tuned for arithmetic fail to transfer to multi-hop QA.

Table 2: Generalizability — MGSM, MBPP, LegalBench, PubMedQA (Accuracy % / Time s)

No teleprompters used for AutoDSPy. MBPP metric: pass@1. Best in bold red, second in blue.

MethodRLLLaMA MGSMLLaMA MBPPLLaMA LegalBenchLLaMA PubMedQAQwen MGSMQwen MBPPQwen LegalBenchQwen PubMedQA
Predict62.4/5.872.3/6.032.5/5.757.5/5.963.1/5.673.0/5.833.2/5.558.2/5.7
CoT67.6/7.676.4/7.835.0/7.459.4/7.568.2/7.477.0/7.635.7/7.260.0/7.3
MIPROv265.3/8.275.8/8.333.5/8.058.6/8.166.0/8.076.5/8.134.2/7.859.3/7.9
BetterTogether67.3/7.076.7/7.235.5/6.860.7/6.968.0/6.877.4/7.036.2/6.661.4/6.7
GPT-2-127M68.1/5.576.5/5.736.0/5.461.2/5.668.7/5.377.1/5.536.6/5.261.8/5.4
 69.4/7.277.2/7.336.5/7.061.4/7.169.9/7.077.7/7.137.0/6.861.9/6.9
 69.7/6.378.0/6.538.5/6.262.0/6.470.2/6.178.5/6.339.0/6.062.5/6.2
Phi-4-14B68.8/9.977.8/10.036.5/9.861.8/10.069.3/9.778.3/9.837.0/9.662.3/9.8
 69.7/10.978.5/11.037.2/10.662.5/10.870.2/10.779.0/10.837.7/10.463.0/10.6
 70.5/10.479.5/10.538.7/10.163.3/10.371.0/10.280.0/10.339.4/9.963.8/10.1

AutoDSPy consistently outperforms DSPy baselines across all four additional benchmarks: +2.2% on MGSM (multilingual math), +3.4% on MBPP (code synthesis), +3.0% on LegalBench (legal reasoning), +2.4% on PubMedQA (biomedical QA). GRPO (♣) achieves top accuracy; REINFORCE (◇) prioritizes efficiency.


📊 Ablation Study

Figure 3: Learning Curves & RL Ablation

AutoDSPy Learning Curves and RL Ablation

Figure 3 (a): Accuracy vs training episodes/samples on GPT-2-127M with GRPO (LLaMA-3.1-8B inference). Accuracy increases steadily to 200 episodes/samples and mostly plateaus at 300. However, 300 episodes causes a significant drop on GSM8K (82.4% → 58.8%), indicating overfitting. (b): Impact of varying RL training on GPT-2 across REINFORCE (◇), PPO (♡), GRPO (♣). PPO (♡) improves substantially with more episodes (57.7% → 76.6% on GSM8K). Larger training samples often reduce accuracy due to insufficient episodes to process additional data.

Effect of Training Episodes (200 vs. 300)

AlgorithmGSM8K 200epGSM8K 300epHotPotQA 200epHotPotQA 300ep
REINFORCE ◇76.8%78.9% (↑)74.2%73.1% (↓)
PPO ♡57.7%76.6% (↑↑)75.9%54.6% (↓↓)
GRPO ♣82.4%58.8% (↓↓)73.9%77.8% (↑)

200 episodes is optimal for most configurations. 300 episodes causes severe accuracy drop for GRPO on GSM8K (82.4% → 58.8%) due to overfitting, while PPO benefits substantially (+19.0%), underscoring the need for precise episode tuning.

Figure 4: Teleprompter Impact & GRPO k Sensitivity

Teleprompter Impact and GRPO k Analysis

Figure 4: Teleprompters (LabeledFewShot, BootstrapFewShot, BootstrapFewShotWithRandomSearch, KNNFewShot, COPRO, BootstrapFinetune, Ensemble) evaluated across all three RL algorithms on GSM8K (G) and HotPotQA (H). Teleprompters increase latency across all algorithms (REINFORCE: 5.1s → 24.5s; GRPO: 8.1s → 30.8s; PPO: 7.9s → 28.1s on GSM8K) with inconsistent accuracy gains (GRPO drops 82.4% → 74.6%). Right: GRPO k sensitivity — k=5 optimal for GSM8K precision (82.4%), k=15 benefits HotPotQA (73.9% → 77.1%).

Teleprompter Analysis

Seven teleprompters tested; all excluded from final AutoDSPy design:

ConfigurationGSM8K AccGSM8K TimeHotPotQA AccHotPotQA Time
REINFORCE ◇ (no teleprompter)76.8%5.1s74.2%6.1s
REINFORCE ◇ (with teleprompter)75.8% (↓)24.5s (↑↑)75.7%
PPO ♡ (no teleprompter)57.7%7.9s75.9%8.1s
PPO ♡ (with teleprompter)74.3% (↑)28.1s (↑↑)75.5% (↓)18.4s
GRPO ♣ (no teleprompter)82.4%8.1s73.9%4.2s
GRPO ♣ (with teleprompter)74.6% (↓↓)30.8s (↑↑)69.3% (↓↓)

Teleprompters excluded from final framework: the latency overhead (up to 5×) is impractical for industrial deployment and accuracy gains are inconsistent — GRPO accuracy drops 7.8% on GSM8K and 4.6% on HotPotQA when teleprompters are added.


📊 Statistical Analysis

Figure 5: Exact-Match Rates Across 6 Benchmarks

Exact-Match Rates Across All Benchmarks

Figure 5: Exact-match rates (%) on 100 representative samples per dataset comparing DSPy methods, baseline policies (Random, Hardcoded), and AutoDSPy RL approaches across GSM8K (GSM), HotPotQA (HP), MGSM (MG), MBPP (MB), LegalBench (LB), PubMedQA (PM). GRPO (♣) achieves the best performance in the 81–89% range across all six benchmarks. Performance hierarchy: GRPO > PPO > REINFORCE holds consistently across all datasets (p < 0.01 in all paired t-tests vs. CoT baseline).

Relative Improvements of GRPO over DSPy-CoT

BenchmarkDSPy-CoTGRPO (AutoDSPy)Relative Gain
GSM8K~79.9%~84–89%+4.7%
HotPotQA~72.3%~82–86%+13.8%
MGSM~67.6%~76–80%+13.8%
MBPP~76.4%~85–88%+11.8%
LegalBench~35.0%~39–42%+11.0%
PubMedQA~59.4%~64–67%+11.8%

Table 4: Performance Consistency — 5 Independent Runs (GPT-2-127M, GRPO, GSM8K)

Run12345Mean ± Std
Accuracy (%)80.881.582.182.783.482.1 ± 1.0

Coefficient of variation < 1.2% — GRPO consistently discovers high-quality pipeline configurations regardless of random initialization. Semantic fallback (cosine similarity ≥ 0.92) contributes an additional 2–3% accuracy improvement over exact-match evaluation across all datasets.


📚 Citation

@inproceedings{azim2025autodspy,
  title={AutoDSPy: Automating Modular Prompt Design with Reinforcement Learning for Small and Large Language Models},
  author={Azim, Nafew and Alam, Abrar Ur and Omar, Hasan Bin and Jami, Abdullah Mohammad Muntasir Adnan and Ahad, Jawad Ibn and Kabir, Muhammad Rafsan and Hossain, Md. Ismail and Rahman, Fuad and Amin, Mohammad Ruhul and Rahman, Shafin and Mohammed, Nabeel},
  booktitle={Proceedings of the 2025 Conference on Empirical Methods in Natural Language Processing: Industry Track},
  pages={2881--2896},
  year={2025},
  organization={Association for Computational Linguistics}
}