BerriAI/litellm · error · CustomCodeExecutionError

Custom code guardrail not compiled

Error message

Custom code guardrail not compiled

What it means

CustomCodeExecutionError raised when the custom-code guardrail executes with no compiled function and no recorded compile error — the instance's _compiled_function is None while _compile_error is also None. With the normal flow a compile is attempted before this state can be observed, so hitting this branch usually indicates an internal/lazy-compile race or state loss (e.g. a rollback in the update flow when no previously compiled function existed), and is worth a bug report rather than a config fix.

Source

Thrown at litellm/proxy/guardrails/guardrail_hooks/custom_code/custom_code_guardrail.py:227

        http_post primitives to avoid blocking the event loop.

        Args:
            inputs: Dictionary containing texts, images, tool_calls
            request_data: The original request data with metadata
            input_type: "request" for pre-call, "response" for post-call
            logging_obj: Optional logging object

        Returns:
            GenericGuardrailAPIInputs - possibly modified

        Raises:
            HTTPException: If content is blocked
            CustomCodeExecutionError: If execution fails
        """
        if self._compiled_function is None:
            if self._compile_error:
                raise CustomCodeExecutionError(f"Custom code guardrail not compiled: {self._compile_error}")
            raise CustomCodeExecutionError("Custom code guardrail not compiled")

        try:
            # Prepare inputs dict for the function

            # Prepare request_data with safe subset of information
            safe_request_data: Final = self._prepare_safe_request_data(request_data)

            # Execute the custom function - handle both sync and async functions
            result = self._compiled_function(inputs, safe_request_data, input_type)

            # If the function is async (returns a coroutine), await it
            if asyncio.iscoroutine(result):
                result = await result

            # Process the result
            return self._process_result(
                result=result,
                inputs=inputs,

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Reload/re-register the guardrail (config reload or proxy restart) so a clean compile runs
  2. Retry the request once after the reload — a transient race at first compile resolves itself
  3. If reproducible, capture proxy logs plus the guardrail config (with secrets removed) and open a litellm issue
Defensive patterns

Strategy: validation

Validate before calling

from litellm.proxy.guardrails.guardrail_hooks.custom_code.custom_code_guardrail import CustomCodeGuardrail

# compile once at registration; guards against the no-function/no-error internal state
guardrail = CustomCodeGuardrail(guardrail_name='g', custom_code=src, event_hook='pre_call', default_on=True)
guardrail._compile_custom_code()
assert guardrail._compiled_function is not None

Try / catch

from litellm.proxy.guardrails.guardrail_hooks.custom_code.custom_code_guardrail import CustomCodeExecutionError
try:
    inputs = await guardrail.apply_guardrail(inputs, request_data, 'request')
except CustomCodeExecutionError as e:
    if str(e) == 'Custom code guardrail not compiled':  # no stored reason: internal state issue
        log.error('guardrail in uninitialized state; re-registering')
        guardrail._compile_custom_code()  # one repair attempt, then re-raise if it persists
        inputs = await guardrail.apply_guardrail(inputs, request_data, 'request')
    else:
        raise

Prevention

When it happens

Trigger: A hooked request fires on a guardrail instance whose compile never ran to completion — e.g. traffic racing the very first compile, or update_custom_code rolling back when there was no old compiled function to restore.

Common situations: Rare; observed under concurrent first-request plus code update, or when a guardrail object is constructed programmatically outside the standard registration path.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/1f31e7ef8c40634c. Report an issue: GitHub.