Error handling with Sand-Bob

Error handling with Sand-Bob#

As Sand-Bob handles code within sandbox containers, it cannot easily raise errors and exceptions as we are used to in Python. Instead, it returns error messages as strings.

To demonstrate this, we ask for solving an unsolvable problem: opening a file that does not exist.

from sand_bob import generate_code, config_scadsai_llm

config_scadsai_llm()
result = generate_code("load image.tif and show it", 
              dependencies=["scikit-image", "matplotlib"],
              n_codefix_attempts=0, # supress error fixing
              n_feedback_iterations=0, # supress code feedback
              final_touch=False # do not modify code when done
             )
result

Error: FileNotFoundError

An error occurred while executing the following cell:
------------------
# Load and display a TIFF image, then save the figure.
# Required libraries: scikit-image, matplotlib (already available).

import pathlib
from skimage import io
import matplotlib.pyplot as plt

# ------------------------------------------------------------------
# 1. Load the image
# ------------------------------------------------------------------
image_path = pathlib.Path("image.tif")
if not image_path.is_file():
    raise FileNotFoundError(f"Image file not found: {image_path}")

# The image may be grayscale (2‑D) or RGB (3‑D).  `io.imread` handles both.
img = io.imread(image_path)

# ------------------------------------------------------------------
# 2. Plot the image
# ------------------------------------------------------------------
plt.figure(figsize=(6, 6))
# If grayscale, set cmap='gray'
if img.ndim == 2:
    plt.imshow(img, cmap="gray")
else:
    # skimage loads RGB as (M, N, 3).  Matplotlib expects the same ordering.
    plt.imshow(img)

plt.axis("off")
plt.title(f"Image: {image_path.name}")

# Show the plot (in a Jupyter notebook this will render the figure)
plt.show()

# ------------------------------------------------------------------
# 3. Save the figure as PNG and SVG
# ------------------------------------------------------------------
output_dir = pathlib.Path("/display_output")
png_path = output_dir / "image_display.png"
svg_path = output_dir / "image_display.svg"

plt.savefig(png_path, format="png", bbox_inches="tight")
plt.savefig(svg_path, format="svg", bbox_inches="tight")

# ------------------------------------------------------------------
# 4. Output description and final result
# ------------------------------------------------------------------
print("Displayed the loaded image.")           # second‑last output (description)
print(png_path)                               # last output – only the PNG filename
------------------


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[1], line 13
      9 # 1. Load the image
     10 # ------------------------------------------------------------------
     11 image_path = pathlib.Path("image.tif")
     12 if not image_path.is_file():
---> 13     raise FileNotFoundError(f"Image file not found: {image_path}")
     14 
     15 # The image may be grayscale (2‑D) or RGB (3‑D).  `io.imread` handles both.
     16 img = io.imread(image_path)

FileNotFoundError: Image file not found: image.tif

# Load and display a TIFF image, then save the figure.
# Required libraries: scikit-image, matplotlib (already available).

import pathlib
from skimage import io
import matplotlib.pyplot as plt

# ------------------------------------------------------------------
# 1. Load the image
# ------------------------------------------------------------------
image_path = pathlib.Path("image.tif")
if not image_path.is_file():
    raise FileNotFoundError(f"Image file not found: {image_path}")

# The image may be grayscale (2‑D) or RGB (3‑D).  `io.imread` handles both.
img = io.imread(image_path)

# ------------------------------------------------------------------
# 2. Plot the image
# ------------------------------------------------------------------
plt.figure(figsize=(6, 6))
# If grayscale, set cmap='gray'
if img.ndim == 2:
    plt.imshow(img, cmap="gray")
else:
    # skimage loads RGB as (M, N, 3).  Matplotlib expects the same ordering.
    plt.imshow(img)

plt.axis("off")
plt.title(f"Image: {image_path.name}")

# Show the plot (in a Jupyter notebook this will render the figure)
plt.show()

# ------------------------------------------------------------------
# 3. Save the figure as PNG and SVG
# ------------------------------------------------------------------
output_dir = pathlib.Path("/display_output")
png_path = output_dir / "image_display.png"
svg_path = output_dir / "image_display.svg"

plt.savefig(png_path, format="png", bbox_inches="tight")
plt.savefig(svg_path, format="svg", bbox_inches="tight")

# ------------------------------------------------------------------
# 4. Output description and final result
# ------------------------------------------------------------------
print("Displayed the loaded image.")           # second‑last output (description)
print(png_path)                               # last output – only the PNG filename

