Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(guardrails/model_armor): handle None metadata in post_call _process_response (#34390) (#34405)

devin-ai-integration[bot]7257d0fc89f6
diff --git a/litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py b/litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py
index 31535a5b569..28b9dec100f 100644
--- a/litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/model_armor/model_armor.py
@@ -432,7 +432,7 @@ class ModelArmorGuardrail(CustomGuardrail, VertexBase):
Override to store only the Model Armor API response, not the entire data dict.
This prevents circular references in logging.
"""
- metadata = request_data.get("metadata", {}) if isinstance(request_data, dict) else {}
+ metadata = (request_data.get("metadata") or {}) if isinstance(request_data, dict) else {}
guardrail_response = metadata.get("_model_armor_response", {})
# Determine status – default to "success" but prefer the explicit value if present.
diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py
index 4021f922877..18b5bd92411 100644
--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py
+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_model_armor.py
@@ -3680,6 +3680,22 @@ async def test_pre_call_hook_skips_chat_traffic_when_configured_for_pre_mcp_call
mock_post.assert_not_called()
+def test_process_response_with_none_metadata_does_not_crash():
+ guardrail = _make_guardrail()
+ response = {"id": "batch_123", "status": "validating"}
+ request_data = {"model": "gemini-2.5-flash", "metadata": None}
+
+ result = guardrail._process_response(
+ response=response,
+ request_data=request_data,
+ event_type=GuardrailEventHooks.post_call,
+ )
+
+ assert result is response
+ assert isinstance(request_data["metadata"], dict)
+ assert "standard_logging_guardrail_information" in request_data["metadata"]
+
+
@pytest.mark.asyncio
async def test_moderation_hook_scans_mcp_tool_call_when_configured_for_during_mcp_call():
"""A guardrail configured with mode `during_mcp_call` must scan MCP tool calls.