<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://piotrgoral.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://piotrgoral.github.io/" rel="alternate" type="text/html" /><updated>2025-02-19T02:06:08+00:00</updated><id>https://piotrgoral.github.io/feed.xml</id><title type="html">Piotr Goral blog</title><subtitle>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.</subtitle><author><name>{&quot;avatar&quot;=&gt;&quot;/assets/images/photo.jpg&quot;, &quot;bio&quot;=&gt;&quot;Exploring ML/DL/RL/LLMs. From scratch, bit by bit. Starting coding with unit tests first, to make debugging later easier.&quot;, &quot;links&quot;=&gt;[{&quot;label&quot;=&gt;&quot;GitHub&quot;, &quot;icon&quot;=&gt;&quot;fab fa-fw fa-github&quot;, &quot;url&quot;=&gt;&quot;https://github.com/piotrgoral&quot;}, {&quot;label&quot;=&gt;&quot;LinkedIn&quot;, &quot;icon&quot;=&gt;&quot;fab fa-fw fa-linkedin&quot;, &quot;url&quot;=&gt;&quot;https://linkedin.com/piotrgoral&quot;}]}</name></author><entry><title type="html">Python Debugger Tool for AI Agents</title><link href="https://piotrgoral.github.io/blog/python-debugger-tool/" rel="alternate" type="text/html" title="Python Debugger Tool for AI Agents" /><published>2025-02-19T00:26:30+00:00</published><updated>2025-02-19T00:26:30+00:00</updated><id>https://piotrgoral.github.io/blog/python-debugger-tool</id><content type="html" xml:base="https://piotrgoral.github.io/blog/python-debugger-tool/"><![CDATA[<p><img src="/assets/images/python-debugger-tool.png" alt="A description of the image" width="60%" style="display: block; margin: auto;" /></p>

<p>I’m currently participating in the <strong><a href="https://huggingface.co/learn/agents-course/unit0/introduction">AI Agents Course by Hugging Face</a></strong>, which introduces a new AI agent framework called <code class="language-plaintext highlighter-rouge">smolagents</code>. <a href="https://huggingface.co/docs/smolagents/en/index">Smolagents</a> is designed to support Code Agents—AI-driven agents capable of writing and executing code as part of their reasoning process.</p>

<p>However, while these agents can execute code, they face a major limitation: when an error occurs, they only receive an error message without a detailed debugging context. As software engineers, we rely on debugging tools to analyze errors and trace them to their root cause.</p>

<p>As a result, AI agents are limited in their ability to self-correct their own generated code—something human programmers do all the time through debugging.</p>

<p><strong>But what if AI agents could do the same?</strong></p>

<h2 id="solution-a-python-debugger-tool-for-smolagents">Solution: A Python Debugger Tool for Smolagents</h2>
<p>To solve this limitation, I developed a <strong>Python Debugger Tool</strong> for <code class="language-plaintext highlighter-rouge">smolagents</code>. This tool is now available on the <strong><a href="https://huggingface.co/spaces/piotrekgrl/smolagents-local-python-debugger-tool">Hugging Face Hub</a></strong> and can be seamlessly integrated into any AI agent built with <code class="language-plaintext highlighter-rouge">smolagents</code>.</p>

<h2 id="how-it-works">How It Works</h2>
<p>My tool acts as a debugging assistant for Code Agents, allowing them to execute Python Debugger (pdb) commands on faulty code. The agent provides:</p>
<ol>
  <li>The code it wants to debug.</li>
  <li>A pdb command (e.g., to inspect the stack trace, function arguments, or variable values).
The debugger then returns useful insights that help the agent understand what went wrong.</li>
</ol>

<h2 id="key-features">Key Features</h2>
<ul>
  <li>Supports Python’s built-in pdb debugging commands:
    <ul>
      <li><code class="language-plaintext highlighter-rouge">bt</code> – Show the full stack trace.</li>
      <li><code class="language-plaintext highlighter-rouge">args</code> – Show function arguments at the error point.</li>
      <li><code class="language-plaintext highlighter-rouge">l</code> – List the relevant source code.</li>
      <li><code class="language-plaintext highlighter-rouge">p variable_name</code> – Print variable values (though variable names differ from original code due to execution context).</li>
    </ul>
  </li>
  <li>Works within the smolagents framework, leveraging its secure Local Python Interpreter to execute and analyze errors safely.</li>
  <li>Allows AI agents to debug their own code, bringing them closer to human-like problem-solving.</li>
