All jobs

Fix "load() missing 1 required positional argument: 'Loader'" in PyYAML

Yes. An agent finds each yaml.load call that PyYAML 6 rejects, switches it to a safe loader, runs the code path, and opens a pull request.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The error is PyYAML 6 enforcing a warning that version 5 only printed. yaml.load(stream) with no Loader argument worked silently through PyYAML 3, printed a YAMLLoadWarning in 5.1 and later, and raises TypeError: load() missing 1 required positional argument: 'Loader' from 6.0 on. Projects meet it without changing a line, because their requirements never pinned a version. The agent begins with that file. In a tool like khast3x/Redcloud, requirements.txt is the single word PyYAML, so every fresh install takes the newest release, and the one call, data = yaml.load(stream) in list_available() in redcloud.py, has failed on every fresh install since 6.0.

It reproduces the failure before fixing it, on the code path a user takes. Here that is menu choice 6, "List templates", which opens templates/templates.yml and prints each template's title and description. The agent installs the unpinned requirement in a clean environment and calls the function, and gets the TypeError with redcloud.py, line 97 in the traceback. It notes why the program crashes instead of printing a message: the surrounding except clause catches only yaml.YAMLError, and a missing argument is not one.

Choosing the replacement is the only judgment in the fix. yaml.safe_load(stream) builds plain dictionaries, lists, strings and numbers and refuses tags that construct Python objects; yaml.load(stream, Loader=yaml.FullLoader) accepts more. The agent reads the file being loaded to decide. This one is a list of container templates written in JSON syntax under a .yml name, with nothing but strings, numbers, booleans and lists, so safe_load is correct. It confirms that by loading the file both ways and comparing the results, which are equal. It prefers safe_load for one more reason: the function exists in every PyYAML version, where yaml.FullLoader does not exist before 5.1, so the fix works for a user whose environment is old as well as one whose environment is new.

Then it runs the path again, and this is where reproducing pays off. With the loader fixed, the same menu choice fails on the next line with TypeError: string indices must be integers. The file's top level is an object with two keys, version and templates, and the loop iterates over that object, so templ is the string "version" and templ["title"] cannot work. That second defect fails the same way on PyYAML 3 and 5, which means the listing was already broken and the loader error only hid it. The agent changes the loop to walk data["templates"], runs the choice a third time, and gets 43 templates printed, the first being "Metasploit - Nightly". It does not change the shape of the file, because docker-compose.yml serves the same file to Portainer as its template list.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This project fails with: TypeError: load() missing 1 required positional argument: 'Loader'. Fix it. Read the dependency files and say which PyYAML version a fresh install resolves to and whether anything pins it. Find every yaml.load, yaml.load_all, yaml.dump and custom Loader or constructor in the code. In a clean environment with the newest PyYAML, reproduce the error by running the real code path that reaches each call, and quote the traceback. For each call, read the YAML it loads (or the code that produces it) and decide: use yaml.safe_load unless the data needs Python object tags, in which case stop and report the tags instead of choosing an unsafe loader. Show that the safe loader returns the same data as before for every YAML file in the repository. After the change, run each code path again until it completes; if another error appears behind the first one, fix it only when it is in the same function and say clearly that it is a separate defect, with evidence of whether it predates the PyYAML change. Check that the except clauses around each call catch what the loader can raise. Do not pin PyYAML to an old version as the fix. Do not start Docker or deploy anything. Open a pull request with the traceback before and the output after.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request with a two-line change in redcloud.py: yaml.safe_load(stream) in place of yaml.load(stream), and the loop over data["templates"]. The description quotes the original traceback, then the second one, then the count of templates the listing prints once both are fixed. It states that requirements.txt is left unpinned, since the fixed code runs on old and new PyYAML alike, and suggests a lower bound as an optional follow-up.

The reviewer has one decision to make, and the description frames it. The first line of the diff is the fix that was asked for. The second repairs a feature that fails the same way on PyYAML 3 and 5, so it did not work before either. A maintainer may want them as two commits, and the pull request is arranged so that splitting them is trivial. What the reviewer should not accept is the first line alone with a claim that the listing works, because the run shows it does not.

Nothing else in the tool is touched or exercised. The other menu choices shell out to docker-compose, ssh and docker-machine, and none of them reads YAML through Python. The agent says so, and says it did not run them.

Run it on every error-tracker issue with this TypeError

For a deployed service this error usually reaches you through an error tracker the morning after an image rebuild. A tracker such as Sentry signs its own webhook deliveries and cannot send the Authorization: Bearer header that a Rush webhook trigger requires, so have a small handler on your server receive the alert, keep only issues whose title contains missing 1 required positional argument: 'Loader', and forward the payload to the trigger's URL with the trigger's secret. The stack trace lands after the saved prompt as event text, which tells the agent which call to start from.

For repositories you have not deployed lately, there is nothing to alert. Search them for yaml.load( and start one run for each through the API with the prompt above, each with a request_id that is a UUID v5 of the repository name.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

The fix everyone knows is one line, and applying it by search and replace is how a team ends up with a pull request that silences the error and leaves the feature broken. The second failure here is visible only to someone who runs the code path after the first fix. The agent does that as a matter of routine, in a sandbox with a fresh install of the newest PyYAML, which is the environment the error comes from and one a developer's long-lived virtualenv often is not.

The run also leaves a record of the reasoning behind the loader choice. safe_load is right when the data is plain, and wrong when a file relies on Python object tags, and the difference is decided by reading the data. The transcript shows the file being read and both loaders compared, so a reviewer does not have to take "safe_load is fine here" on trust.

Frequently asked questions

Should I use safe_load, FullLoader or pin PyYAML below 6?

Use yaml.safe_load unless your YAML contains tags that build Python objects. It needs no Loader argument and exists in every PyYAML version. Loader=yaml.FullLoader reproduces the old default more closely but can construct more than plain data. Pinning below 6 hides the error and keeps you on releases that no longer get fixes.

Why did this start failing when I changed nothing?

An unpinned or loosely pinned PyYAML requirement resolved to 6.x on a fresh install, typically when a container image or CI cache was rebuilt. Version 5 had been warning about the call since 5.1; a warning in a log is easy to miss, and 6.0 turned it into an exception.

What breaks if I switch to safe_load?

Only documents that use tags such as !!python/object or !!python/tuple, or custom constructors registered on the default loader. safe_load raises a ConstructorError for those. The agent searches the repository's YAML for such tags and loads every file both ways before it changes the call.

The tool I'm fixing deploys containers. Does the agent run them?

No. The sandbox has no Docker installed, and this fix does not need it. The agent calls the Python function that loads the file and stops there. Anything that would start containers or open an SSH session is listed as not exercised.

More PyYAML jobs

More jobs like this: Fix a bug from an alert

Keep reading