# Fix "ImportError: cannot import name 'triu' from 'scipy.linalg'" in Gensim

Yes. An agent finds the gensim and SciPy pair your install resolves to, moves gensim to a release that imports with it, and reruns your topic model to check.

[Run this on Rush](https://cloud.getrush.ai/run/fix-cannot-import-name-triu-from-scipy-linalg/gensim/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

SciPy 1.13 removed `triu` from `scipy.linalg`; the function had long been available in NumPy. Gensim releases up to 4.3.2 still import it on line 20 of `gensim/matutils.py`, and they declare no upper bound on SciPy. A project that pins `gensim==4.3.2` and leaves SciPy to the resolver therefore breaks on a fresh install without any change of its own: `ImportError: cannot import name 'triu' from 'scipy.linalg'`. The traceback runs entirely through library files, so it reads like a broken installation, and reinstalling reproduces it exactly.

a-jimenezc/topics_from_pdf, a Gradio app that extracts topics from a PDF with LDA and asks a language model to summarise them, is pinned the way many applications are. `requirements.txt` has seven exact pins, among them `gradio==4.12.0`, `gensim==4.3.2`, `nltk==3.8.1` and `langchain==0.1.5`, and no line for SciPy or NumPy. The `Dockerfile` starts from `python:3.10` and runs `pip install -r requirements.txt`, so every image build resolves SciPy again. Today, on Python 3.10, that resolves SciPy 1.15.3 with NumPy 1.26.4, and importing `src/topics_from_pdf.py` fails on its second line, `from gensim import corpora`.

The agent rebuilds that environment in a Python 3.10 virtual environment, records the resolved versions and the traceback, and then tests the two possible repairs. Raising the pin by one patch release is the first: with `gensim==4.3.3` the resolver itself moves SciPy back to 1.13.1, because that release declares the bounds 4.3.2 lacked, and the module imports. Keeping 4.3.2 and adding a `scipy<1.13` line is the second, and it also imports. The agent prefers the first because it puts the constraint where it belongs, in gensim's own metadata, and leaves one line changed. It also reports how the newest gensim behaves, which imported with SciPy 1.17 and NumPy 2 in a check on Python 3.11, as a separate and larger upgrade.

An import that succeeds is not the end of the check, because a different gensim and SciPy can change numbers. The app's model is built on line 59 of `src/topics_from_pdf.py`, `LdaModel(corpus, num_topics=num_topics, id2word=dictionary, passes=15)`, with no `random_state`, so two runs differ even on identical versions and a naive before and after comparison would prove nothing. The agent runs the function on a sample PDF with a fixed seed passed in for the test only, on the last working pair of versions and on the proposed pair, and compares the topic word lists. It does not start the chat model step, which needs a HuggingChat account, and it does not build the image, because the sandbox has no Docker.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This project fails on a fresh install with ImportError: cannot import name 'triu' from 'scipy.linalg', raised from gensim/matutils.py. Read every dependency file and Dockerfile and report how gensim, scipy and numpy are specified, including when scipy and numpy are not listed at all, and which Python version the Dockerfile or CI uses. Create a clean virtual environment on that Python version (install it with a version manager if needed), install the dependencies exactly as the Dockerfile or CI does, and report the resolved gensim, scipy and numpy versions and the full traceback from importing the module that uses gensim. Then test two fixes in separate clean environments: (1) raise gensim to the nearest patch release that imports, leaving scipy unlisted; (2) keep gensim as pinned and constrain scipy below 1.13. For each, report what the resolver chose for scipy and numpy and whether every gensim import in the code succeeds. Prefer the fix that changes one line and lets gensim's own metadata constrain scipy. Also report, without applying it, what the newest gensim release would require. Do not patch files inside site-packages and do not add a shim that defines triu. Then check behaviour: find where the code trains or loads a gensim model, run it on a small sample input with a fixed random seed supplied only in the test, in an environment with the last working versions and in the fixed environment, and compare the outputs. Skip steps that need external accounts or API keys and say which you skipped. Do not build Docker images. Open a pull request with the version table for each option, the comparison, and any saved model files in the repository that should be reloaded and checked by hand.",
  "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 one changed line in `requirements.txt` in the usual case: the gensim pin raised by a patch release. If the repository has a lockfile, it is regenerated with the project's tool and the command is named. The description carries a table with three rows, the failing state and the two repairs, each showing the SciPy and NumPy versions the resolver chose and whether the imports succeeded.

Below the table is the behaviour check: which function was run, on what input, with which seed, and how the topic word lists compared between the last working versions and the proposed ones. A reviewer reads this for the claim it supports, that the app produces equivalent topics after the change, and for its limits. If the model is trained without a seed in production, the description says that run-to-run variation already exists and is larger than anything the upgrade introduces.

The description closes with what was skipped: any step that needed an account or a key, and the image build. The first image build in CI after merging is the confirmation that the container resolves the same versions the sandbox did.

## Run it on every nightly image build

A requirements file that pins some packages and not their dependencies can break on any night a transitive dependency ships a release. Give the Project an automation with a `schedule` trigger, `every` set to `1d`, and a prompt that tells the agent to install the requirements into a clean environment on the image's Python version, import every top-level module of the application, and open a pull request only when an import fails. The pull request then arrives before the next deploy rebuilds the image, not after it.

If your CI already rebuilds the image on a schedule, react to its failure instead: an automation with a `github` trigger on `workflow_run.failure` receives the failed run as text after its prompt, and the prompt tells the agent to act when the log shows an `ImportError` raised inside an installed package.

## Why run it on Rush?

The diagnosis depends on what a resolver picks today on one specific Python version, and that cannot be read from the repository. It has to be installed. Doing it properly means three clean environments, one failing and two repaired, each with its versions recorded. An agent builds them in a sandbox with nothing cached, which is the same starting point as a Docker build, and the versions and tracebacks stay in the session where the reviewer can check them against the table.

The second half of the job is the one that usually gets skipped: showing that the model still behaves. Training a topic model twice under controlled seeds and comparing the output is not hard, but it is work with no visible result when all goes well. In a run it is simply part of the request, executed with real commands on a real input file, and its result is written into the pull request whether it is reassuring or not.

## Frequently asked questions

### Which gensim version fixes the triu import?

In a check, gensim 4.3.2 failed with SciPy 1.13.0 and later, gensim 4.3.3 imported and made the resolver choose SciPy 1.13.1, and gensim 4.4.0 imported with SciPy 1.17.1 and NumPy 2 on Python 3.11. The smallest change from a 4.3.2 pin is 4.3.3.

### Can I pin scipy below 1.13 instead?

Yes. With `gensim==4.3.2` and SciPy 1.12.0 the import succeeds. It adds a line for a package your code may not use directly, and the line has to be remembered and removed later, which is why the agent prefers moving gensim one patch release.

### Why did this break when I changed nothing?

Because SciPy was never pinned. The requirements file fixed gensim, and gensim 4.3.2 accepts any newer SciPy, so each fresh install took the latest one available for your Python version. The break arrived with the first install after SciPy 1.13 was published.

### Will my saved LDA or Word2Vec models still load?

A patch release should not change the model formats, but the agent does not assume it: it lists any model files it finds in the repository so you can reload them once on the new versions. It does not load models it cannot find, such as files kept in object storage, and says so.

### Does the agent rebuild my Docker image?

No. The sandbox has no Docker, so it reproduces the image's install step in a virtual environment on the same Python version. The pull request notes that the first image build in CI is still the final confirmation.

## More Gensim jobs

- [Fix "AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0"](https://getrush.ai/run/fix-vocab-attribute-removed-from-keyedvector/gensim/)
- [Fix "TypeError: __init__() got an unexpected keyword argument 'size'" in gensim 4](https://getrush.ai/run/fix-word2vec-unexpected-keyword-argument-size/gensim/)

## More jobs like this: Fix a bug from an alert

- [Fix "ImportError: cannot import name '_unicodefun' from 'click'" in Black](https://getrush.ai/run/fix-cannot-import-name-unicodefun-from-click/black/)
- [Fix "ImportError: cannot import name 'url_decode' from 'werkzeug.urls'" with Flask-Login 0.6.2](https://getrush.ai/run/fix-cannot-import-name-url-decode-from-werkzeug-urls/flask-login/)
- [Fix "cannot load such file -- net/smtp (LoadError)" on Ruby 3.1 and Later](https://getrush.ai/run/fix-cannot-load-such-file-net-smtp/ruby/)
- [Fix "Can't load plugin: sqlalchemy.dialects:postgres" in a Flask App](https://getrush.ai/run/fix-cant-load-plugin-sqlalchemy-dialects-postgres/sqlalchemy/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
