{"slug": "self-healing-code-try-ai-fixes-runtime-errors-with-ai", "title": "🩹 Self-Healing Code — try_ai Fixes Runtime Errors with AI", "summary": "Pipe's new try_ai syntax catches runtime errors and uses an LLM to repair and re-execute the broken expression, with a catch block as a final fallback. In an example, the type error \"42\" * 3 was automatically fixed to (to_num \"42\") * 3, returning 126, while healthy code like 6 * 7 runs without any API call. The feature is part of the Pipe in 30 Lines series and requires a DeepSeek API key to run.", "body_md": "[← All posts← Alle Beiträge](../blog.html)\n\n# 🩹 Self-Healing Code — `try_ai`\n\nFixes Runtime Errors with AI\n\n**A type error that repairs itself: try_ai catches the crash, asks the LLM to fix the expression, and re-runs it.**\n\n> **Part of the *Pipe in 30 Lines* series:** [RAG without a vector DB](tutorial-local-rag.html) · [Parallel LLM calls](tutorial-parallel.html) · [Your first MCP server](tutorial-first-mcp-server.html)\n\n`\"42\" * 3`\n\nis a type error — a string multiplied by a number. Most runtimes crash. Pipe's `try_ai`\n\ncatches the error, sends the broken expression to the LLM, and re-runs the repaired code. The `catch`\n\nblock only fires if even the AI can't fix it.\n\n```\nai_provider \"deepseek\"\n\nresult: try_ai\n    \"42\" * 3          -- E002 type error → AI repairs it\ncatch e\n    0                 -- only reached if the fix fails\n\nprint result          -- 126\n\nfast: try_ai\n    6 * 7             -- healthy code: zero API calls\ncatch e\n    -1\n\nprint fast            -- 42\n```\n\nWhat happens here:\n\nruns the block. On success it returns the value directly —`try_ai`\n\n**no API call, no latency**. The safety net is free.- On a runtime error it asks the LLM to repair the broken expression:\n`\"42\" * 3`\n\nbecomes`(to_num \"42\") * 3`\n\nand re-runs. - The\nblock is the final fallback — your pipeline never dies silently.`catch`\n\nThis is where a language boundary pays off: `try_ai`\n\nisn't a library that wraps your calls, it's syntax that can *rewrite and re-execute a fragment of your own program*. That's self-healing infrastructure — not an SDK callback.\n\n```\n[try_ai] E002 | attempt 1 | \"\"42\" * 3\" → \"(to_num \"42\") * 3\" | ✓ FIXED\n126\n42\n```\n\nRun it: `export DEEPSEEK_API_KEY=... && pipe -vm -q examples/blog_try_ai.pipe`\n\n. The deeper mechanics (including the sandboxed re-run profile) are covered in the [MCP Cell deep dive](mcp-cell.html).\n\n# 🩹 Selbstheilender Code — `try_ai`\n\nrepariert Laufzeitfehler mit KI\n\n**Ein Typfehler, der sich selbst repariert: try_ai fängt den Crash, lässt das LLM den Ausdruck fixen und führt ihn erneut aus.**\n\n> **Teil der Serie *Pipe in 30 Lines*:** [RAG ohne Vektor-DB](tutorial-local-rag.html) · [Parallele LLM-Calls](tutorial-parallel.html) · [Dein erster MCP-Server](tutorial-first-mcp-server.html)\n\n`\"42\" * 3`\n\nist ein Typfehler — ein String mal eine Zahl. Die meisten Runtimes crashen. Pipes `try_ai`\n\nfängt den Fehler, schickt den kaputten Ausdruck an das LLM und führt den reparierten Code erneut aus. Der `catch`\n\n-Block feuert nur, wenn selbst die KI es nicht hinbekommt.\n\n```\nai_provider \"deepseek\"\n\nresult: try_ai\n    \"42\" * 3          -- E002 Typfehler → KI repariert ihn\ncatch e\n    0                 -- wird nur erreicht, wenn der Fix scheitert\n\nprint result          -- 126\n\nfast: try_ai\n    6 * 7             -- gesunder Code: null API-Calls\ncatch e\n    -1\n\nprint fast            -- 42\n```\n\nWas hier passiert:\n\nführt den Block aus. Bei Erfolg liefert es den Wert direkt —`try_ai`\n\n**kein API-Call, keine Latenz**. Das Sicherheitsnetz ist kostenlos.- Bei einem Laufzeitfehler bittet es das LLM, den Ausdruck zu reparieren: aus\n`\"42\" * 3`\n\nwird`(to_num \"42\") * 3`\n\n, dann wird erneut ausgeführt. - Der\n-Block ist die letzte Absicherung — deine Pipeline stirbt nie still.`catch`\n\nHier zahlt sich die Sprachgrenze aus: `try_ai`\n\nist keine Bibliothek, die deine Calls umwickelt, sondern Syntax, die *ein Fragment deines eigenen Programms umschreiben und neu ausführen* kann. Selbstheilende Infrastruktur — kein SDK-Callback.\n\n```\n[try_ai] E002 | attempt 1 | \"\"42\" * 3\" → \"(to_num \"42\") * 3\" | ✓ FIXED\n126\n42\n```\n\nStarten: `export DEEPSEEK_API_KEY=... && pipe -vm -q examples/blog_try_ai.pipe`\n\n. Die tieferen Mechanismen (inkl. des sandboxed Re-Run-Profils) findest du im [MCP-Cell-Deep-Dive](mcp-cell.html).", "url": "https://wpnews.pro/news/self-healing-code-try-ai-fixes-runtime-errors-with-ai", "canonical_source": "https://pipe-lang.com/blog/tutorial-self-healing.html", "published_at": "2026-08-14 07:41:58.439294+00:00", "updated_at": "2026-08-14 07:42:00.242340+00:00", "lang": "en", "topics": ["artificial-intelligence", "developer-tools"], "entities": ["Pipe", "DeepSeek"], "alternates": {"html": "https://wpnews.pro/news/self-healing-code-try-ai-fixes-runtime-errors-with-ai", "markdown": "https://wpnews.pro/news/self-healing-code-try-ai-fixes-runtime-errors-with-ai.md", "text": "https://wpnews.pro/news/self-healing-code-try-ai-fixes-runtime-errors-with-ai.txt", "jsonld": "https://wpnews.pro/news/self-healing-code-try-ai-fixes-runtime-errors-with-ai.jsonld"}}