{"record":{"id":"4ee6dc731010fd6a","repo":"Aider-AI/aider","slug":"error-loading-model-definition-from-model-fname","errorCode":null,"errorMessage":"Error loading model definition from {model_fname}: {e}","messagePattern":"Error loading model definition from (.+?): (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"aider/models.py","lineNumber":1129,"sourceCode":"\ndef register_litellm_models(model_fnames):\n    files_loaded = []\n    for model_fname in model_fnames:\n        if not os.path.exists(model_fname):\n            continue\n\n        try:\n            data = Path(model_fname).read_text()\n            if not data.strip():\n                continue\n            model_def = json5.loads(data)\n            if not model_def:\n                continue\n\n            # Defer registration with litellm to faster path.\n            model_info_manager.local_model_metadata.update(model_def)\n        except Exception as e:\n            raise Exception(f\"Error loading model definition from {model_fname}: {e}\")\n\n        files_loaded.append(model_fname)\n\n    return files_loaded\n\n\ndef validate_variables(vars):\n    missing = []\n    for var in vars:\n        if var not in os.environ:\n            missing.append(var)\n    if missing:\n        return dict(keys_in_environment=False, missing_keys=missing)\n    return dict(keys_in_environment=True, missing_keys=missing)\n\n\ndef sanity_check_models(io, main_model):\n    problem_main = sanity_check_model(io, main_model)","sourceCodeStart":1111,"sourceCodeEnd":1147,"githubUrl":"https://github.com/Aider-AI/aider/blob/5dc9490bb35f9729ef2c95d00a19ccd30c26339c/aider/models.py#L1111-L1147","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the JSON5 error at the line/column given in the wrapped parser message in the named file.","Ensure the top level is an object keyed by model name, e.g. {\"ollama/llama3\": {\"max_input_tokens\": 8192}}.","Validate offline first: python -c \"import json5; json5.load(open('FILE'))\".","Empty or all-whitespace files are fine to leave, but remove scratch files you didn't mean to register."],"exampleFix":"// before (metadata.json — trailing content, invalid)\n{\"my/model\": {\"max_input_tokens\": 8192}},\n\n// after\n{\"my/model\": {\"max_input_tokens\": 8192}}","handlingStrategy":"validation","validationCode":"import json5\n\ndef validate_model_metadata_file(path):\n    data = json5.load(open(path))\n    assert isinstance(data, dict), \"top level must be a JSON5 object keyed by model name\"\n    for k, v in data.items():\n        assert isinstance(v, dict), f\"metadata for {k!r} must be an object\"\n    return True","typeGuard":null,"tryCatchPattern":"try:\n    register_litellm_models([path])\nexcept Exception as e:\n    if \"Error loading model definition\" in str(e):\n        print(f\"Fix JSON5 in {path}; parser detail follows the colon: {e}\")\n    raise","preventionTips":["Validate with json5.load offline before passing the file to aider.","Keep metadata files as flat objects: {\"model/name\": {\"max_input_tokens\": N}}.","Watch for smart quotes/BOM when copy-pasting examples from the web."],"tags":["json5","configuration","model-metadata","parsing","aider"],"backgroundTag":null,"analyzedSha":"5dc9490bb35f9729ef2c95d00a19ccd30c26339c","analyzedAt":"2026-08-15T05:40:10.498Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}