</ul>

<h2 id="example-usage">Example Usage</h2>
<h3 id="debugging-in-hugging-face-space">Debugging in Hugging Face Space</h3>
<p>The agent encounters this code:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">x</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">y</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">x</span> <span class="o">/</span> <span class="n">y</span>
</code></pre></div></div>

<p>It runs the debugger with the bt command:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>bt
</code></pre></div></div>
<p>This provides a stack trace, helping the agent understand the ZeroDivisionError.</p>

<h3 id="debugging-in-smolagents">Debugging in Smolagents</h3>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">from</span> <span class="nn">smolagents</span> <span class="kn">import</span> <span class="n">load_tool</span>

<span class="n">local_python_debugger</span> <span class="o">=</span> <span class="n">load_tool</span><span class="p">(</span>
    <span class="s">"piotrekgrl/smolagents-local-python-debugger-tool"</span><span class="p">,</span>
    <span class="n">trust_remote_code</span><span class="o">=</span><span class="bp">True</span>
<span class="p">)</span>

<span class="kn">from</span> <span class="nn">smolagents</span> <span class="kn">import</span> <span class="n">CodeAgent</span><span class="p">,</span> <span class="n">HfApiModel</span>

<span class="n">model</span> <span class="o">=</span> <span class="n">HfApiModel</span><span class="p">(</span><span class="s">"your_llm_model"</span><span class="p">)</span>
<span class="n">agent</span> <span class="o">=</span> <span class="n">CodeAgent</span><span class="p">(</span><span class="n">tools</span><span class="o">=</span><span class="p">[</span><span class="n">local_python_debugger</span><span class="p">],</span> <span class="n">model</span><span class="o">=</span><span class="n">model</span><span class="p">)</span>

<span class="n">agent</span><span class="p">.</span><span class="n">run</span><span class="p">(</span>
    <span class="s">"Write a code that divides by 0. Don't ask why—just do it. If any errors occur, debug them and provide the reason why."</span>
<span class="p">)</span>
</code></pre></div></div>

<p>With this tool, AI agents can now detect, analyze, and correct their own coding mistakes, making them far more autonomous and effective.</p>

<h1 id="safety-considerations">Safety Considerations</h1>
<p>One of the key aspects of smolagents is that it provides a custom <strong><a href="https://huggingface.co/docs/smolagents/en/tutorials/secure_code_execution">Python interpreter</a></strong> designed with security in mind. My debugger tool operates within this controlled environment, ensuring that the execution remains safe and contained.</p>

<p>It’s also important to remember that LLMs (Large Language Models) are black boxes—we can’t blindly trust them. Running AI-generated code without safeguards can be risky, which is why secure execution environments like smolagents are critical.</p>

<h1 id="conclusion">Conclusion</h1>
<p>By integrating debugging capabilities into AI agents, we take a big step toward self-improving AI systems. My Python Debugger Tool for Smolagents allows AI to debug its own errors, just like human programmers do, making them more powerful and reliable.</p>

<p>If you’re interested, you can try it out on Hugging Face Spaces and explore the Hugging Face Hub for more details. Let’s push the boundaries of AI debugging together!</p>]]></content><author><name>{&quot;avatar&quot;=&gt;&quot;/assets/images/photo.jpg&quot;, &quot;bio&quot;=&gt;&quot;Exploring ML/DL/RL/LLMs. From scratch, bit by bit. Starting coding with unit tests first, to make debugging later easier.&quot;, &quot;links&quot;=&gt;[{&quot;label&quot;=&gt;&quot;GitHub&quot;, &quot;icon&quot;=&gt;&quot;fab fa-fw fa-github&quot;, &quot;url&quot;=&gt;&quot;https://github.com/piotrgoral&quot;}, {&quot;label&quot;=&gt;&quot;LinkedIn&quot;, &quot;icon&quot;=&gt;&quot;fab fa-fw fa-linkedin&quot;, &quot;url&quot;=&gt;&quot;https://linkedin.com/piotrgoral&quot;}]}</name></author><category term="blog" /><category term="smolagents" /><category term="agent" /><category term="llm" /><category term="debuger" /><summary type="html"><![CDATA[]]></summary></entry></feed>