simonw / simonw/llm

Error in llm chat due to invalid key \e[d from case conversion in key bindings (Windows 11)

Open
#585 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
12.5k
Forks
998
Avg merge
3d 13h
Merged PRs (30d)
10

Description

Description:

I'm encountering an error when running the llm chat command on Windows. The error appears to be related to key bindings in the pyreadline3 library, where key descriptions are converted to lowercase, causing certain keys to be unrecognized.

Error Message:
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Scripts\llm.exe\__main__.py", line 7, in <module>
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\click\core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\llm\cli.py", line 346, in chat
    readline.parse_and_bind("\\e[D: backward-char")
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyreadline3\rlmain.py", line 114, in parse_and_bind
    self.mode._bind_key(key, func)
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyreadline3\modes\basemode.py", line 176, in _bind_key
    keyinfo = make_KeyPress_from_keydescr(key.lower()).tuple()
  File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyreadline3\keysyms\common.py", line 191, in make_KeyPress_from_keydescr
    raise IndexError("Not a valid key: '%s'" % keydescr)
IndexError: Not a valid key: '\e[d'

Steps to Reproduce:

Install the llm package on a Windows machine.
Run the command:

llm chat -m gpt-4o

Analysis:

The error seems to originate from the llm package attempting to bind key sequences using readline.parse_and_bind. Specifically, in llm/cli.py:

readline.parse_and_bind("\\e[D: backward-char")

In the pyreadline3 library used on Windows, the _bind_key method in basemode.py converts the key description to lowercase:

Copy code
keyinfo = make_KeyPress_from_keydescr(key.lower()).tuple()

This conversion changes '\e[D' to '\e[d', which is not recognized by pyreadline3, leading to the IndexError.

Environment:

OS: Windows 11
Python Version: 3.12.7
llm Version: 0.16
pyreadline3 Version: 3.5.4

Additional Info

The issue seems specific to Windows due to differences in how key inputs are handled compared to Unix-like systems according to chatgpt.

It also suggested to use prompt_toolkit instead of pyreadline3 but it might be a huge change.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in llm/cli.py at the readline.parse_and_bind call and reproduce the failure with llm chat -m gpt-4o on Windows 11. Check how the installed pyreadline3 version handles the key sequence and determine a compatible fix. Done means the command starts without the IndexError and key bindings work on Windows.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.