Execution Details

  • Execution reason: Initial execution
  • Dependencies: scikit-image, matplotlib
  • Build Time: 0.52s
  • Run Time: 5.80s
  • Execution Time: 6.38s
  • Total Time: 17.94s
  • Traceback:
    An error occurred while executing the following cell:
    ------------------
    # Load and display a TIFF image, then save the figure.
    # Required libraries: scikit-image, matplotlib (already available).
    
    import pathlib
    from skimage import io
    import matplotlib.pyplot as plt
    
    # ------------------------------------------------------------------
    # 1. Load the image
    # ------------------------------------------------------------------
    image_path = pathlib.Path("image.tif")
    if not image_path.is_file():
        raise FileNotFoundError(f"Image file not found: {image_path}")
    
    # The image may be grayscale (2‑D) or RGB (3‑D).  `io.imread` handles both.
    img = io.imread(image_path)
    
    # ------------------------------------------------------------------
    # 2. Plot the image
    # ------------------------------------------------------------------
    plt.figure(figsize=(6, 6))
    # If grayscale, set cmap='gray'
    if img.ndim == 2:
        plt.imshow(img, cmap="gray")
    else:
        # skimage loads RGB as (M, N, 3).  Matplotlib expects the same ordering.
        plt.imshow(img)
    
    plt.axis("off")
    plt.title(f"Image: {image_path.name}")
    
    # Show the plot (in a Jupyter notebook this will render the figure)
    plt.show()
    
    # ------------------------------------------------------------------
    # 3. Save the figure as PNG and SVG
    # ------------------------------------------------------------------
    output_dir = pathlib.Path("/display_output")
    png_path = output_dir / "image_display.png"
    svg_path = output_dir / "image_display.svg"
    
    plt.savefig(png_path, format="png", bbox_inches="tight")
    plt.savefig(svg_path, format="svg", bbox_inches="tight")
    
    # ------------------------------------------------------------------
    # 4. Output description and final result
    # ------------------------------------------------------------------
    print("Displayed the loaded image.")           # second‑last output (description)
    print(png_path)                               # last output – only the PNG filename
    ------------------
    
    
    ---------------------------------------------------------------------------
    FileNotFoundError                         Traceback (most recent call last)
    Cell In[1], line 13
          9 # 1. Load the image
         10 # ------------------------------------------------------------------
         11 image_path = pathlib.Path("image.tif")
         12 if not image_path.is_file():
    ---> 13     raise FileNotFoundError(f"Image file not found: {image_path}")
         14 
         15 # The image may be grayscale (2‑D) or RGB (3‑D).  `io.imread` handles both.
         16 img = io.imread(image_path)
    
    FileNotFoundError: Image file not found: image.tif
    
    
LLM backend
TaskFunctionModel
Generate codeprompt_scadsai_llmopenai/gpt-oss-120b
Fix codeprompt_scadsai_llmopenai/gpt-oss-120b
Determine dependenciesprompt_scadsai_llmopenai/gpt-oss-120b
Generate code feedbackprompt_scadsai_llmgoogle/gemma-4-31B-it
Summarize codeprompt_scadsai_llmopenai/gpt-oss-120b
Notebook conversionprompt_scadsai_llmopenai/gpt-oss-120b

The error message can also be accessed like this:

result.error
'FileNotFoundError'
print(result.traceback)
An error occurred while executing the following cell:
------------------
# Load and display a TIFF image, then save the figure.
# Required libraries: scikit-image, matplotlib (already available).

import pathlib
from skimage import io
import matplotlib.pyplot as plt

# ------------------------------------------------------------------
# 1. Load the image
# ------------------------------------------------------------------
image_path = pathlib.Path("image.tif")
if not image_path.is_file():
    raise FileNotFoundError(f"Image file not found: {image_path}")

# The image may be grayscale (2‑D) or RGB (3‑D).  `io.imread` handles both.
img = io.imread(image_path)

# ------------------------------------------------------------------
# 2. Plot the image
# ------------------------------------------------------------------
plt.figure(figsize=(6, 6))
# If grayscale, set cmap='gray'
if img.ndim == 2:
    plt.imshow(img, cmap="gray")
else:
    # skimage loads RGB as (M, N, 3).  Matplotlib expects the same ordering.
    plt.imshow(img)

plt.axis("off")
plt.title(f"Image: {image_path.name}")

# Show the plot (in a Jupyter notebook this will render the figure)
plt.show()

# ------------------------------------------------------------------
# 3. Save the figure as PNG and SVG
# ------------------------------------------------------------------
output_dir = pathlib.Path("/display_output")
png_path = output_dir / "image_display.png"
svg_path = output_dir / "image_display.svg"

plt.savefig(png_path, format="png", bbox_inches="tight")
plt.savefig(svg_path, format="svg", bbox_inches="tight")

# ------------------------------------------------------------------
# 4. Output description and final result
# ------------------------------------------------------------------
print("Displayed the loaded image.")           # second‑last output (description)
print(png_path)                               # last output – only the PNG filename
------------------


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[1], line 13
      9 # 1. Load the image
     10 # ------------------------------------------------------------------
     11 image_path = pathlib.Path("image.tif")
     12 if not image_path.is_file():
---> 13     raise FileNotFoundError(f"Image file not found: {image_path}")
     14 
     15 # The image may be grayscale (2‑D) or RGB (3‑D).  `io.imread` handles both.
     16 img = io.imread(image_path)

FileNotFoundError: Image file not found: image.tif