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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(responses): accept pydantic tool objects returned by guardrails
mateo-berri7cbc74399d68
diff --git a/litellm/llms/openai/responses/guardrail_translation/tool_merge.py b/litellm/llms/openai/responses/guardrail_translation/tool_merge.pyindex 9a34946838a..b596adfad6f 100644--- a/litellm/llms/openai/responses/guardrail_translation/tool_merge.py+++ b/litellm/llms/openai/responses/guardrail_translation/tool_merge.py@@ -3,7 +3,7 @@ from itertools import accumulate, chain, groupbyfrom types import MappingProxyTypefrom typing import Final, TypeAlias-from pydantic import TypeAdapter, ValidationError+from pydantic import BaseModel, TypeAdapter, ValidationErrorfrom litellm._logging import verbose_loggerfrom litellm.responses.litellm_completion_transformation.transformation import (@@ -19,8 +19,9 @@ _CHAT_TOOL_TOP_LEVEL_KEYS: Final = frozenset({"type", "function"})def _as_tool(value: object) -> Tool | None:+ candidate: Final = value.model_dump(exclude_unset=True) if isinstance(value, BaseModel) else valuetry:- return _TOOL_ADAPTER.validate_python(value)+ return _TOOL_ADAPTER.validate_python(candidate)except ValidationError:return Nonediff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_tool_merge.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_tool_merge.pyindex cbcddb98235..9c236d81f51 100644--- a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_tool_merge.py+++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_tool_merge.py@@ -9,6 +9,7 @@ from litellm.llms.openai.responses.guardrail_translation.tool_merge import mergefrom litellm.responses.litellm_completion_transformation.transformation import (LiteLLMCompletionResponsesConfig,)+from litellm.types.proxy.guardrails.guardrail_hooks.generic_guardrail_api import GuardrailToolParamdef _groups(tools):@@ -166,6 +167,28 @@ def test_guardrail_output_is_read_once():assert list(merged) == original+def test_pydantic_guardrail_tools_round_trip_like_dicts():+ original = [_function("a"), {"type": "namespace", "name": "ns", "tools": [_function("x", "X doc")]}]+ groups = _groups(original)+ models = [GuardrailToolParam.model_validate(chat_tool) for chat_tool in _flat(groups)]++ merged = merge_guardrailed_tools(original, groups, models)++ assert list(merged) == original+ assert all(merged_tool is original_tool for merged_tool, original_tool in zip(merged, original))+++def test_pydantic_guardrail_edit_lands_on_the_member():+ original = [{"type": "namespace", "name": "ns", "tools": [_function("x", "X doc")]}]+ groups = _groups(original)+ edited = copy.deepcopy(_flat(groups))+ edited[0]["function"]["description"] = "EDITED"++ merged = merge_guardrailed_tools(original, groups, [GuardrailToolParam.model_validate(edited[0])])++ assert list(merged) == [{"type": "namespace", "name": "ns", "tools": [_function("x", "EDITED")]}]++def test_non_object_guardrail_items_are_dropped():original = [_function("a")]groups = _groups(original)