{"record":{"id":"7d5511653173d9ee","repo":"mudler/LocalAI","slug":"inline-reward-function-name-rejected-inline-r","errorCode":null,"errorMessage":"Inline reward function '{name}' rejected: inline reward code executes arbitrary Python and is disabled by default. Set {ALLOW_INLINE_ENV}=true on the backend to enable it (only on a trusted, access-controlled instance), or use a builtin reward function instead.","messagePattern":"Inline reward function '(.+?)' rejected: inline reward code executes arbitrary Python and is disabled by default\\. Set (.+?)=true on the backend to enable it \\(only on a trusted, access-controlled instance\\), or use a builtin reward function instead\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"backend/python/trl/reward_functions.py","lineNumber":242,"sourceCode":"                )\n            func = BUILTIN_REGISTRY[name]\n            if params:\n                # Convert string params to appropriate types\n                typed_params = {}\n                for k, v in params.items():\n                    try:\n                        typed_params[k] = int(v)\n                    except (ValueError, TypeError):\n                        try:\n                            typed_params[k] = float(v)\n                        except (ValueError, TypeError):\n                            typed_params[k] = v\n                func = functools.partial(func, **typed_params)\n            reward_funcs.append(func)\n\n        elif spec_type == \"inline\":\n            if not _inline_rewards_allowed():\n                raise ValueError(\n                    f\"Inline reward function '{name}' rejected: inline reward code \"\n                    f\"executes arbitrary Python and is disabled by default. Set \"\n                    f\"{ALLOW_INLINE_ENV}=true on the backend to enable it (only on a \"\n                    f\"trusted, access-controlled instance), or use a builtin reward \"\n                    f\"function instead.\"\n                )\n            code = spec.get(\"code\", \"\")\n            if not code.strip():\n                raise ValueError(f\"Inline reward function '{name}' has no code\")\n            func = compile_inline_reward(name, code)\n            reward_funcs.append(func)\n\n        else:\n            raise ValueError(f\"Unknown reward function type '{spec_type}'. Use 'builtin' or 'inline'\")\n\n    return reward_funcs\n","sourceCodeStart":224,"sourceCodeEnd":259,"githubUrl":"https://github.com/mudler/LocalAI/blob/44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26/backend/python/trl/reward_functions.py#L224-L259","documentation":"Inline reward specs execute arbitrary caller-supplied Python via exec, and the backend deliberately treats its _SAFE_BUILTINS allowlist as non-security (comments note trivial escapes via __subclasses__). Because the fine-tuning endpoint is unauthenticated by default, inline rewards are opt-in: the backend refuses them unless LOCALAI_TRL_ALLOW_INLINE_REWARD is set to a truthy value (1/true/yes/on).","triggerScenarios":"Sending a spec with type:'inline' while the env var is unset; setting the var on the client instead of the backend process; value 'True ' with trailing content not in the accepted set (though strip().lower() handles most).","commonSituations":"Operators unaware inline rewards are gated; Kubernetes deployments where the env var was added to the wrong container; shared instances where enabling it intentionally requires a trusted, access-controlled environment.","solutions":["If you control the instance and accept the risk, set LOCALAI_TRL_ALLOW_INLINE_REWARD=true in the backend environment and restart it.","Prefer rewriting the reward as a builtin-type spec if the logic fits an existing registry function.","If enabling, restrict API access (auth proxy/network policy) since the endpoint executes arbitrary Python."],"exampleFix":"# before: inline spec, env unset\n# after: backend env\nLOCALAI_TRL_ALLOW_INLINE_REWARD=true   # docker-compose / k8s env of the trl backend","handlingStrategy":"validation","validationCode":"import os\n\ndef inline_rewards_enabled() -> bool:\n    return os.environ.get(\"LOCALAI_TRL_ALLOW_INLINE_REWARD\", \"\").strip().lower() in (\"1\", \"true\", \"yes\", \"on\")\n\ndef spec_allowed(spec: dict) -> bool:\n    return spec.get(\"type\", \"builtin\") != \"inline\" or inline_rewards_enabled()","typeGuard":null,"tryCatchPattern":"try:\n    funcs = build_reward_functions(specs)\nexcept ValueError as e:\n    if \"disabled by default\" in str(e):\n        return error_response(str(e), hint=\"operator must opt in or use builtin\")\n    raise","preventionTips":["Default to builtin specs; treat inline as an operator-only escape hatch.","If enabling inline rewards, put auth in front of the fine-tune endpoint.","Set the env var in the backend container spec, not the client."],"tags":["trl","reward-functions","security","code-execution","configuration","localai"],"backgroundTag":null,"analyzedSha":"44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26","analyzedAt":"2026-08-15T10:13:50.291Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}