# Fix "TypeError: __init__() got an unexpected keyword argument 'size'" in gensim 4

Yes. An agent renames size to vector_size, iter to epochs and wv.vocab to key_to_index, then trains a small word2vec on the gensim a fresh install resolves to.

[Run this on Rush](https://cloud.getrush.ai/run/fix-word2vec-unexpected-keyword-argument-size/gensim/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

gensim 4.0.0, released in March 2021, renamed the word2vec constructor's `size` to `vector_size` and the model's `iter` to `epochs`, and removed the `vocab` dictionary from `KeyedVectors`. On gensim 4.4.0 the three failures read `TypeError: Word2Vec.__init__() got an unexpected keyword argument 'size'`, `AttributeError: 'Word2Vec' object has no attribute 'iter'`, and `AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0. Use KeyedVector's .key_to_index dict, .index_to_key list, and methods .get_vecattr(key, attr) and .set_vecattr(key, attr, new_val) instead.` A training script written against 3.x hits them in that order, one per run, so the first traceback understates the work.

ChenRocks/fast_abs_rl, the code for an ACL 2018 summarization paper, is a clear case. Its `requirements.txt` is five lines, `pytorch=0.4.0`, `gensim`, `tensorboardX`, `cytoolz` and `pyrouge`; the first is conda syntax, which pip 26.3 refuses with `Invalid requirement: 'pytorch=0.4.0'` and the hint `= is not a valid operator. Did you mean == ?`, so a reader installs the packages by hand as the README suggests, and `gensim` resolves to 4.4.0. Step one of the README's training steps is `python train_word2vec.py --path=[path/to/word2vec]`. That script builds `gensim.models.Word2Vec(size=args.dim, min_count=5, workers=16, sg=1)`, trains with `model.train(sentences, total_examples=model.corpus_count, epochs=model.iter)`, and names its output files with `len(model.wv.vocab)//1000`. The embedding loader in `utils.py`, `make_embedding`, later opens the saved model with `gensim.models.Word2Vec.load(w2v_file).wv` and indexes it with `w2v[word]` and `word in w2v`, which still work on 4.x.

The agent reproduces on a clean environment first. `train_word2vec.py` imports `count_data` from `utils.py`, which imports torch, so it installs a CPU build of torch alongside gensim and cytoolz, points `DATA` at a directory with a few small JSON articles in the format the `Sentences` class reads, and runs the script: it fails at the constructor with the `size` error. It then applies the three renames, `vector_size=args.dim`, `epochs=model.epochs`, and `len(model.wv.key_to_index)`, each the documented 4.0 equivalent, and runs again until the script saves both the `.bin` model and the `.w2v` text file.

Then it checks the consumer. `make_embedding` is run against the file the script just wrote, through the same `Word2Vec.load(...).wv` path `train_abstractor.py` uses when given `--w2v`, to confirm that lookups by word and the `in` test behave on a 4.x `KeyedVectors`. The agent does not pin gensim below 4 and adds no `hasattr` branches: 3.8.3 is from May 2020, and the renamed arguments are the only API this script uses.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "This repository's word2vec training script fails with \"TypeError: __init__() got an unexpected keyword argument 'size'\" after gensim was installed fresh. Read the dependency files and the README's install instructions, report how gensim is pinned and whether the requirements file is installable with pip as written, then create a clean virtual environment on a current Python, install what the script needs (a CPU build of torch if the script imports it), and report the gensim version that was resolved. Build a small input in the format the script reads, run it, and save the traceback. Search every .py and .ipynb file for the gensim 3 API: size=, iter, wv.vocab, .vocab, index2word, and most_similar on the model instead of wv, and list each use with its file and line. Replace them with the gensim 4 equivalents (vector_size, epochs, key_to_index, index_to_key, wv.most_similar). Do not pin gensim below 4 and do not add try/except or hasattr branches. Run the script again and keep fixing removed gensim APIs until it completes and writes its output files. Then load the saved model through the repository's own loading code and confirm word lookup works. Open a pull request with the list of changed lines, the resolved versions, and the commands that now succeed.",
  "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 three renamed tokens in the training script and nothing else, plus a description that carries the proof: the gensim version a clean install resolved, the traceback before the change, every gensim 3 name found in the repository with its file and line, and the `python train_word2vec.py --path` run that now writes `word2vec.128d.0k.bin` and its `.w2v` twin from the small example corpus.

What a reviewer checks is that the renames are exact equivalents and that nothing downstream reads the removed attribute. `vector_size`, `epochs` and `key_to_index` are the names gensim's own error message points to, and the description shows the saved model opened through `utils.make_embedding` and queried by word. If the repository has notebooks or evaluation scripts that call `most_similar` on the model object rather than on `wv`, they are listed as further changes, each its own commit.

A reviewer also sees what was not done: `pytorch=0.4.0` is still conda syntax in `requirements.txt`, and the pull request says so rather than silently rewriting the dependency file, because making it pip-installable is a decision about which torch to name.

## Run it on every Monday morning

A repository whose requirements are unpinned breaks on an install day, not on a commit day. Give the Project an automation with a `schedule` trigger, for example the cron expression `0 7 * * 1` in your timezone, and a prompt that tells the agent to install the dependencies fresh, run each training entry point on a small example input, and open a pull request only when one fails on an API a new release removed. On a week when nothing broke, the run ends with a one-line answer and no pull request.

For a lab's set of paper repositories, start the request above once per repository from a script with an API key, and read each session's `pr_url` when its `run.completed` webhook arrives. Each run installs its own torch and gensim in its own sandbox, and runs past the key's `max_concurrent` limit are accepted and wait as `queued`.

## Why run it on Rush?

Three renames take a minute. What takes an afternoon on a laptop is the rest: a clean environment with a CPU torch, an example corpus in the script's format, a run that reaches the save, and the saved model loaded back through the repository's own code. An agent in a sandbox does all of it and leaves every command and its output in the session, so the pull request arrives with the evidence rather than a promise.

It also starts where a new reader starts. A machine that has carried gensim 3.8 since 2020 never shows the error; a sandbox installs what `pip install gensim` gives today, which is the only environment in which the script fails, and the one in which it has to work.

## Frequently asked questions

### Why do I get a different error after fixing size?

Because gensim 4 changed three things this script touches, and the script meets them one at a time: the `size` argument at construction, `model.iter` at training, and `wv.vocab` when it names the output file. The agent runs until the script finishes rather than stopping at the first traceback, and lists all three.

### Is vector_size the same as size?

Yes, it is a rename of the embedding dimension with the same default. `epochs` replaces `iter` the same way, and `model.epochs` carries the value that `model.iter` used to, so `epochs=model.epochs` trains for the same number of passes.

### What replaces len(model.wv.vocab)?

`len(model.wv.key_to_index)`, a dict from word to row index that gensim 4 keeps in place of the old `vocab` dict of `Vocab` objects. Counts, membership tests and lookups by word all work on it, and `index_to_key` is the list in the other direction.

### Can I pin gensim to 3.8.3 instead?

3.8.3 is from May 2020 and the renames are the only gensim 3 API this script uses, so a pin postpones a three-line change and keeps a five-year-old package next to a current torch. The request above tells the agent not to pin.

### Will models saved with gensim 3 still load?

The agent does not test that, because the repository ships no saved word2vec. It trains a fresh one with gensim 4, saves it, and loads it through the repository's own `make_embedding`; a pretrained 3.x file of your own is worth a follow-up run that loads it the same way.

## More gensim jobs

- [Fix "ImportError: cannot import name 'triu' from 'scipy.linalg'" in Gensim](https://getrush.ai/run/fix-cannot-import-name-triu-from-scipy-linalg/gensim/)
- [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/)

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

- [Fix "RuntimeError: Working outside of application context" from db.create_all() in Flask-SQLAlchemy 3](https://getrush.ai/run/fix-working-outside-of-application-context/flask-sqlalchemy/)
- [Fix "AttributeError: module 'lib' has no attribute 'X509_V_FLAG_CB_ISSUER_CHECK'" From pyOpenSSL](https://getrush.ai/run/fix-x509-v-flag-cb-issuer-check-attribute-error/pyopenssl/)
- [Fix "XLRDError: Excel xlsx file; not supported" in a Python Project](https://getrush.ai/run/fix-xlrd-xlsx-not-supported/xlrd/)
- [Fix "load() missing 1 required positional argument: 'Loader'" in PyYAML](https://getrush.ai/run/fix-yaml-load-missing-loader/pyyaml/)

## 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/)
