All jobs

Fix "AttributeError: cython_sources" When Installing PyYAML 5.4.1

Yes. An agent finds the PyYAML pin that no longer builds, moves it to a release with a wheel for your Python, and proves the install in a clean environment.

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

What the agent does

The failure arrives on a day when nobody touched the project. pip install -r requirements.txt stops at PyYAML while "Getting requirements to build wheel", and the traceback ends in AttributeError: cython_sources. Current setuptools prints the longer form, AttributeError: 'build_ext' object has no attribute 'cython_sources'. Two facts combine to produce it. PyYAML 5.4.1 has no wheel for newer interpreters, so pip builds it from the source archive. That archive's pyproject.toml asks for "setuptools", "wheel", "Cython" with no upper bound, pip's isolated build environment installs the newest Cython, and PyYAML's setup.py calls self.cython_sources(...), a method that Cython 3 does not provide.

leucos/ansible-tuto, a step-by-step Ansible tutorial, shows the shape of the problem. Its requirements.txt is a freeze of 30 packages, with PyYAML==5.4.1 on line 26 beside ansible==2.10.7 and ansible-base==2.10.5, and its README tells readers to install with pip inside a virtualenv. On Python 3.9 that line is harmless: pip downloads PyYAML-5.4.1-cp39-cp39-manylinux1_x86_64.whl and compiles nothing. On Python 3.11 the same line fails with the error above, under pip and under uv alike. The agent starts there. It reads the pin, finds which interpreters the project claims to support, creates a clean environment on the one that fails, and records the failing install before changing anything.

