Aider-AI/aider · error · Exception

Error loading model definition from {model_fname}: {e}

Error message

Error loading model definition from {model_fname}: {e}

What it means

register_litellm_models in aider/models.py reads each --model-metadata file (JSON5), and any failure reading, json5-parsing, or processing it is re-raised as Exception('Error loading model definition from {file}: {e}'). Unlike the settings loader, empty files are skipped gracefully, so the raise comes from real content problems: JSON5 syntax errors (trailing commas are fine in JSON5, but stray comments in wrong places, unquoted keys in strict spots, or encoding issues are not) or a non-object document (e.g. a list) being merged into local_model_metadata.

Source

Thrown at aider/models.py:1129

def register_litellm_models(model_fnames):
    files_loaded = []
    for model_fname in model_fnames:
        if not os.path.exists(model_fname):
            continue

        try:
            data = Path(model_fname).read_text()
            if not data.strip():
                continue
            model_def = json5.loads(data)
            if not model_def:
                continue

            # Defer registration with litellm to faster path.
            model_info_manager.local_model_metadata.update(model_def)
        except Exception as e:
            raise Exception(f"Error loading model definition from {model_fname}: {e}")

        files_loaded.append(model_fname)

    return files_loaded


def validate_variables(vars):
    missing = []
    for var in vars:
        if var not in os.environ:
            missing.append(var)
    if missing:
        return dict(keys_in_environment=False, missing_keys=missing)
    return dict(keys_in_environment=True, missing_keys=missing)


def sanity_check_models(io, main_model):
    problem_main = sanity_check_model(io, main_model)

View on GitHub (pinned to 5dc9490bb3)

Solutions

  1. Fix the JSON5 error at the line/column given in the wrapped parser message in the named file.
  2. Ensure the top level is an object keyed by model name, e.g. {"ollama/llama3": {"max_input_tokens": 8192}}.
  3. Validate offline first: python -c "import json5; json5.load(open('FILE'))".
  4. Empty or all-whitespace files are fine to leave, but remove scratch files you didn't mean to register.

Example fix

// before (metadata.json — trailing content, invalid)
{"my/model": {"max_input_tokens": 8192}},

// after
{"my/model": {"max_input_tokens": 8192}}
Defensive patterns

Strategy: validation

Validate before calling

import json5

def validate_model_metadata_file(path):
    data = json5.load(open(path))
    assert isinstance(data, dict), "top level must be a JSON5 object keyed by model name"
    for k, v in data.items():
        assert isinstance(v, dict), f"metadata for {k!r} must be an object"
    return True

Try / catch

try:
    register_litellm_models([path])
except Exception as e:
    if "Error loading model definition" in str(e):
        print(f"Fix JSON5 in {path}; parser detail follows the colon: {e}")
    raise

Prevention

When it happens

Trigger: Passing a .aider.model.metadata.json (or --model-metadata-file) with invalid JSON5 syntax, wrong encoding, or a top-level structure that isn't a mapping of model-name -> metadata. The update(model_def) into a dict fails or the parse raises; the message names the offending file and includes the parser's error with line/column.

Common situations: Adding local/Ollama model metadata (max_input_tokens, tokenizer info) and leaving a dangling brace or comment syntax that the json5 parser rejects; hand-merging examples from blog posts with smart quotes or BOM characters.

Related errors


AI-assisted analysis of Aider-AI/aider@5dc9490bb3 (2026-08-15). Data as JSON: /api/errors/4ee6dc731010fd6a. Report an issue: GitHub.