{"slug": "textgrad-automatic-differentiation-via-text", "title": "TextGrad: Automatic \"Differentiation\" via Text", "summary": "TextGrad, a framework for automatic 'differentiation' via text feedback from large language models, was published in Nature on 19th March 2025. The framework, which implements backpropagation through text, introduces a new engine based on LiteLLM to support models from Bedrock, Together, Gemini, and others. TextGrad provides a PyTorch-like API for defining loss functions and optimizing them with textual gradients.", "body_md": "An autograd engine -- for textual gradients!\n\nTextGrad is a powerful framework building automatic ``differentiation'' via text. TextGrad implements backpropagation through text feedback provided by LLMs, strongly building on the gradient metaphor\n\nWe provide a simple and intuitive API that allows you to define your own loss functions and optimize them using text feedback. This API is similar to the Pytorch API, making it simple to adapt to your usecases.\n\n**19th March 2025**\n\nTextGrad published in [Nature](https://www.nature.com/articles/s41586-025-08661-4)!\n\n**Past Updates**:\n\nWe are introducing a new engine based on [litellm](https://github.com/BerriAI/litellm). This should allow\nyou to use any model you like, as long as it is supported by litellm. This means that now\n**Bedrock, Together, Gemini and even more** are all supported by TextGrad!\n\nThis should be seen as experimental but we plan to depreciate the old engines in the future.\n\nIn addition to this, with the new engines it should be easy to enable and disable caching.\n\nWe are in the process of testing these new engines and deprecating the old engines. If you have any issues, please let us know!\n\nThe new litellm engines can be loaded with the following code:\n\nAn example of loading a litellm engine:\n\n```\nengine = get_engine(\"experimental:gpt-4o\", cache=False)\n\n# this also works with\n\nset_backward_engine(\"experimental:gpt-4o\", cache=False)\n```\n\nBe sure to set the relevant environment variables for the new engines!\n\nAn example of forward pass:\n\n``` python\nimport httpx\nfrom textgrad.engine_experimental.litellm import LiteLLMEngine\n\nLiteLLMEngine(\"gpt-4o\", cache=True).generate(content=\"hello, what's 3+4\", system_prompt=\"you are an assistant\")\n\nimage_url = \"https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg\"\nimage_data = httpx.get(image_url).content\n\nLiteLLMEngine(\"gpt-4o\", cache=True).generate(content=[image_data, \"what is this my boy\"], system_prompt=\"you are an assistant\")\n```\n\nIn the examples folder you will find two new notebooks that show how to use the new engines.\n\nIf you know PyTorch, you know 80% of TextGrad. Let's walk through the key components with a simple example. Say we want to use GPT-4o to solve a simple reasoning problem.\n\nThe question is *If it takes 1 hour to dry 25 shirts under the sun, how long will it take to dry 30 shirts under the sun? Reason step by step.* (Thanks, [Reddit User](https://www.reddit.com/r/OpenAI/comments/18q479x/comment/kf444es/))\n\n``` python\nimport textgrad as tg\n\ntg.set_backward_engine(\"gpt-4o\", override=True)\n\n# Step 1: Get an initial response from an LLM.\nmodel = tg.BlackboxLLM(\"gpt-4o\")\nquestion_string = (\"If it takes 1 hour to dry 25 shirts under the sun, \"\n                   \"how long will it take to dry 30 shirts under the sun? \"\n                   \"Reason step by step\")\n\nquestion = tg.Variable(question_string,\n                       role_description=\"question to the LLM\",\n                       requires_grad=False)\n\nanswer = model(question)\n```\n\n⚠️ answer: To determine how long it will take to dry 30 shirts under the sun,we can use a proportional relationship based on the given information.Here’s the step-by-step reasoning: [.....]So, it will take 1.2 hours (or 1 hour and 12 minutes) to dry 30 shirts under the sun.\n\nAs you can see, **the model's answer is incorrect.** We can optimize the answer using TextGrad to get the correct answer.\n\n```\nanswer.set_role_description(\"concise and accurate answer to the question\")\n\n# Step 2: Define the loss function and the optimizer, just like in PyTorch!\n# Here, we don't have SGD, but we have TGD (Textual Gradient Descent)\n# that works with \"textual gradients\".\noptimizer = tg.TGD(parameters=[answer])\nevaluation_instruction = (f\"Here's a question: {question_string}. \"\n                           \"Evaluate any given answer to this question, \"\n                           \"be smart, logical, and very critical. \"\n                           \"Just provide concise feedback.\")\n\n# TextLoss is a natural-language specified loss function that describes\n# how we want to evaluate the reasoning.\nloss_fn = tg.TextLoss(evaluation_instruction)\n```\n\n🧠\n\nloss: [...] Your step-by-step reasoning is clear and logical,but it contains a critical flaw in the assumption that drying time isdirectly proportional to the number of shirts. [...]\n\n```\n# Step 3: Do the loss computation, backward pass, and update the punchline.\n# Exact same syntax as PyTorch!\nloss = loss_fn(answer)\nloss.backward()\noptimizer.step()\nanswer\n```\n\n✅\n\nanswer: It will still take 1 hour to dry 30 shirts under the sun,assuming they are all laid out properly to receive equal sunlight.\n\nWe have many more examples around how TextGrad can optimize all kinds of variables -- code, solutions to problems, molecules, prompts, and all that!\n\nWe have prepared a couple of tutorials to get you started with TextGrad. The order of this tutorial is what we would recommend to follow for a beginner. You can run them directly in Google Colab by clicking on the links below (but you need an OpenAI/Anthropic key to run the LLMs).\n\n| Tutorial | Difficulty | Colab Link |\n|---|---|---|\n| 1. Introduction to TextGrad Primitives | ||\n| 2. Solution Optimization | ||\n| 3. Optimizing a Code Snippet and Define a New Loss | ||\n| 4. Prompt Optimization | ||\n| 5. MultiModal Optimization |\n\nYou can install TextGrad using any of the following methods.\n\n**With pip**:\n\n```\npip install textgrad\n```\n\n**With conda**:\n\n```\nconda install -c conda-forge textgrad\n```\n\n💡 The conda-forge package for\n\n`textgrad`\n\nis maintained[here].\n\n**Bleeding edge installation with pip**:\n\n```\npip install git+https://github.com/zou-group/textgrad.git\n```\n\n**Installing textgrad with vllm**:\n\n```\npip install textgrad[vllm]\n```\n\nSee [here](https://pip.pypa.io/en/stable/cli/pip_install/) for more details on various methods of pip installation.\n\nTextGrad can optimize unstructured variables, such as text. Let us have an initial solution to a math problem that we want to improve. Here's how to do it with TextGrad, using GPT-4o:\n\n```\ntg.set_backward_engine(\"gpt-4o\")\n\ninitial_solution = \"\"\"To solve the equation 3x^2 - 7x + 2 = 0, we use the quadratic formula:\nx = (-b ± √(b^2 - 4ac)) / 2a\na = 3, b = -7, c = 2\nx = (7 ± √((-7)^2 - 4 * 3(2))) / 6\nx = (7 ± √(7^3) / 6\nThe solutions are:\nx1 = (7 + √73)\nx2 = (7 - √73)\"\"\"\n\n# Define the variable to optimize, let requires_grad=True to enable gradient computation\nsolution = tg.Variable(initial_solution,\n                       requires_grad=True,\n                       role_description=\"solution to the math question\")\n\n# Define the optimizer, let the optimizer know which variables to optimize, and run the loss function\n\nloss_fn = tg.TextLoss(\"You will evaluate a solution to a math question. Do not attempt to solve it yourself, do not give a solution, only identify errors. Be super concise.\")\n\noptimizer = tg.TGD(parameters=[solution])\nloss = loss_fn(solution)\n```\n\nOutput:\n\nVariable(value=Errors:\n\n- Incorrect sign in the discriminant calculation: it should be b^2 - 4ac, not b^2 + 4ac.\n- Incorrect simplification of the quadratic formula: the denominator should be 2a, not 6.\n- Final solutions are missing the division by 2a., role=response from the language model, grads=)\n\n```\nloss.backward()\noptimizer.step()\nprint(solution.value)\n```\n\nOutput:\n\nTo solve the equation 3x^2 - 7x + 2 = 0, we use the quadratic formula: x = (-b ± √(b^2 - 4ac)) / 2a\n\nGiven: a = 3, b = -7, c = 2\n\nSubstitute the values into the formula: x = (7 ± √((-7)^2 - 4(3)(2))) / 6 x = (7 ± √(49 - 24)) / 6 x = (7 ± √25) / 6 x = (7 ± 5) / 6\n\nThe solutions are: x1 = (7 + 5) / 6 = 12 / 6 = 2 x2 = (7 - 5) / 6 = 2 / 6 = 1/3\n\nTextGrad can also optimize prompts in PyTorch style! Here's how to do it with TextGrad, using GPT-4o for feedback, and optimizing a prompt for gpt-3.5-turbo:\n\n``` python\nimport textgrad as tg\nfrom textgrad.tasks import load_task\n\nllm_engine = tg.get_engine(\"gpt-3.5-turbo\")\ntg.set_backward_engine(\"gpt-4o\")\n\n_, val_set, _, eval_fn = load_task(\"BBH_object_counting\", llm_engine)\nquestion_str, answer_str = val_set[0]\nquestion = tg.Variable(question_str, role_description=\"question to the LLM\", requires_grad=False)\nanswer = tg.Variable(answer_str, role_description=\"answer to the question\", requires_grad=False)\n```\n\nQuestion:\n\nI have two stalks of celery, two garlics, a potato, three heads of broccoli, a carrot, and a yam. How many vegetables do I have?\n\nGround Truth Answer:\n\n10\n\n```\nsystem_prompt = tg.Variable(\"You are a concise LLM. Think step by step.\",\n                            requires_grad=True,\n                            role_description=\"system prompt to guide the LLM's reasoning strategy for accurate responses\")\n\nmodel = tg.BlackboxLLM(llm_engine, system_prompt=system_prompt)\noptimizer = tg.TGD(parameters=list(model.parameters()))\n\nprediction = model(question)\n```\n\nPrediction:\n\nYou have a total of seven vegetables: two stalks of celery, two garlics, one potato, three heads of broccoli, one carrot, and one yam.\n\n```\nloss = eval_fn(inputs=dict(prediction=prediction, ground_truth_answer=answer))\n```\n\nLoss denoting accuracy:\n\nVariable(value=0, grads=)\n\n```\nloss.backward()\n```\n\nSystem prompt gradients:\n\n... 2.\n\nEncourage Explicit Summation: - The prompt should encourage the model to explicitly state the summation process. This can help in verifying the accuracy of the count. For example, \"Explain your calculations clearly and verify the total.\"....\n\n```\noptimizer.step()\n```\n\nNew system prompt value:\n\nYou are a concise LLM. Think step by step. Prioritize accuracy in your calculations. Identify and count each item individually. Explain your calculations clearly and verify the total. After calculating, review your steps to ensure the total is correct. If you notice a discrepancy in your count, re-evaluate the list and correct any mistakes.\n\n```\nprediction = model(question)\n```\n\nNew prediction:\n\nLet's count the number of each vegetable:\n\n- Celery stalks: 2\n- Garlics: 2\n- Potato: 1\n- Broccoli heads: 3\n- Carrot: 1\n- Yam: 1\nNow, let's add up the total number of vegetables: 2 + 2 + 1 + 3 + 1 + 1 = 10\n\nYou have a total of 10 vegetables.\n\nMany existing works greatly inspired this project! Here is a non-exhaustive list:\n\n- 📚\n[PyTorch](https://github.com/pytorch/pytorch/)The one and only. We owe a ton to PyTorch, hard to do justice here. - 📚\n[DSPy](https://github.com/stanfordnlp/dspy)is a pioneer in writing LM-based programs in many different ways! Has been a huge inspiration for us. - 📚\n[Micrograd](https://github.com/karpathy/micrograd): A tiny autograd engine greatly inspired our simple design! - 📚\n[ProTeGi](https://github.com/microsoft/LMOps/tree/main/prompt_optimization): We owe the term \"Textual Gradients\" to ProTeGi! - 📚\n[Reflexion](https://github.com/noahshinn/reflexion): A self-reflection that showed us the power of text-based reflection!\n\n```\n@article{yuksekgonul2025optimizing,\n  title={Optimizing generative AI by backpropagating language model feedback},\n  author={Yuksekgonul, Mert and Bianchi, Federico and Boen, Joseph and Liu, Sheng and Lu, Pan and Huang, Zhi and Guestrin, Carlos and Zou, James},\n  journal={Nature},\n  volume={639},\n  pages={609--616},\n  year={2025},\n}\n```\n\nWe are grateful for all the help we got from our contributors!\n\n|\nFederico Bianchi |\n\n**Mert Yuksekgonul**\n\n**Nihal Nayak**\n\n**Sugato Ray**\n\n**Pan Lu**\n\n**David Ruan**\n\n**San**\n\n**Zhi Huang**\n\n**Albert**\n\n**tboen1**\n\n**Atakan Tekparmak**", "url": "https://wpnews.pro/news/textgrad-automatic-differentiation-via-text", "canonical_source": "https://github.com/zou-group/textgrad", "published_at": "2026-08-01 18:45:44+00:00", "updated_at": "2026-08-01 18:52:21.418460+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-research", "developer-tools"], "entities": ["TextGrad", "Nature", "LiteLLM", "GPT-4o", "PyTorch"], "alternates": {"html": "https://wpnews.pro/news/textgrad-automatic-differentiation-via-text", "markdown": "https://wpnews.pro/news/textgrad-automatic-differentiation-via-text.md", "text": "https://wpnews.pro/news/textgrad-automatic-differentiation-via-text.txt", "jsonld": "https://wpnews.pro/news/textgrad-automatic-differentiation-via-text.jsonld"}}