Papers
Topics
Authors
Recent
Search
2000 character limit reached

Textual Gradient Optimization (TGO)

Updated 9 July 2026
  • Textual Gradient Optimization (TGO) is an optimization method that treats text as a variable, replacing analytic derivatives with natural-language critiques from LLMs.
  • It operates through iterative loops of forward execution, evaluation, and LLM-mediated textual gradient descent to refine prompts, code, workflows, and more.
  • TGO has shown empirical benefits across various benchmarks while also highlighting challenges in stability, cost, and the interpretation of textual gradients.

Textual Gradient Optimization (TGO) denotes a class of optimization methods that treat discrete text-valued artifacts as the variables to be optimized and replace analytic derivatives with natural-language feedback generated by LLMs or related evaluators. In the canonical formulation, the optimized object may be a prompt, a codebase, a workflow sketch, a SMILES string, or another text-valued node in a computation graph; downstream loss signals are converted into textual critiques, and a Textual Gradient Descent (TGD) step rewrites the current text accordingly. Across the recent literature, TGO appears both as a prompt-optimization technique and as a broader abstraction for optimizing compound AI systems, simulator code, federated prompts, co-evolving attack and defense prompts, and multi-agent workflows (Yuksekgonul et al., 2024, Hua et al., 21 Oct 2025).

1. Terminology and historical emergence

The terminology is historically heterogeneous. In one earlier line of work, "TextGrad" refers to a white-box adversarial attack generator for NLP robustness evaluation that uses a projected-gradient-descent-style optimization over continuously relaxed site-selection and token-replacement variables, followed by randomized sampling back to discrete text (Hou et al., 2022). In a later and now more common line, prompt optimization is cast as a gradient-like process in which minibatch errors are summarized as natural-language critiques and then applied as directed prompt edits; the APO or ProTeGi framework is an early example of this view, combining textual "gradients" with beam search and bandit selection (Pryzant et al., 2023).

The general computation-graph formulation was systematized by TextGrad, which treats a compound AI system as a directed acyclic graph whose nodes are text-valued variables and whose backward pass consists of propagating natural-language feedback through the graph (Yuksekgonul et al., 2024). Subsequent work broadened the scope of the idea rather than merely refining prompts. SOCIA-Nabla treats simulator construction as instance optimization over source code within a textual computation graph (Hua et al., 21 Oct 2025). FedTextGrad adapts textual gradients to federated learning (Chen et al., 27 Feb 2025). AEGIS uses a TGO module inside a co-evolutionary loop for prompt-injection attack and defense (Liu et al., 27 Aug 2025). FlowBot applies textual gradients to bilevel workflow induction (Yu et al., 29 Apr 2026), and TextResNet reformulates textual backpropagation for deep compound systems through residual-style signal routing (Huang et al., 9 Feb 2026).

2. Mathematical formulations

In the computation-graph formulation, each node vVv \in \mathcal{V} is a text-valued variable produced by a black-box forward function fvf_v applied to its predecessors. Given a downstream objective L\mathcal{L}, textual gradients are produced by a backward operator fvTf_v^T that returns natural-language feedback describing how vv should change to reduce the loss. TextGrad writes the recursive backward rule as

Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),

where \bigsqcup denotes concatenation of feedback from successor nodes, and the update is a TGD step

vnew=TGD.step ⁣(v,Lv).v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).

In this abstraction, the "gradient" is not a tensor but a textual critique, and the optimizer is itself an LLM call that rewrites the variable under its role description and any constraints (Yuksekgonul et al., 2024).

A prompt-level formulation appears in FedTextGrad. Given a fixed LLM M\mathcal{M}, an input query xx, and a prompt fvf_v0, the forward computation is

fvf_v1

with an implicit scalar evaluation fvf_v2 extracted from the evaluation text. The paper then presents a chain-rule analogy in which the textual gradient with respect to the prompt is decomposed into feedback about the response and feedback about how the prompt should change to improve the response, followed by a prompt rewrite through fvf_v3 (Chen et al., 27 Feb 2025).

