Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions docs/examples/tools/python_plotting_repair.py
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't been putting files directly into docs/examples. Please create a folder for this. I'm not sure what the folder should be; maybe it can go in the existing tools dir? I also see that docs/examples/as_generic_chat_history.py is in that same directory, can you please move it as well (either in this PR or a separate one).

Copy link
Copy Markdown
Contributor

@markstur markstur May 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think a user running this gets sufficient output or comments or README to explain what is happening in the example. Unless it's just my environment. If this is just a test then it should be a test. If it is an example it should be a better example than what I am seeing.

  • empty final code. (see inline suggestion)
  • output path error not explained

Here's my output:

% uv run python docs/examples/tools/python_plotting_repair.py 
=== 11:49:44-INFO ======
Starting Mellea session: backend=ollama, model=granite4.1:3b, context=ChatContext
======================================================================
Testing plotting-code repair with Python tool requirements
======================================================================
Task: Create a plot of sin(x) for x in 0..2π and save it to /var/folders/t1/4w96hyys53nbm0btnw9sjdl00000gn/T/tmpb9aik0p7/plot.png

=== 11:49:44-INFO ======
SOFAI: Starting S1 Solver (ModelIdentifier(hf_model_name='ibm-granite/granite-4.1-3b', ollama_name='granite4.1:3b', watsonx_name=None, mlx_name=None, openai_name=None, bedrock_name=None, hf_tokenizer_name=None)) loop (budget=3)
S1 Solver:   0%|                                                                                                                                                                            | 0/3 [00:00<?, ?it/s]=== 11:49:44-INFO ======
Tools for call: dict_keys(['python'])
=== 11:49:48-INFO ======
SOFAI S1: SUCCESS on attempt 1
S1 Solver:   0%|                                                                                                                                                                            | 0/3 [00:04<?, ?it/s]

Result: SUCCESS

✓ Model successfully generated and executed plotting code

Final generated code:
----------------------------------------------------------------------

----------------------------------------------------------------------

✗ Output file not found: /var/folders/t1/4w96hyys53nbm0btnw9sjdl00000gn/T/tmpb9aik0p7/plot.png

Repair iterations: 1
  ✓ Attempt 1: 9/9 requirements passed

======================================================================
Test completed
======================================================================

Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# pytest: ollama, e2e, qualitative
"""Repair plotting code with Python-tool and plotting-specific requirements."""

import tempfile
import traceback
from pathlib import Path

import mellea
from mellea.backends import ModelOption
from mellea.backends.tools import MelleaTool
from mellea.stdlib.requirements import (
python_plotting_requirements,
python_tool_requirements,
)
from mellea.stdlib.sampling import SOFAISamplingStrategy
from mellea.stdlib.tools import local_code_interpreter
from mellea.stdlib.tools.interpreter import ExecutionResult


def python(code: str) -> ExecutionResult:
"""Execute Python code.

Args:
code: Python code to execute

Returns:
Execution result containing stdout, stderr, and success status
"""
return local_code_interpreter(code)


def main():
"""Run the plotting repair example."""
with tempfile.TemporaryDirectory() as tmpdir:
output_path = str(Path(tmpdir) / "plot.png")

m = mellea.start_session(context_type="chat")

requirements = [
*python_tool_requirements(allowed_imports=["numpy", "matplotlib", "math"]),
*python_plotting_requirements(output_path=output_path),
]

sampling_strategy = SOFAISamplingStrategy(
s1_solver_backend=m.backend,
s2_solver_backend=m.backend,
s2_solver_mode="fresh_start",
loop_budget=3,
feedback_strategy="first_error",
)

task_summary = (
f"Create a plot of sin(x) for x in 0..2π and save it to {output_path}"
)

print("=" * 70)
print("Testing plotting-code repair with Python tool requirements")
print("=" * 70)
print(f"Task: {task_summary}\n")

try:
result = m.instruct(
task_summary,
requirements=requirements,
strategy=sampling_strategy,
return_sampling_results=True,
tool_calls=True,
model_options={ModelOption.TOOLS: [MelleaTool.from_callable(python)]},
)

print(f"\nResult: {'SUCCESS' if result.success else 'FAILED'}\n")

if result.success:
print("✓ Model successfully generated and executed plotting code")
print("\nFinal generated code:")
print("-" * 70)
print(result.result.value)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code comes from tool_calls:

Suggested change
print(result.result.value)
print(result.result.tool_calls['python'].args['code'])

print("-" * 70)

if Path(output_path).exists():
file_size = Path(output_path).stat().st_size
print(f"\n✓ Output file created: {output_path}")
print(f" File size: {file_size} bytes")
else:
print(f"\n✗ Output file not found: {output_path}")

print(f"\nRepair iterations: {len(result.sample_validations)}")
for attempt_idx, validations in enumerate(result.sample_validations, 1):
passed = sum(1 for _, val in validations if val.as_bool())
total = len(validations)
status = "✓" if passed == total else "✗"
print(
f" {status} Attempt {attempt_idx}: {passed}/{total} "
f"requirements passed"
)

for req, val in validations:
if not val.as_bool():
print(f" - {req.description}")
if val.reason:
reason_preview = val.reason[:100].replace("\n", " ")
print(f" Error: {reason_preview}...")

else:
print("✗ Failed to generate working plotting code after all attempts\n")
print("Last attempt output:")
print("-" * 70)
print(result.result.value)
print("-" * 70)

print(f"\nFailure history ({len(result.sample_validations)} attempts):")
for attempt_idx, validations in enumerate(result.sample_validations, 1):
failed_count = sum(1 for _, val in validations if not val.as_bool())
if failed_count > 0:
print(f"\n Attempt {attempt_idx}:")
for req, val in validations:
if not val.as_bool():
print(f" - {req.description}")
if val.reason:
reason_lines = val.reason.split("\n")[:2]
for line in reason_lines:
print(f" {line}")

except Exception as e:
print(f"✗ Exception during sampling: {e}")
traceback.print_exc()

print("\n" + "=" * 70)
print("Test completed")
print("=" * 70)


if __name__ == "__main__":
main()

# Made with Bob
4 changes: 4 additions & 0 deletions mellea/stdlib/requirements/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# Import from core for ergonomics.
from ...core import Requirement, ValidationResult, default_output_to_bool
from .md import as_markdown_list, is_markdown_list, is_markdown_table
from .plotting import python_plotting_requirements
from .python_reqs import PythonExecutionReq
from .python_tools import python_tool_requirements
from .requirement import (
ALoraRequirement,
LLMaJRequirement,
Expand All @@ -26,6 +28,8 @@
"default_output_to_bool",
"is_markdown_list",
"is_markdown_table",
"python_plotting_requirements",
"python_tool_requirements",
"req",
"reqify",
"requirement_check_to_bool",
Expand Down
9 changes: 9 additions & 0 deletions mellea/stdlib/requirements/plotting/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Plotting-specific requirements for Python tool validation.
Provides matplotlib and plotting-focused requirement factories separate from
generic Python tool requirements.
"""

from .matplotlib import python_plotting_requirements

__all__ = ["python_plotting_requirements"]
Loading
Loading