The fix is a one-line change with a check behind it. PyYAML 6.0.1 declares Cython<3.0 in its build requirements and ships wheels for current interpreters; in a clean Python 3.11 environment it installs without compiling, with the libyaml binding present. Before moving the pin the agent looks for code that would notice the major version: any yaml.load( call without a Loader argument, which PyYAML 6 rejects. This repository has 35 YAML files and no Python source, so the only consumer is Ansible itself. The agent therefore proves the change through Ansible: ansible --version to show it imports, then ansible-playbook --syntax-check over the playbooks under step-00 to step-13. The repository's own test/run.sh needs the three Vagrant machines running, so the agent says it did not run that script and leaves it to the reviewer.

One fixed pin often uncovers the next. With PyYAML moved, the same file stops again on Python 3.11 at cffi==1.14.5, another pin from the same freeze. It has no wheel for that interpreter, so pip falls back to compiling it, and in a clean environment with no C toolchain that build fails. The agent does not quietly rewrite the whole file. It installs line by line, lists every pin that has no wheel for the target Python with the error each one gives, and proposes the smallest set of bumps that makes the file install. Where a project has a reason to stay on 5.4.1, it writes the other fix instead: a constraints file containing cython<3, passed to pip through PIP_CONSTRAINT, which makes the build environment pick an older Cython so 5.4.1 builds again.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Installing this project's Python dependencies fails while building PyYAML with \"AttributeError: cython_sources\" (newer setuptools prints \"'build_ext' object has no attribute 'cython_sources'\"). Find every file that pins PyYAML: requirements files, constraints files, setup.py, setup.cfg, pyproject.toml, lockfiles, Dockerfiles and CI workflows. Find which Python versions the project supports from its metadata, CI matrix, Dockerfiles and README. Create a clean virtual environment on the newest of those versions and reproduce the failing install first; keep the output. Then fix it: prefer moving the pin to the lowest PyYAML release that installs from a wheel on every supported Python (6.0.1 or later). Before you do, search the code for yaml.load calls without a Loader argument and for any other use of an API removed in PyYAML 6, and fix those in the same pull request. If something in the project requires PyYAML below 6, keep the pin and add a constraints file with cython<3 instead, wired into every install command through PIP_CONSTRAINT, and say why. After the change, install the whole dependency file in a new clean environment. If another pin fails to install on that Python, do not bump it silently: list each one with its error and the smallest version that installs, and include only the bumps needed for the file to install. Run the project's tests, or if it has none, import or run whatever consumes PyYAML and show the output. Do not change the Python version. Open a pull request with the failing output, the passing output, and the list of other pins you found.",
  "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 that changes the PyYAML line and nothing it cannot justify. The description opens with the install output from before the change, ending in the cython_sources traceback, and the output from after it on the same interpreter. If the project had to stay on 5.4.1, the diff instead adds the constraints file and the PIP_CONSTRAINT setting in each place dependencies are installed, such as a Dockerfile and a workflow.

Below that is the part a quick fix never includes: a table of every other pin that fails on the target Python, each with its error and the lowest version that installs. For a freeze like the one in ansible-tuto that table matters more than the PyYAML line, because it tells the maintainer whether the file can be repaired with two bumps or should be regenerated on a current interpreter.

A reviewer checks two things. First, that the new PyYAML version sits inside what the project's other dependencies accept; the pull request quotes the resolver's output so this is visible. Second, that the proof matches how the project is used. A syntax check of playbooks is weaker evidence than a full play against real hosts, and the description says which one was done.

Run it on every failed install job

This error shows up first in CI, on the day a base image or a runner moves to a newer Python. Add a step to the job that installs dependencies, set to run only on failure. It searches the step's log for cython_sources and, when it matches, sends the request above with your API key and the failing branch as the resources URL, in the form https://github.com/<owner>/<repo>/tree/<branch>. Send a request_id that is a UUID v5 of the workflow run id, so that a re-run of the job returns the run already started and does not open a second pull request.

If the key has a webhook_url, Rush posts run.completed there when the agent finishes, with the answer and a session_url. A small handler can post that answer on the commit or in your team channel. Teams with the same stale pin in many repositories can send one request per repository at once; a key runs up to its max_concurrent limit and the rest wait as queued.

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

Why run it on Rush?

A developer's machine is the worst place to study this failure, because pip keeps wheels it has built. If PyYAML 5.4.1 was compiled there before Cython 3 existed, the cached wheel is reused and the install passes, while the same command fails in a fresh container. A Rush sandbox starts with no pip cache and no leftover virtualenv, so what the agent sees is what your CI sees.

The run also keeps its evidence. Each command the agent ran, with its output, is in the session transcript: the failing build, the passing install, the line-by-line pass over the remaining pins. When the pull request says a pin cannot install on Python 3.11, the traceback that shows it is one read away, and the reviewer does not need to rebuild the environment to believe it.

Frequently asked questions

Why did the install start failing when nothing in my repository changed?

Because the build environment is not pinned by your repository. PyYAML 5.4.1 asks for Cython with no version limit, so the day Cython 3 was published every source build of 5.4.1 began to fail. Projects on a Python old enough to have a 5.4.1 wheel were not affected, which is why it often appears right after a Python upgrade.

Can I keep PyYAML 5.4.1?

Yes, by controlling the Cython used to build it. A constraints file with cython<3, passed through the PIP_CONSTRAINT environment variable, reaches pip's isolated build environment and 5.4.1 builds. Installing cython<3 yourself and passing --no-build-isolation also works. Both produced a working install in a clean Python 3.11 environment; the agent chooses the constraints file because it survives in Dockerfiles and CI.

Will moving to PyYAML 6 break my code?

The change most projects meet is that yaml.load() requires a Loader argument. The agent searches for calls without one and fixes them in the same pull request, choosing yaml.safe_load where the input is plain data. If your code never calls PyYAML directly, the check is whether the libraries that do still import and run.

It installs on my laptop. Why does it fail in Docker?

Either the Python differs or pip is reusing a wheel. Python 3.9 has a ready-made 5.4.1 wheel, so nothing is compiled there. On a newer Python, a wheel built locally in the past can sit in pip's cache and hide the failure. A fresh image has neither.

More PyYAML jobs

More jobs like this: Fix a bug from an alert

Keep reading