SOCIA-Nabla provides a constrained optimization view in which the optimized variable is simulator code fvf_v4. Its objective is

fvf_v5

with

fvf_v6

At iteration fvf_v7, execution yields outputs fvf_v8, evaluation produces fvf_v9, and the textual gradient is defined as

L\mathcal{L}0

where L\mathcal{L}1 is represented by numeric diffs such as residuals or failing tests. The resulting L\mathcal{L}2 is a set of localized, executable code-edit suggestions (Hua et al., 21 Oct 2025).

3. Optimization loops and architectural variants

Despite the diversity of applications, recent TGO systems repeatedly instantiate a common loop: forward execution, scalar or textual evaluation, natural-language gradient acquisition, and LLM-mediated update. What differs across papers is how the feedback is aggregated, stabilized, routed, or constrained.

Work Optimized object Distinct mechanism
TextGrad (Yuksekgonul et al., 2024) Text-valued graph nodes loss.backward() plus TextualGradientDescent.step
SOCIA-Nabla (Hua et al., 21 Oct 2025) Simulator code Momentum over last L\mathcal{L}3 gradients and projection to a feasible set
FedTextGrad (Chen et al., 27 Feb 2025) Federated prompts Server-side prompt aggregation with UID-guided summarization
AEGIS (Liu et al., 27 Aug 2025) Attack and defense prompts Gradient buffering, multi-route gradients, co-evolution
REVOLVE (Zhang et al., 2024) Prompts, solutions, code Immediate feedback plus response-similarity term
TSGD-M (Ding et al., 31 May 2025) Prompts Token-wise mixture sampling over past prompts
FlowBot (Yu et al., 29 Apr 2026) Workflow sketch and per-call prompts Bilevel optimization with layer-wise textual backprop
TextResNet (Huang et al., 9 Feb 2026) Compound AI system prompts/configurations Additive Semantic Deltas, Semantic Gradient Decomposition, Causal Routing, Density-Aware Optimization Scheduling

In APO or ProTeGi, the gradient-like update is explicitly tied to minibatches: the current prompt is evaluated on a subset of data, errors are summarized into textual critiques, edited prompt candidates are generated, and beam search plus a bandit-style selection procedure chooses which candidates survive (Pryzant et al., 2023). TextGrad retains the gradient analogy but generalizes it to arbitrary directed acyclic graphs of text-valued variables (Yuksekgonul et al., 2024).

Several later works focus on stabilization. SOCIA-Nabla accumulates momentum by merging the last L\mathcal{L}4 gradients with decay L\mathcal{L}5, then performs an unprojected code update followed by a projection step that enforces compilability, I/O schema consistency, and other hard constraints (Hua et al., 21 Oct 2025). TSGD-M instead introduces sampling-based momentum: it maintains a buffer of past prompts, assigns exponentially decayed weights

L\mathcal{L}6

and performs token-wise mixture sampling from the decoding distributions conditioned on past prompts, thereby mimicking SGD with momentum at the token-generation level (Ding et al., 31 May 2025).

Other works modify the topology of the optimization process itself. AEGIS runs two TGO instances in parallel inside a co-evolutionary loop, one for attacker prompts and one for defender prompts, and augments each update with a FIFO gradient buffer and multi-route gradients for different objective dimensions such as ASR, L\mathcal{L}7, TPR, and TNR (Liu et al., 27 Aug 2025). REVOLVE adds a cross-iteration similarity term L\mathcal{L}8 to immediate textual feedback L\mathcal{L}9, yielding an overall update fvTf_v^T0 that acts as a second-order analogue without forming a Hessian (Zhang et al., 2024). FlowBot separates optimization into an inner loop over per-call prompts and an outer loop over workflow structure (Yu et al., 29 Apr 2026). TextResNet addresses deep-chain failure modes by forcing forward computation into residual-style additive semantic deltas and by decomposing backward feedback into local and upstream components before routing them (Huang et al., 9 Feb 2026).

4. Application domains and empirical record

