Skip to content

Fix: Handle missing tiktoken dependency in LazyLiteLLM#4850

Open
paipeline wants to merge 1 commit intoAider-AI:mainfrom
paipeline:fix/tiktoken-import-error-4848
Open

Fix: Handle missing tiktoken dependency in LazyLiteLLM#4850
paipeline wants to merge 1 commit intoAider-AI:mainfrom
paipeline:fix/tiktoken-import-error-4848

Conversation

@paipeline
Copy link
Copy Markdown

Problem

Users encounter uncaught ModuleNotFoundError for 'tiktoken' when aider tries to load litellm during model sanity checks (issue #4848). This results in a cryptic stack trace that doesn't provide actionable guidance.

Root Cause

The LazyLiteLLM._load_litellm() method imports litellm without error handling. When litellm's dependencies (like tiktoken) are missing, the import fails with an unhelpful stack trace.

Solution

Added proper error handling in _load_litellm() to:

  • Catch ImportError and ModuleNotFoundError during litellm import
  • Provide specific, helpful error message for tiktoken missing case
  • Give generic helpful message for other missing dependencies
  • Suggest reinstallation command to resolve the issue

Changes

  • Modified aider/llm.py: Added try-except block around importlib.import_module('litellm')
  • Specific error handling for tiktoken dependency
  • Clear error messages with installation instructions

Testing

Verified the fix handles both tiktoken-specific errors and general import errors gracefully, providing actionable error messages instead of stack traces.

Fixes #4848

Adds proper error handling in LazyLiteLLM._load_litellm() to gracefully
handle ModuleNotFoundError when litellm's dependencies (like tiktoken)
are missing. Provides clear error messages with installation instructions.

Fixes issue Aider-AI#4848 where users encountered uncaught ModuleNotFoundError
for 'tiktoken' when aider tried to load litellm during model sanity checks.

The fix:
- Catches ImportError and ModuleNotFoundError during litellm import
- Provides specific error message for tiktoken missing case
- Gives generic helpful message for other missing dependencies
- Suggests reinstallation command to resolve the issue

This prevents the cryptic stack trace and gives users actionable guidance.
@claui
Copy link
Copy Markdown
Contributor

claui commented Apr 2, 2026

The LazyLiteLLM._load_litellm() method imports litellm without error handling. When litellm's dependencies (like tiktoken) are missing

My two cents:

  • The upstream project already declares tiktoken correctly as a dependency.

  • As a transitive dependency, it’s already correctly declared in Aider’s own requirements.txt, too.

  • We don’t know why tiktoken was missing in the reported case. It could be any random reason. The user might have installed Aider and then pip uninstalled tiktoken, or something else entirely may have happened.

  • The corrective action (pip install) that the proposed code presents to the user is unhelpful because it assumes one specific kind of environment that might not match actual reality. Mind that aider-install uses uv and a uv-managed venv, so depending on PATH, invoking pip install is likely to not even help and might even error out (if the user’s default environment happens to be managed by the system distribution).

  • Random sanity checks scattered throughout client code for arbitrary (but mandatory) transitive dependencies makes the code unmaintainable in the long run and is counterproductive.
    Why just check specifically for tiktoken?
    Why not for all the other 100-ish transitive dependencies?

The LazyLiteLLM class should not be responsible for making sure that one single transitive dependency (out of more than 100) is present. The status quo (a stack trace) is exactly what the user needs so they can report the issue (which they correctly did.)

I propose that this PR not be merged.

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.

Uncaught ModuleNotFoundError in utils.py line 40

3 participants