mudler/LocalAI · error · ValueError

Inline reward function '{name}' has no code

Error message

Inline reward function '{name}' has no code

What it means

An inline reward spec passed the enablement gate but its 'code' field is empty or whitespace-only, so there is nothing to compile into _user_reward_<name>. The check runs before compile_inline_reward, failing fast with the spec's name.

Source

Thrown at backend/python/trl/reward_functions.py:251

                        try:
                            typed_params[k] = float(v)
                        except (ValueError, TypeError):
                            typed_params[k] = v
                func = functools.partial(func, **typed_params)
            reward_funcs.append(func)

        elif spec_type == "inline":
            if not _inline_rewards_allowed():
                raise ValueError(
                    f"Inline reward function '{name}' rejected: inline reward code "
                    f"executes arbitrary Python and is disabled by default. Set "
                    f"{ALLOW_INLINE_ENV}=true on the backend to enable it (only on a "
                    f"trusted, access-controlled instance), or use a builtin reward "
                    f"function instead."
                )
            code = spec.get("code", "")
            if not code.strip():
                raise ValueError(f"Inline reward function '{name}' has no code")
            func = compile_inline_reward(name, code)
            reward_funcs.append(func)

        else:
            raise ValueError(f"Unknown reward function type '{spec_type}'. Use 'builtin' or 'inline'")

    return reward_funcs

View on GitHub (pinned to 44413a9d06)

Solutions

  1. Provide a non-empty code string defining def _user_reward_<name>(...).
  2. Verify the JSON key is exactly 'code' (lowercase).
  3. Test the spec dict before sending: assert spec.get('code','').strip().

Example fix

# before
{"type": "inline", "name": "len"}
# after
{"type": "inline", "name": "len", "code": "def _user_reward_len(c, **kw):\n    return [float(len(x)) for x in c]"}
Defensive patterns

Strategy: validation

Validate before calling

def inline_spec_has_code(spec: dict) -> bool:
    return bool(str(spec.get("code", "")).strip())

Try / catch

try:
    func = compile_inline_reward(name, code)
except ValueError as e:
    if "has no code" in str(e):
        return validation_error(field="code", message="inline reward needs a non-empty code body")
    raise

Prevention

When it happens

Trigger: spec = {'type':'inline','name':'my_reward'} with code omitted; code='' after template substitution or string stripping removed everything; JSON key typo 'Code' vs 'code'.

Common situations: Templated request builders that insert code conditionally; YAML configs where the block scalar was accidentally empty; case-sensitive key mismatch.

Related errors


AI-assisted analysis of mudler/LocalAI@44413a9d06 (2026-08-15). Data as JSON: /api/errors/a3cb66eb3ee2801b. Report an issue: GitHub.