Skip to content

feat: python tools requirement#1040

Open
akihikokuroda wants to merge 3 commits intogenerative-computing:mainfrom
akihikokuroda:pythonrequirement
Open

feat: python tools requirement#1040
akihikokuroda wants to merge 3 commits intogenerative-computing:mainfrom
akihikokuroda:pythonrequirement

Conversation

@akihikokuroda
Copy link
Copy Markdown
Member

@akihikokuroda akihikokuroda commented May 7, 2026

Requirement PR

Use this template when adding or modifying requirements in mellea/stdlib/requirements/.

Description

Add requirements for Python code generation.

Implementation Checklist

Base Class

  • Extends appropriate base class:
    • Requirement - standard requirement
    • ALoraRequirement - uses specialized Intrinsic/Adapter for generation-based validation

Validation Logic

  • validation_fn defined (if using Python-based validation)
    • re-usable functionality within the validation_fn should be separated out into mellea/stdlib/tools/
  • validate returns a ValidationResult with
    • a thunk and context if using a backend to generate
    • a specific reason and score when possible

Integration

  • Requirement exported in mellea/stdlib/requirements/__init__.py or, if you are adding a library of requirements, from your sub-module

Testing

  • Tests added to tests/requirements/
  • New code has 100% coverage
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used: claude

Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
@akihikokuroda akihikokuroda requested a review from a team as a code owner May 7, 2026 21:22
@github-actions github-actions Bot added the enhancement New feature or request label May 7, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

The PR description has been updated. Please fill out the template for your PR to be reviewed.

result=False,
reason="Your code creates plots with pyplot but never calls `plt.savefig()` to save them.\n\n"
"Add this before your plotting code or at the end:\n"
" plt.savefig('{output_path}')\n"
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.

I think this should match the approach in _make_output_artifacts_validator in the way it handles path/output_path.

I assume this was intended to be a f" string instead of a " string.

"Fix this by adding to the top of your code:\n"
" import matplotlib\n"
" matplotlib.use('Agg')\n\n"
"Then replace `plt.show()` with `plt.savefig('{output_path}'); plt.close()`",
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.

Same as below ie

I think this should match the approach in _make_output_artifacts_validator in the way it handles path/output_path.

I assume this was intended to be a f" string instead of a " string.

return None


def _get_unauthorized_imports(
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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it is. A new helper function is added that is used by both.

return validate


def _make_output_limit_validator(
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.

Does this have an associated test?

Bit hazy on the stdout and stderr here - in this case the ctx.last_output() is a ModelOutputThunk I think? Does that have stdout/err?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Here is what the claude found about the stdout and stderr:

Your intuition is correct. Here's the precise behavior:

  • ctx.last_output() returns ModelOutputThunk | None
  • ModelOutputThunk doesn't define stdout/stderr in its class
  • These attributes are dynamically injected at runtime when tools execute and return ExecutionResult
  • The hasattr() checks (lines 366-369) are defensive and correct — they gracefully handle both cases (tool execution with output vs. pure LLM response)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, there was no tests. New tests are added now.

Comment thread docs/examples/python_plotting_repair.py Outdated
print("=" * 70)
print("Testing Granite 4.1's ability to repair plotting failures")
print("=" * 70)
print(f"Task: Create a plot of sin(x) and save to {output_path}\n")
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.

suggest using a task variable to share the task string used in actual description with here.

Comment thread docs/examples/python_plotting_repair.py Outdated

async def main():
"""Run the canonical plotting repair example."""
import tempfile
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.

move all the imports to the top

module_name = node.module.split(".")[0]
if module_name not in allowed_imports:
unauthorized.append(module_name)
except (SyntaxError, ValueError):
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.

why not raise these?
This pass means that the function did not do its job.

Most likely this code is unusable elsewhere anyway, but if that external assumption is true it just snuck past the unauthorized import check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Comments are added explaining this.

headless_backends = ("Agg", "Svg", "Cairo", "PDF", "PS", "WebAgg", "nbAgg")
for backend in headless_backends:
if (
f"matplotlib.use('{backend}')" in code
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.

commented out code would be a false positive.

You might consider using python tokenize to strip comments.
Probably strip docstring too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added simpler way to strip the comments.

Comment thread docs/examples/python_plotting_repair.py Outdated
m = mellea.start_session()

# Create requirements bundle for plotting validation
# Allows matplotlib import (no output_path = skip file creation check)
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.

This example is too confusing. It always fail w/o output_path because there is a bunch of code to ensure that the output file exists. Then I add output_path and still consistently fail to get a file.

This might be intentional (fail to write file), but if so it needs to be clearer because right now it looks like a bad example that needs fixing/debugging.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is intentional. I added comment explaining a little better.

Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
@akihikokuroda akihikokuroda requested a review from markstur May 9, 2026 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Requirements library for the Python tool

3 participants