mudler/LocalAI · error · ValueError
Syntax error in inline reward function '{name}': {e}
Error message
Syntax error in inline reward function '{name}': {e} What it means
compile_inline_reward takes user-supplied Python source defining a function named _user_reward_<name> and runs compile() on it before exec'ing in a restricted namespace. A SyntaxError from compile() is re-raised as ValueError with the offending function name and compiler message, so the caller knows which spec failed.
Source
Thrown at backend/python/trl/reward_functions.py:171
Available modules: re, math, json, string.
"""
func_source = (
f"def _user_reward_{name}(completions, **kwargs):\n"
+ "\n".join(f" {line}" for line in code.splitlines())
)
restricted_globals = {
"__builtins__": _SAFE_BUILTINS,
"re": re,
"math": math,
"json": json,
"string": string,
}
try:
compiled = compile(func_source, f"<inline-reward-{name}>", "exec")
except SyntaxError as e:
raise ValueError(f"Syntax error in inline reward function '{name}': {e}")
exec(compiled, restricted_globals)
func = restricted_globals[f"_user_reward_{name}"]
# Validate with a quick smoke test
try:
result = func(["test"], answer=["test"])
if not isinstance(result, list):
raise ValueError(
f"Inline reward function '{name}' must return a list, got {type(result).__name__}"
)
except Exception as e:
if "must return a list" in str(e):
raise
# Other errors during smoke test are acceptable (e.g. missing kwargs)
pass
return funcView on GitHub (pinned to 44413a9d06)
Solutions
- Reproduce locally: python -c "compile(open('reward.py').read(), 'x', 'exec')" to find the exact line.
- Fix the syntax and ensure the code defines _user_reward_<name> exactly (the compile step also expects that symbol to exist afterwards).
- Check the JSON encoding of the code string — real newlines must survive as \n in the JSON payload.
Example fix
# before code = "def _user_reward_len(c, **kw) return [float(len(x)) for x in c]" # missing colon # after code = "def _user_reward_len(c, **kw):\n return [float(len(x)) for x in c]"
Defensive patterns
Strategy: validation
Validate before calling
def reward_code_compiles(name: str, code: str) -> bool:
try:
compile(code, f"<inline-reward-{name}>", "exec")
return True
except SyntaxError:
return False Try / catch
try:
func = compile_inline_reward(name, code)
except ValueError as e:
if "Syntax error" in str(e):
return validation_error(field="reward_funcs", detail=str(e))
raise Prevention
- compile() the code client-side before sending the request.
- Store reward snippets as .py files in tests so editors catch syntax errors.
- Round-trip the code through json.loads(json.dumps(code)) to verify escaping.
When it happens
Trigger: An inline reward spec whose code has a missing colon/indent, uses print without import under Python 2 habits, or was truncated by shell quoting/JSON escaping (e.g. lost newlines).
Common situations: Passing code through multiple JSON layers that eat backslash-n; writing the function body without the required def _user_reward_<name>() signature mismatch causing parse errors; copy-paste from notebooks with smart quotes.
Related errors
- Inline reward function '{name}' must return a list, got {typ
- Inline reward function '{name}' has no code
- GRPO requires at least one reward function. Specify reward_f
- reward_funcs must be a JSON array of reward function specs
- Unknown builtin reward function '{name}'. Available: {availa
AI-assisted analysis of mudler/LocalAI@44413a9d06 (2026-08-15).
Data as JSON: /api/errors/b57417feb62beee0.
Report an issue: GitHub.