{"record":{"id":"033f7db76502143d","repo":"BerriAI/litellm","slug":"block-code-execution-guardrail-requires-a-guardrai","errorCode":null,"errorMessage":"Block Code Execution guardrail requires a guardrail_name","messagePattern":"Block Code Execution guardrail requires a guardrail_name","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"litellm/proxy/guardrails/guardrail_hooks/block_code_execution/__init__.py","lineNumber":44,"sourceCode":"    value: Final = getattr(litellm_params, key, default)\n    if value is not None:\n        return value\n    raw: Final = guardrail.get(\"litellm_params\")\n    if isinstance(raw, dict) and key in raw:\n        return raw[key]\n    return default\n\n\ndef initialize_guardrail(\n    litellm_params: \"LitellmParams\",\n    guardrail: \"Guardrail\",\n) -> BlockCodeExecutionGuardrail:\n    \"\"\"Initialize the Block Code Execution guardrail from config.\"\"\"\n    import litellm\n\n    guardrail_name: Final = guardrail.get(\"guardrail_name\")\n    if not guardrail_name:\n        raise ValueError(\"Block Code Execution guardrail requires a guardrail_name\")\n\n    blocked_languages: Final[list[str] | None] = cast(\n        list[str] | None,\n        _get_param(litellm_params, guardrail, \"blocked_languages\"),\n    )\n    action: Final = cast(\n        Literal[\"block\", \"mask\"],\n        _get_param(litellm_params, guardrail, \"action\", \"block\"),\n    )\n    confidence_threshold: Final = float(\n        cast(\n            int | float | str,\n            _get_param(litellm_params, guardrail, \"confidence_threshold\", 0.5),\n        )\n    )\n    detect_execution_intent: Final = bool(_get_param(litellm_params, guardrail, \"detect_execution_intent\", True))\n    mode: Final = _get_param(litellm_params, guardrail, \"mode\")\n    event_hook: Final = cast(","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/proxy/guardrails/guardrail_hooks/block_code_execution/__init__.py#L26-L62","documentation":"litellm's proxy raises this ValueError at startup/config-load time when a guardrail entry of type block_code_execution has no guardrail_name key. Every guardrail in the config's guardrails list must carry a unique guardrail_name because it is the identifier used by mode/enabled hooks, request-level guardrail selection (guardrails=[...] on a /chat/completions call), and logging. Without it the guardrail cannot be registered, so initialization aborts immediately.","triggerScenarios":"Adding a guardrails: [{litellm_params: {guardrail: block_code_execution, ...}}] entry to the proxy config without a top-level guardrail_name field, then starting litellm proxy or hot-reloading the config. initialize_guardrail() reads guardrail.get('guardrail_name'), gets None, and raises.","commonSituations":"Copy-pasting a guardrail config block from docs and dropping the name field; refactoring config where guardrail_name was accidentally nested under litellm_params (it must be a sibling, at the entry top level); YAML indentation mistakes that move guardrail_name into the wrong mapping.","solutions":["Add a unique top-level guardrail_name to the guardrail entry (sibling of litellm_params, not inside it).","Confirm the YAML structure: guardrails is a list; each item has guardrail_name plus litellm_params; run the config through a YAML linter or litellm's config validator.","If you hot-reload configs, restart the proxy after fixing so initialize_guardrail re-runs."],"exampleFix":"# before\nguardrails:\n  - litellm_params:\n      guardrail: block_code_execution\n      blocked_languages: [python, bash]\n\n# after\nguardrails:\n  - guardrail_name: block-code-execution-pre-call\n    litellm_params:\n      guardrail: block_code_execution\n      blocked_languages: [python, bash]","handlingStrategy":"validation","validationCode":"import yaml\ncfg = yaml.safe_load(open(\"config.yaml\"))\nfor g in cfg.get(\"guardrails\", []):\n    if g.get(\"litellm_params\", {}).get(\"guardrail\") == \"block_code_execution\":\n        assert isinstance(g.get(\"guardrail_name\"), str) and g[\"guardrail_name\"].strip(), (\n            f\"block_code_execution entry missing guardrail_name: {g}\")\n        assert g[\"guardrail_name\"] not in names; names.add(g[\"guardrail_name\"])","typeGuard":"from typing import Any\ndef is_valid_guardrail_entry(entry: Any) -> bool:\n    return (\n        isinstance(entry, dict)\n        and isinstance(entry.get(\"guardrail_name\"), str)\n        and bool(entry[\"guardrail_name\"].strip())\n        and isinstance(entry.get(\"litellm_params\"), dict)\n    )","tryCatchPattern":null,"preventionTips":["Add a CI step that validates guardrails entries have guardrail_name before the proxy config ships.","Keep a canonical annotated config.yaml example and diff new entries against it.","Never nest guardrail_name inside litellm_params — it is a sibling key."],"tags":["litellm","guardrails","block-code-execution","configuration","startup"],"backgroundTag":"missing-required-config-field","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}