On standard prompt, solution, and code-optimization benchmarks, TextGrad reports improvements across several tasks without changing the framework: GPT-4o zero-shot accuracy on GPQA Diamond rises from fvTf_v^T1 to fvTf_v^T2; MMLU-ML rises from fvTf_v^T3 to fvTf_v^T4; MMLU-Phys rises from fvTf_v^T5 to fvTf_v^T6; LeetCode-Hard pass rate rises from fvTf_v^T7 to fvTf_v^T8; and prompt optimization improves Object Counting from fvTf_v^T9 to vv0, Word Sorting from vv1 to vv2, and GSM8k from vv3 to vv4 (Yuksekgonul et al., 2024). REVOLVE reports a vv5 improvement in prompt optimization, a vv6 gain in solution refinement, and a vv7 increase in code optimization, while converging in fewer iterations; although each step costs about vv8 as much time as TextGrad, total time to converge is reported as vv9--Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),0 (Zhang et al., 2024).

TGO has also been extended to system-level artifacts. SOCIA-Nabla applies TGO to simulator generation and reports the lowest error in Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),1 of Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),2 metrics across User Modeling, Mask Adoption, and Personal Mobility, including a Personal Mobility OOD DARD of Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),3 versus Reflexion's Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),4 and G-SIM-SBI's Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),5 (Hua et al., 21 Oct 2025). FlowBot applies textual gradients to workflow induction and reports an average of Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),6 on six tasks, compared with approximately Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),7 for its reimplementation of AFlow with GPT-4.1 mini, while using about Lv=wSuccessorsOf(v)fvT ⁣(v,w,Lw),\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),8 API calls (\$\frac{\partial \mathcal{L}}{\partial v} = \mathop{\bigsqcup}_{w \in \mathrm{SuccessorsOf}(v)} f_v^T\!\left(v, w, \frac{\partial \mathcal{L}}{\partial w}\right),9338K9338\text{K}\bigsqcup$0131) (Yu et al., 29 Apr 2026). TextResNet evaluates deep compound systems and reports HotpotQA $\bigsqcup$1 F1 versus $\bigsqcup$2 for TextGrad, BigCodeBench $\bigsqcup$3 pass@1 versus $\bigsqcup$4, PubMedQA $\bigsqcup$5 accuracy versus $\bigsqcup$6, and STARK $\bigsqcup$7 MRR versus $\bigsqcup$8; in deep-chain experiments with depth up to $\bigsqcup$9, TextGrad collapses to F1 $v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$0 while TextResNet remains stable at approximately $v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$1 (Huang et al., 9 Feb 2026).

In federated and safety-oriented settings, FedTextGrad identifies prompt aggregation as the central technical hurdle. On BBH Object Counting, centralized TextGrad with LLaMA-3.1-8B reaches approximately $v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$2 accuracy, FedTextGrad with summarization reaches approximately $v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$3, and FedTextGrad with UID summarization reaches approximately $v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$4; UID summarization improves Object Counting and GSM8k by $v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$5--$v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$6 percentage points, lowers surprisal variance by approximately $v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$7, and keeps prompts below $v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$8 tokens instead of approximately $v_{\text{new}} = \mathrm{TGD.step}\!\left(v, \frac{\partial \mathcal{L}}{\partial v}\right).$9 for concatenation (Chen et al., 27 Feb 2025). AEGIS, which uses TGO inside a co-evolutionary attack-defense loop for prompt injections, reports ASR $\mathcal{M}$0, $\mathcal{M}$1, defender TPR $\mathcal{M}$2, and defender TNR $\mathcal{M}$3 at the end of $\mathcal{M}$4 GAN iterations; without the gradient buffer, final TPR drops by $\mathcal{M}$5--$\mathcal{M}$6 and convergence becomes unstable (Liu et al., 27 Aug 2025).

TGO has also been integrated with numeric optimization and with non-LLM generative models. Language-Guided Tuning uses an outer textual gradient over configuration parameters $\mathcal{M}$7 alongside standard inner-loop weight updates over $\mathcal{M}$8, and reports MNIST accuracy $\mathcal{M}$9 versus $x$0 for Bayesian optimization, CIFAR-10 accuracy $x$1 versus $x$2, House-Price MAE $x$3 versus $x$4, and Wine-Quality MSE $x$5 versus $x$6 (Lu et al., 21 Aug 2025). In diffusion-model prompt optimization, a discrete TGO framework uses compact subspaces, a Shortcut Text Gradient, and a Gumbel-Softmax relaxation; on DiffusionDB prompt enhancement, DPO-Diff improves CLIP from approximately $x$7 to $x$8 and HPSv2 from approximately $x$9 to $f_v$00, while for adversarial attack it raises CLIP from approximately $f_v$01 to $f_v$02 and lowers HPSv2 from $f_v$03 to $f_v$04 (Wang et al., 2024).

5. Technical limitations and conceptual disputes

The core TextGrad paper explicitly identifies cost, latency, and stability as limitations: each graph edge and parameter update invokes an LLM call, textual gradients can be noisy, success depends on the LLM's ability to critique and improve, and real-world scientific or clinical applications require rigorous external validation (Yuksekgonul et al., 2024). FedTextGrad adds a federated-specific instability: as the number of clients increases beyond $f_v$05, heterogeneity and communication noise cause performance to drop nearly linearly, and naïve prompt aggregation either exceeds context windows or compresses client updates too aggressively (Chen et al., 27 Feb 2025). TextResNet frames a related failure mode for deep compound systems as Semantic Entanglement and Attribution Ambiguity, arguing that standard textual backpropagation mixes local critiques with upstream context in ways that cause baseline methods to collapse on long chains (Huang et al., 9 Feb 2026).

A more fundamental critique is that the gradient metaphor itself may be misleading. One recent study argues that textual gradients are a flawed metaphor for automatic prompt optimization because the chain-rule analogy breaks down, there is no consistent textual analogue of step size or momentum, and the multi-stage "gradient-like" loop shows no consistent advantage over simpler one-step feedback prompting (Melcer et al., 15 Dec 2025). In the experiments reported there, all feedback-driven methods improve over the default prompt by $f_v$06--$f_v$07 percentage points across tasks, but correct versus incorrect labels do not produce statistically meaningful differences in test accuracy, training for $f_v$08 steps shows no classic overfitting rise-then-fall pattern, and a Web of Lies case study exhibits "prevalence hacking" through the prompt instruction "NEVER declare 'unknown'" (Melcer et al., 15 Dec 2025).

These critiques do not negate the empirical utility reported by other papers, but they change the interpretation. A plausible implication is that many successful TGO systems work less like gradient descent in a strict mathematical sense and more like structured, data-driven rewriting procedures whose effectiveness depends on evaluation design, candidate selection, and search control.

6. Extensions and open directions

The research trajectory points toward broader optimization targets and more elaborate control of feedback flow. TextGrad proposes extending textual differentiation to multimodal graphs, adaptive optimization with variance reduction and adaptive step sizes, meta-learning that improves gradient prompts themselves, retrieval-augmented or tool-using agents, and external validation for molecular design and radiotherapy planning (Yuksekgonul et al., 2024). FedTextGrad identifies the retention of essential information during server-side prompt aggregation as the central open problem in federated settings and motivates further work on summarization and privacy-preserving coordination (Chen et al., 27 Feb 2025).

More papers extend TGO from prompt rewriting to the optimization of workflows, simulators, and deep agentic systems. FlowBot formulates workflow induction as bilevel optimization in which both the workflow sketch fvf_v09 and the per-call prompts fvf_v10 are optimized with textual gradients (Yu et al., 29 Apr 2026). SOCIA-Nabla treats simulator source code itself as the trainable object and reserves human-in-the-loop interaction for task-spec confirmation (Hua et al., 21 Oct 2025). TextResNet restructures the backward pass through Semantic Gradient Decomposition, Causal Routing, and Density-Aware Optimization Scheduling (Huang et al., 9 Feb 2026). Taken together, these developments suggest a shift from local prompt editing toward the optimization of entire compound AI systems, where the primary research questions concern signal attribution, constraint handling, aggregation, stability, and evaluation rather than prompt wording alone.

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Textual Gradient Optimization (TGO).