Skip to content

Empty attachments are a usage error, not a traceback - #1854

Merged
simonw merged 1 commit into
simonw:mainfrom
Ansansan:fix/empty-attachment-mimetype
Nov 11, 2026
Merged

simonw merged 1 commit into
simonw:mainfrom
Ansansan:fix/empty-attachment-mimetype

Conversation

@Ansansan

@Ansansan Ansansan commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #1853

printf '' | llm -m mock -a - hi (and -a with an empty file) crashed with puremagic's PureValueError: Input was empty. For empty input puremagic raises a ValueError subclass rather than PureError (with puremagic 2.2.0, and PureValueError isn't exported at the top level), so it got past both mimetype_from_string and mimetype_from_path.

Both helpers now catch (puremagic.PureError, ValueError) and return None, so the CLI shows its usual usage error:

Error: Invalid value for '-a' / '--attachment': Could not determine mimetype of stdin

Tests:

  • test_prompt_attachment_empty[stdin] and [file]: exit code 2, the error message, and the model never called
  • test_mimetype_of_empty_content: both helpers return None for empty content

All three failed before the change with the traceback from the issue. The full suite passes (1401), and black --check, mypy llm, ruff check and cog --check are clean.

Prepared with Claude Code.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes #1853. After a rebase on main, it can be merged from my side. #1857 landed after your base and also adds tests at the end of tests/test_attachments.py, so that file conflicts. I kept both blocks. Then empty stdin, an empty file and an empty pipe all give the -a usage error, exit 2. The three new tests fail with the old utils.py.

# Python 3.12.3, puremagic 2.2.0; base b9786a2daa, head e91985abbb, main 39d325492b; PYTHONPATH=<exported tree>
printf '' | python -m llm -m nosuchmodel -a - hi
  (base) -> puremagic.main.PureValueError: Input was empty   (exit 1)
  (head) -> Error: Invalid value for '-a' / '--attachment': Could not determine mimetype of stdin   (exit 2)
python -m llm -m nosuchmodel -a empty.bin hi   (head) -> ... Could not determine mimetype of empty.bin   (exit 2)
pytest tests/test_attachments.py -q   (base) -> 13 passed
pytest tests/test_attachments.py -q   (head) -> 16 passed
pytest tests/test_attachments.py -q   (head, utils.py reverted) -> 3 failed, 13 passed
# git merge-tree --write-tree origin/main e91985abbb, conflict resolved by keeping both test blocks
pytest tests/test_attachments.py -q   (merged) -> 18 passed
python -m llm -m nosuchmodel -a <(printf '') hi   (merged) -> ... Could not determine mimetype of /proc/self/fd/13   (exit 2)

non-blocking: the except in mimetype_from_path has no comment on why ValueError is there. Copy the one from mimetype_from_string?

`llm -a -` with empty stdin, or `-a` with an empty file, crashed with
puremagic's "Input was empty": for empty input puremagic raises a
ValueError subclass, not PureError, so it got past both mimetype
helpers. They now return None for it, and the CLI reports "Could not
determine mimetype of ..." as it does for any unrecognised content.

Fixes simonw#1853

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonw
simonw force-pushed the fix/empty-attachment-mimetype branch from e91985a to cecd06f Compare November 11, 2026 17:09
@simonw
simonw merged commit 1bf6323 into simonw:main Nov 11, 2026
17 checks passed
simonw added a commit that referenced this pull request Nov 11, 2026
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.

llm -a - with empty stdin crashes with a traceback

3 participants