mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
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 <fengtsai@berri.ai> Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
9ce888399f
commit
e0ff2476f0
2 changed files with 35 additions and 3 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue