From e411f202fde0b5463987a9091cb215f547859d7e Mon Sep 17 00:00:00 2001 From: rudra717 <52209277+rudra717@users.noreply.github.com> Date: Mon, 6 Apr 2026 10:21:15 -0700 Subject: [PATCH] fix: handle string input in Azure guardrails for Responses API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Address Greptile review — the Responses API allows input as either: - A list of message objects: [{"role": "user", "content": "..."}] - A plain string: "What is the capital of France?" The previous fix passed data["input"] directly to get_user_prompt(), which would crash with AttributeError when input is a string (iterating over characters instead of message dicts). Now uses isinstance checks to handle both formats: wrap strings into a message object, pass lists directly. Co-Authored-By: Claude --- .../guardrails/guardrail_hooks/azure/prompt_shield.py | 9 ++++++++- .../guardrails/guardrail_hooks/azure/text_moderation.py | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/azure/prompt_shield.py b/litellm/proxy/guardrails/guardrail_hooks/azure/prompt_shield.py index 5f06eeb5d89..d64c786d9c6 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/azure/prompt_shield.py +++ b/litellm/proxy/guardrails/guardrail_hooks/azure/prompt_shield.py @@ -134,7 +134,14 @@ class AzureContentSafetyPromptShieldGuardrail(AzureGuardrailBase, CustomGuardrai "Azure Prompt Shield: Running pre-call prompt scan, on call_type: %s", call_type, ) - new_messages: Optional[List[AllMessageValues]] = data.get("messages") or data.get("input") + new_messages: Optional[List[AllMessageValues]] = data.get("messages") + if new_messages is None: + # Responses API uses "input" instead of "messages" + input_data = data.get("input") + if isinstance(input_data, str): + new_messages = [{"role": "user", "content": input_data}] + elif isinstance(input_data, list): + new_messages = input_data if new_messages is None: verbose_proxy_logger.warning( "Azure Prompt Shield: not running guardrail. No messages in data" diff --git a/litellm/proxy/guardrails/guardrail_hooks/azure/text_moderation.py b/litellm/proxy/guardrails/guardrail_hooks/azure/text_moderation.py index 0b15438907b..bafec44d214 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/azure/text_moderation.py +++ b/litellm/proxy/guardrails/guardrail_hooks/azure/text_moderation.py @@ -227,7 +227,14 @@ class AzureContentSafetyTextModerationGuardrail(AzureGuardrailBase, CustomGuardr "Azure Text Moderation: Running pre-call prompt scan, on call_type: %s", call_type, ) - new_messages: Optional[List[AllMessageValues]] = data.get("messages") or data.get("input") + new_messages: Optional[List[AllMessageValues]] = data.get("messages") + if new_messages is None: + # Responses API uses "input" instead of "messages" + input_data = data.get("input") + if isinstance(input_data, str): + new_messages = [{"role": "user", "content": input_data}] + elif isinstance(input_data, list): + new_messages = input_data if new_messages is None: verbose_proxy_logger.warning( "Azure Text Moderation: not running guardrail. No messages in data"