Skip to content

Treat empty extra-openai-models.yaml as no extra models (fixes #505)#1459

Open
jbbqqf wants to merge 1 commit into
simonw:mainfrom
jbbqqf:fix/505-empty-extra-openai-models-yaml
Open

Treat empty extra-openai-models.yaml as no extra models (fixes #505)#1459
jbbqqf wants to merge 1 commit into
simonw:mainfrom
jbbqqf:fix/505-empty-extra-openai-models-yaml

Conversation

@jbbqqf
Copy link
Copy Markdown

@jbbqqf jbbqqf commented May 23, 2026

Summary

Fixes #505 — an empty extra-openai-models.yaml file (or one that contains only whitespace or comments) crashes every llm invocation with TypeError: 'NoneType' object is not iterable.

yaml.safe_load returns None for those inputs. The previous code then ran for extra_model in extra_models: against None. The fix adds an early return when extra_models is falsy.

The original report describes the exact scenario this guards against: a user creates the file intending to come back to it later, leaves it empty, and is then locked out of every llm command until they remember the file exists and delete it.

Reproduce BEFORE/AFTER yourself (copy-paste)

# Disposable user dir so we don't touch anything real
export LLM_USER_PATH="$(mktemp -d)"
# Reproduce the trip-wires from the issue: empty file, whitespace, comments
: > "$LLM_USER_PATH/extra-openai-models.yaml"  # empty

git checkout main      # BEFORE
# Expected: TypeError: 'NoneType' object is not iterable (from openai_models.py)
python -c "import llm; llm.get_models()"

git checkout fix/505-empty-extra-openai-models-yaml   # AFTER
# Expected: no output (success — empty file is treated as "no extra models")
python -c "import llm; llm.get_models()"

What I ran locally

$ pytest tests/test_llm.py::test_extra_openai_models_empty_yaml -v
tests/test_llm.py::test_extra_openai_models_empty_yaml[]                                                                                     PASSED
tests/test_llm.py::test_extra_openai_models_empty_yaml[   \n  \n]                                                                            PASSED
tests/test_llm.py::test_extra_openai_models_empty_yaml[# this is a placeholder\n# I'll fill it in later\n]                                   PASSED

$ pytest -q
757 passed in 55.56s

The new test fails on main with the exact TypeError from the issue and passes on this branch. The other 754 tests are unchanged.

ruff check and black --check are clean on both touched files.

Edge cases

Scenario BEFORE main AFTER this PR
File absent OK (existing early-return on extra_path.exists() is false) OK (unchanged)
File present, empty (: > file) TypeError: 'NoneType' object is not iterable Treated as "no extra models", returns silently
File present, whitespace only (" \n \n") Same TypeError Same as above
File present, comments only ("# placeholder\n") Same TypeError Same as above
File present, valid list of models Models registered correctly Unchanged — existing test_openai_localai_configuration + test_extra_openai_models_async still pass
File present, malformed YAML Raises yaml.YAMLError (informative) Unchanged

PR drafted with assistance from Claude Code (Anthropic). The change was reviewed manually against llm/default_plugins/openai_models.py and tests/test_llm.py. The BEFORE/AFTER reproducer block above is the one I used during development; reviewers can paste it verbatim.

yaml.safe_load returns None for an empty / whitespace-only / comments-only
YAML file, which then crashed register_models() with
"TypeError: 'NoneType' object is not iterable" the next time llm did
anything (e.g. `llm prompt`, `llm models`).

Treat that case the same as "no extra models". Adds a regression test
covering the three trip-wires from the issue: truly empty, whitespace
only, and comments only.

Refs simonw#505
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better handling for empty extra-openai-models.yaml file

1 participant