From e0ff2476f0359d6e222c2f7997e4256938909a53 Mon Sep 17 00:00:00 2001 From: yucheng Date: Thu, 24 Sep 2026 06:47:56 +0000 Subject: [PATCH] fix(guardrails): forward truthy tools and model values in the chat handler Match the base behavior and forward non-empty tools and model values verbatim instead of dropping them when they are not the expected type. Co-authored-by: feng.tsai Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../chat/guardrail_translation/handler.py | 6 ++-- .../test_openai_chat_guardrail_handler.py | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/litellm/llms/openai/chat/guardrail_translation/handler.py b/litellm/llms/openai/chat/guardrail_translation/handler.py index b9ddf18a96c..095a3f29d2b 100644 --- a/litellm/llms/openai/chat/guardrail_translation/handler.py +++ b/litellm/llms/openai/chat/guardrail_translation/handler.py @@ -313,11 +313,11 @@ class OpenAIChatCompletionsHandler(BaseTranslation): structured_messages[index] for index in scoped_message_indices ] tools: Final = data.get("tools") - if tools and isinstance(tools, list) and not scan_only_tool_results: + if tools and not scan_only_tool_results: inputs["tools"] = cast("list[ChatCompletionToolParam]", tools) # cast-ok: raw request json model: Final = data.get("model") - if model and isinstance(model, str): - inputs["model"] = model + if model: + inputs["model"] = cast("str", model) # cast-ok: base forwarded the raw model value return inputs, structured_messages, scoped_message_indices def _not_run_reason( diff --git a/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_chat_guardrail_handler.py b/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_chat_guardrail_handler.py index f2a175ad313..16781c11297 100644 --- a/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_chat_guardrail_handler.py +++ b/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_chat_guardrail_handler.py @@ -280,3 +280,35 @@ class TestOpenAIChatHandlerAttachmentsDefaultScope: assert guardrail.inputs["texts"] == ["look"] assert "files" not in guardrail.inputs assert "images" not in guardrail.inputs + + +class TestOpenAIChatHandlerToolsAndModelForwarding: + @pytest.mark.asyncio + async def test_tools_dict_is_forwarded_verbatim_to_inputs(self): + guardrail = ScanningGuardrail() + tools = {"name": "lookup", "parameters": {"query": "str"}} + data = { + "model": "gpt-4o", + "tools": tools, + "messages": [{"role": "user", "content": "hi"}], + } + + await OpenAIChatCompletionsHandler().process_input_messages(data=data, guardrail_to_apply=guardrail) + + assert guardrail.calls == 1 + assert guardrail.inputs is not None + assert guardrail.inputs["tools"] == tools + + @pytest.mark.asyncio + async def test_model_list_is_forwarded_verbatim_to_inputs(self): + guardrail = ScanningGuardrail() + data = { + "model": ["gpt-4o"], + "messages": [{"role": "user", "content": "hi"}], + } + + await OpenAIChatCompletionsHandler().process_input_messages(data=data, guardrail_to_apply=guardrail) + + assert guardrail.calls == 1 + assert guardrail.inputs is not None + assert guardrail.inputs["model"] == ["gpt-4o"]