{"record":{"id":"5cefb7a440edd6e5","repo":"mudler/LocalAI","slug":"engine-args-is-not-valid-json-e","errorCode":null,"errorMessage":"engine_args is not valid JSON: {e}","messagePattern":"engine_args is not valid JSON: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"backend/python/sglang/backend.py","lineNumber":125,"sourceCode":"\n    def _apply_engine_args(self, engine_kwargs: dict, engine_args_json: str) -> dict:\n        \"\"\"Merge user-supplied engine_args (JSON object) into the kwargs dict\n        that will be forwarded to ``sglang.Engine`` (which constructs a\n        ``ServerArgs`` from them).\n\n        Mirrors ``backend/python/vllm/backend.py::_apply_engine_args`` but\n        operates on the kwargs dict because sglang's ``Engine.__init__``\n        accepts ``**kwargs`` directly rather than a pre-built dataclass.\n        Validation happens against ``ServerArgs`` fields so a typo fails\n        early with a close-match suggestion instead of producing a confusing\n        ``TypeError`` deep inside engine startup.\n        \"\"\"\n        if not engine_args_json:\n            return engine_kwargs\n        try:\n            extra = json.loads(engine_args_json)\n        except json.JSONDecodeError as e:\n            raise ValueError(f\"engine_args is not valid JSON: {e}\") from e\n        if not isinstance(extra, dict):\n            raise ValueError(\n                f\"engine_args must be a JSON object, got {type(extra).__name__}\"\n            )\n        valid = {f.name for f in dataclasses.fields(ServerArgs)}\n        for key in extra:\n            if key not in valid:\n                suggestion = difflib.get_close_matches(key, valid, n=1)\n                hint = f\" did you mean {suggestion[0]!r}?\" if suggestion else \"\"\n                raise ValueError(f\"unknown engine_args key {key!r}.{hint}\")\n        engine_kwargs.update(extra)\n        return engine_kwargs\n\n    def _messages_to_dicts(self, messages) -> List[dict]:\n        result: List[dict] = []\n        for msg in messages:\n            d = {\"role\": msg.role, \"content\": msg.content or \"\"}\n            if msg.name:","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/mudler/LocalAI/blob/44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26/backend/python/sglang/backend.py#L107-L143","documentation":"Raised by the sglang backend's engine-args parser when the model config's engine_args string is not parseable JSON. The value is expected to be a JSON object serialized as a string; any JSON syntax error (trailing commas, single quotes, unquoted keys) surfaces here with the json module's position info chained.","triggerScenarios":"Setting engine_args in YAML config to single-quoted Python-dict syntax (\"{'tp_size': 2}\"), leaving a trailing comma, or breaking quoting across YAML lines so the string is truncated.","commonSituations":"Writing engine_args in model YAML where YAML and JSON quoting interact (nested quotes), hand-editing configs, or passing a dict where a JSON string is expected.","solutions":["Use strict double-quoted JSON: '{\"tp_size\": 2}' as a single-line string","Validate the string with a JSON linter or python -c 'import json,sys; json.load(open(sys.argv[1]))' before deploying","Remove trailing commas and use double quotes for keys and values"],"exampleFix":"# before (YAML)\nengine_args: \"{'tp_size': 2,}\"\n\n# after (YAML)\nengine_args: '{\"tp_size\": 2}'","handlingStrategy":"validation","validationCode":"import json\nraw = cfg.get('engine_args', '')\nif raw:\n    try:\n        json.loads(raw)\n    except json.JSONDecodeError as e:\n        raise ValueError(f'fix engine_args in model YAML: {e}') from e","typeGuard":"def is_valid_engine_args_json(s: str) -> bool:\n    try:\n        json.loads(s)\n        return True\n    except (TypeError, json.JSONDecodeError):\n        return False","tryCatchPattern":"try:\n    kwargs = _apply_engine_args(base, engine_args_json)\nexcept ValueError as err:\n    fail_config(f'model config error: {err}')  # abort startup with clear message","preventionTips":["Use double-quoted JSON on one line in YAML","Lint engine_args with a JSON parser in CI/preflight","Never use Python dict literals or trailing commas"],"tags":["python","sglang","json","configuration"],"backgroundTag":null,"analyzedSha":"44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26","analyzedAt":"2026-08-15T10:13:50.291Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}