This paper combines traditional optimization theory with modern Natural Language Processing (NLP) by formalizing Textual Gradient Descent (TextGrad) within a measure-theoretic framework. We introduce the concept of Expected Textual Loss, a Monte Carlo-inspired approach that enables gradient-based methods in discrete text spaces. Our extension, Monte Carlo TextGrad, improves convergence by systematically sampling from synthetic input distributions and integrating them into the optimization loop. Experimental validation spans both controlled object counting tasks and the LeetCode Hard benchmark, where our approach achieves statistically significant improvements in completion rates over baseline models and standard TextGrad. In addition, we analyze the potential distributional bias introduced by synthetic sampling through Kullback–Leibler divergence, establishing a principled framework for diagnosing and mitigating misalignment between training and deployment distributions. These results demonstrate that Monte Carlo TextGrad provides both faster convergence and greater robustness under distribution shift.
Atabekov et al. (Sat,) studied this question.