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
"""
Unit tests for OpenAI Responses API Guardrail Translation Handler
Tests the handler's ability to process input/output for the Responses API
with guardrail transformations.
"""
import copy
import json
from collections.abc import Callable
from typing import Any, Final, List, Literal, Optional, Tuple
from unittest.mock import AsyncMock, MagicMock, patch
import logging
import pytest
from fastapi import HTTPException
from pydantic import BaseModel
from openai.types.responses import (
ResponseCustomToolCall,
ResponseCustomToolCallInputDeltaEvent,
ResponseCustomToolCallInputDoneEvent,
ResponseFunctionToolCall,
)
from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.llms import get_guardrail_translation_mapping
from litellm.llms.openai.responses.guardrail_translation.handler import (
OpenAIResponsesHandler,
)
from litellm.llms.openai.responses.guardrail_translation.tool_merge import merge_guardrailed_tools
from litellm.proxy.guardrails.guardrail_hooks.generic_guardrail_api import GenericGuardrailAPI
from litellm.types.llms.openai import ChatCompletionToolCallChunk
from litellm.responses.litellm_completion_transformation.compaction_items import compaction_summary
from litellm.responses.litellm_completion_transformation.transformation import (
LiteLLMCompletionResponsesConfig,
)
from litellm.types.llms.openai import ResponsesAPIResponse
from litellm.responses.litellm_completion_transformation.tool_search import build_tool_search_call
from litellm.types.responses.main import (
CustomToolCallOutputItem,
GenericResponseOutputItem,
OutputText,
OutputToolSearchCall,
)
from litellm.types.utils import CallTypes, GenericGuardrailAPIInputs
class MockGuardrail(CustomGuardrail):
"""Mock guardrail for testing that transforms text for requests and blocks responses"""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
"""
For requests: Append [GUARDRAILED] to text
For responses: Block by raising HTTPException (masking responses is no longer supported)
"""
texts = inputs.get("texts", [])
if input_type == "response":
# Responses should be blocked, not masked
raise HTTPException(
status_code=400,
detail={"error": "Response blocked by guardrail", "texts": texts},
)
# For requests, we can still mask/transform
inputs["texts"] = [f"{text} [GUARDRAILED]" for text in texts]
return inputs
class RecordingMaskingGuardrail(MockGuardrail):
"""MockGuardrail that also records the texts and structured message contents it was shown"""
def __init__(self, guardrail_name: str) -> None:
super().__init__(guardrail_name=guardrail_name)
self.seen_texts: list[list[str]] = []
self.seen_message_contents: list[list[object]] = []
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict[str, object],
input_type: Literal["request", "response"],
logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
self.seen_texts.append(list(inputs.get("texts", [])))
self.seen_message_contents.append([m["content"] for m in inputs.get("structured_messages") or []])
return await super().apply_guardrail(inputs, request_data, input_type, logging_obj)
class ToolCallRecordingGuardrail(CustomGuardrail):
def __init__(self, guardrail_name: str) -> None:
super().__init__(guardrail_name=guardrail_name)
self.seen_tool_calls: list[object] = []
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict[str, object],
input_type: Literal["request", "response"],
logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
self.seen_tool_calls.extend(inputs.get("tool_calls", []))
return inputs
class LastTextDroppingGuardrail(CustomGuardrail):
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict[str, object],
input_type: Literal["request", "response"],
logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
return {**inputs, "texts": list(inputs.get("texts", []))[:-1]}
class TextsReplacingGuardrail(CustomGuardrail):
"""Answers with the given texts list, or without a texts key at all when given None"""
def __init__(self, guardrail_name: str, texts: tuple[str, ...] | None) -> None:
super().__init__(guardrail_name=guardrail_name)
self.texts: Final = texts
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict[str, object],
input_type: Literal["request", "response"],
logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
answer: Final = {key: value for key, value in inputs.items() if key != "texts"}
return answer if self.texts is None else {**answer, "texts": list(self.texts)}
class PersimmonMaskingGuardrail(CustomGuardrail):
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[LiteLLMLoggingObj] = None,
) -> GenericGuardrailAPIInputs:
tool_calls = [
{
**tool_call,
"function": {
**tool_call["function"],
"arguments": tool_call["function"]["arguments"].replace("persimmon", "[MASKED]"),
},
}
for tool_call in inputs.get("tool_calls", [])
]
return {**inputs, "tool_calls": tool_calls}
class RecordingPersimmonMaskingGuardrail(PersimmonMaskingGuardrail):
def __init__(self, guardrail_name: str) -> None:
super().__init__(guardrail_name=guardrail_name)
self.seen_tool_calls: list[tuple[str, str]] = []
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
self.seen_tool_calls.extend(
(tool_call["function"]["name"], tool_call["function"]["arguments"])
for tool_call in inputs.get("tool_calls", [])
)
return await super().apply_guardrail(inputs, request_data, input_type, logging_obj)
class ArgumentsRedactingGuardrail(CustomGuardrail):
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
tool_calls = [
{**tool_call, "function": {**tool_call["function"], "arguments": "[REDACTED]"}}
for tool_call in inputs.get("tool_calls", [])
]
return {**inputs, "tool_calls": tool_calls}
class FlatShapeGuardrail(CustomGuardrail):
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[LiteLLMLoggingObj] = None,
) -> GenericGuardrailAPIInputs:
flat_tool_calls = [{"name": "exec", "input": "rm -rf /"} for _ in inputs.get("tool_calls", [])]
return {**inputs, "tool_calls": flat_tool_calls}
class DroppingGuardrail(CustomGuardrail):
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[LiteLLMLoggingObj] = None,
) -> GenericGuardrailAPIInputs:
return {**inputs, "tool_calls": []}
CUSTOM_TOOL_CALL_ITEM = {
"type": "custom_tool_call",
"id": "ctc_1",
"call_id": "call_exec_1",
"name": "exec",
"input": "echo persimmon",
"status": "completed",
}
class TestOpenAIResponsesHandlerDiscovery:
"""Test that the handler is properly discovered by the guardrail system"""
def test_handler_discovered_for_responses(self):
"""Test that handler is discovered for CallTypes.responses"""
handler_class = get_guardrail_translation_mapping(CallTypes.responses)
assert handler_class == OpenAIResponsesHandler
def test_handler_discovered_for_aresponses(self):
"""Test that handler is discovered for CallTypes.aresponses"""
handler_class = get_guardrail_translation_mapping(CallTypes.aresponses)
assert handler_class == OpenAIResponsesHandler
def test_handler_has_required_methods(self):
"""Test that handler has required methods"""
handler = OpenAIResponsesHandler()
assert hasattr(handler, "process_input_messages")
assert hasattr(handler, "process_output_response")
assert callable(handler.process_input_messages)
assert callable(handler.process_output_response)
class TestOpenAIResponsesHandlerInputProcessing:
"""Test input processing functionality"""
@pytest.mark.asyncio
async def test_process_input_string(self):
"""Test processing simple string input"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
data = {"input": "Hello world", "model": "gpt-4"}
result = await handler.process_input_messages(data, guardrail)
assert result["input"] == "Hello world [GUARDRAILED]"
assert result["model"] == "gpt-4"
@pytest.mark.asyncio
async def test_process_input_none(self):
"""Test processing when input is None"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
data = {"model": "gpt-4"}
result = await handler.process_input_messages(data, guardrail)
assert "input" not in result
assert result["model"] == "gpt-4"
@pytest.mark.asyncio
async def test_process_input_list_with_string_content(self):
"""Test processing list input with string content"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
data = {
"input": [
{"role": "user", "content": "Hello", "type": "message"},
{"role": "user", "content": "World", "type": "message"},
],
"model": "gpt-4",
}
result = await handler.process_input_messages(data, guardrail)
assert result["input"][0]["content"] == "Hello [GUARDRAILED]"
assert result["input"][1]["content"] == "World [GUARDRAILED]"
assert result["model"] == "gpt-4"
@pytest.mark.asyncio
async def test_compaction_summary_is_scanned_and_rewritten_inside_its_item(self) -> None:
affinity_prefix: Final = "litellm_enc:bW9kZWxfaWQ6ZGVwLWE=;"
data: Final = {
"input": [
{
"type": "compaction",
"id": "cmp_1",
"encrypted_content": affinity_prefix
+ json.dumps({"type": "compaction", "content": "Cat is Biscuit."}),
},
{"role": "user", "content": "Cat name?", "type": "message"},
],
"model": "claude-sonnet-5-5",
}
result: Final = await OpenAIResponsesHandler().process_input_messages(data, MockGuardrail(guardrail_name="t"))
rewritten: Final = result["input"][0]["encrypted_content"]
assert rewritten.startswith(affinity_prefix)
assert json.loads(rewritten.removeprefix(affinity_prefix)) == {
"type": "compaction",
"content": "Cat is Biscuit. [GUARDRAILED]",
}
assert result["input"][1]["content"] == "Cat name? [GUARDRAILED]"
@pytest.mark.asyncio
async def test_process_input_list_with_multimodal_content(self):
"""Test processing list input with multimodal content"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
data = {
"input": [
{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image"},
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.jpg"},
},
],
"type": "message",
}
],
"model": "gpt-4",
}
result = await handler.process_input_messages(data, guardrail)
assert result["input"][0]["content"][0]["text"] == "Describe this image [GUARDRAILED]"
# Image URL should remain unchanged
assert result["input"][0]["content"][1]["image_url"]["url"] == "https://example.com/image.jpg"
@pytest.mark.asyncio
async def test_process_input_with_empty_content(self):
"""Test processing input with empty or None content"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
data = {
"input": [
{"role": "user", "content": None, "type": "message"},
{"role": "user", "content": "", "type": "message"},
],
"model": "gpt-4",
}
result = await handler.process_input_messages(data, guardrail)
# None content should remain None
assert result["input"][0]["content"] is None
# Empty string should be processed
assert result["input"][1]["content"] == " [GUARDRAILED]"
@pytest.mark.asyncio
async def test_instructions_over_string_input_are_scanned_first_and_rewritten_in_place(self) -> None:
handler = OpenAIResponsesHandler()
guardrail = RecordingMaskingGuardrail(guardrail_name="test")
data = {"model": "gpt-4", "instructions": "Be terse", "input": "Hello"}
result = await handler.process_input_messages(data, guardrail)
assert guardrail.seen_texts == [["Be terse", "Hello"]]
assert guardrail.seen_message_contents == [["Be terse", "Hello"]]
assert result["instructions"] == "Be terse [GUARDRAILED]"
assert result["input"] == "Hello [GUARDRAILED]"
@pytest.mark.asyncio
async def test_instructions_over_list_input_are_scanned_first_and_rewritten_in_place(self) -> None:
handler = OpenAIResponsesHandler()
guardrail = RecordingMaskingGuardrail(guardrail_name="test")
data = {
"model": "gpt-4",
"instructions": "Be terse",
"input": [
{"role": "user", "content": "Hello"},
{"role": "user", "content": [{"type": "input_text", "text": "World"}]},
],
}
result = await handler.process_input_messages(data, guardrail)
assert guardrail.seen_texts == [["Be terse", "Hello", "World"]]
assert guardrail.seen_message_contents == [["Be terse", "Hello", [{"type": "text", "text": "World"}]]]
assert result["instructions"] == "Be terse [GUARDRAILED]"
assert result["input"] == [
{"role": "user", "content": "Hello [GUARDRAILED]"},
{"role": "user", "content": [{"type": "input_text", "text": "World [GUARDRAILED]"}]},
]
@pytest.mark.asyncio
async def test_empty_instructions_are_not_scanned(self) -> None:
handler = OpenAIResponsesHandler()
guardrail = RecordingMaskingGuardrail(guardrail_name="test")
data = {"model": "gpt-4", "instructions": "", "input": "Hello"}
result = await handler.process_input_messages(data, guardrail)
assert guardrail.seen_texts == [["Hello"]]
assert result["instructions"] == ""
assert result["input"] == "Hello [GUARDRAILED]"
@pytest.mark.asyncio
async def test_text_answer_missing_the_instructions_row_is_rejected_and_leaves_request_untouched(self) -> None:
from litellm.llms.base_llm.guardrail_translation.utils import UnappliableRequestRewrite
handler = OpenAIResponsesHandler()
guardrail = LastTextDroppingGuardrail(guardrail_name="dropper")
data = {"model": "gpt-4", "instructions": "Be terse", "input": [{"role": "user", "content": "Hello"}]}
original = copy.deepcopy(data)
with pytest.raises(UnappliableRequestRewrite) as excinfo:
await handler.process_input_messages(data, guardrail)
assert excinfo.value.guardrail_name == "dropper"
assert data["instructions"] == original["instructions"]
assert data["input"] == original["input"]
@pytest.mark.asyncio
@pytest.mark.parametrize("answered_texts", [None, ()], ids=["no_texts_key", "empty_texts"])
@pytest.mark.parametrize("data_input", ["Hello", [{"role": "user", "content": "Hello"}]])
async def test_answer_without_texts_leaves_instructions_and_input_untouched_like_chat_completions(
self, answered_texts: tuple[str, ...] | None, data_input: str | list[dict[str, str]]
) -> None:
handler = OpenAIResponsesHandler()
guardrail = TextsReplacingGuardrail(guardrail_name="silent", texts=answered_texts)
data = {"model": "gpt-4", "instructions": "Be terse", "input": data_input}
original = copy.deepcopy(data)
result = await handler.process_input_messages(data, guardrail)
assert result["instructions"] == original["instructions"]
assert result["input"] == original["input"]
def _skipping_system(guardrail: CustomGuardrail) -> CustomGuardrail:
guardrail.skip_system_message_in_guardrail = True
return guardrail
class TestSkipSystemMessageScopesInstructions:
"""skip_system_message_in_guardrail keeps the Responses system prompt out of the scan the same
way it keeps chat `system` messages and Anthropic top-level `system` out: instructions and
system-role input items leave both texts and structured_messages, and rewrites leave them verbatim."""
@pytest.mark.asyncio
@pytest.mark.parametrize("data_input", ["Hello", [{"role": "user", "content": "Hello"}]])
async def test_instructions_are_neither_scanned_nor_rewritten(self, data_input: str | list[dict[str, str]]) -> None:
handler = OpenAIResponsesHandler()
guardrail = _skipping_system(RecordingMaskingGuardrail(guardrail_name="test"))
data = {"model": "gpt-4", "instructions": "Be terse", "input": data_input}
result = await handler.process_input_messages(data, guardrail)
assert guardrail.seen_texts == [["Hello"]]
assert guardrail.seen_message_contents == [["Hello"]]
assert result["instructions"] == "Be terse"
rewritten = result["input"][0]["content"] if isinstance(data_input, list) else result["input"]
assert rewritten == "Hello [GUARDRAILED]"
@pytest.mark.asyncio
async def test_system_input_items_leave_scope_and_user_items_still_align_with_structured_messages(self) -> None:
handler = OpenAIResponsesHandler()
guardrail = _skipping_system(RecordingMaskingGuardrail(guardrail_name="test"))
data = {
"model": "gpt-4",
"instructions": "Be terse",
"input": [
{"role": "system", "content": "House rules"},
{"role": "developer", "content": "Dev note"},
{"role": "user", "content": [{"type": "input_text", "text": "World"}]},
],
}
result = await handler.process_input_messages(data, guardrail)
assert guardrail.seen_texts == [["Dev note", "World"]]
assert guardrail.seen_message_contents == [["Dev note", [{"type": "text", "text": "World"}]]]
assert result["instructions"] == "Be terse"
assert result["input"] == [
{"role": "system", "content": "House rules"},
{"role": "developer", "content": "Dev note [GUARDRAILED]"},
{"role": "user", "content": [{"type": "input_text", "text": "World [GUARDRAILED]"}]},
]
@pytest.mark.asyncio
async def test_only_system_content_means_nothing_is_scanned(self) -> None:
handler = OpenAIResponsesHandler()
guardrail = _skipping_system(RecordingMaskingGuardrail(guardrail_name="test"))
data = {"model": "gpt-4", "instructions": "Be terse", "input": [{"role": "system", "content": "Rules"}]}
original = copy.deepcopy(data)
result = await handler.process_input_messages(data, guardrail)
assert guardrail.seen_texts == []
assert result == original
@pytest.mark.asyncio
async def test_structured_rewrite_of_the_scoped_rows_keeps_the_skipped_system_prompt(self) -> None:
handler = OpenAIResponsesHandler()
data = {
"model": "gpt-5.6",
"instructions": "Answer from the memo only.",
"input": [
{"role": "system", "content": "House rules"},
{"role": "user", "content": "memo " * 400},
{"role": "assistant", "content": "Understood."},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, _skipping_system(StructuredRewriteGuardrail()))
assert result["instructions"] == "Answer from the memo only."
assert [(item["role"], _texts(item)) for item in result["input"]] == [
("system", ["House rules"]),
("user", [COMPRESSED_MARKER]),
("assistant", ["Understood."]),
("user", ["What is the codename?"]),
]
@pytest.mark.asyncio
async def test_full_coverage_claim_over_only_the_scoped_rows_still_keeps_the_skipped_system_prompt(self) -> None:
handler = OpenAIResponsesHandler()
data = {
"model": "gpt-5.6",
"instructions": "Answer from the memo only.",
"input": [
{"role": "system", "content": "House rules"},
{"role": "user", "content": "memo " * 400},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, _skipping_system(ScopedRowsFullCoverageGuardrail()))
assert result["instructions"] == "Answer from the memo only."
assert [(item["role"], _texts(item)) for item in result["input"]] == [
("system", ["House rules"]),
("user", [COMPRESSED_MARKER]),
("user", ["What is the codename?"]),
]
@pytest.mark.asyncio
async def test_full_coverage_claim_over_the_whole_request_is_installed_without_a_second_merge(self) -> None:
handler = OpenAIResponsesHandler()
data = {
"model": "gpt-5.6",
"instructions": "Answer from the memo only.",
"input": [
{"role": "system", "content": "House rules"},
{"role": "user", "content": "memo " * 400},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, _skipping_system(RebuildingFullCoverageGuardrail()))
assert result["instructions"] == "Answer from the memo only."
assert [(item["role"], _texts(item)) for item in result["input"]] == [
("system", ["House rules"]),
("user", [COMPRESSED_MARKER]),
("user", ["What is the codename?"]),
]
class TestOpenAIResponsesHandlerOutputProcessing:
"""Test output processing functionality"""
@pytest.mark.asyncio
async def test_process_output_response_simple(self):
"""Test processing simple output response - should block, not mask
After unified_guardrail.py changes, responses can only be blocked/rejected, not masked.
This test verifies that the guardrail properly blocks responses.
"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
# Create a mock response with dict format (works with current handler)
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[
{
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "Hello user"},
],
}
],
)
# Response should be blocked, not masked
with pytest.raises(HTTPException) as exc_info:
await handler.process_output_response(response, guardrail)
assert exc_info.value.status_code == 400
assert "Response blocked by guardrail" in str(exc_info.value.detail)
@pytest.mark.asyncio
async def test_process_output_response_multiple_items(self):
"""Test processing output response with multiple output items - should block, not mask
After unified_guardrail.py changes, responses can only be blocked/rejected, not masked.
This test verifies that the guardrail properly blocks responses with multiple items.
"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
# Use dict format (works with current handler)
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[
{
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "First message"},
],
},
{
"type": "message",
"id": "msg_124",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "Second message"},
],
},
],
)
# Response should be blocked, not masked
with pytest.raises(HTTPException) as exc_info:
await handler.process_output_response(response, guardrail)
assert exc_info.value.status_code == 400
assert "Response blocked by guardrail" in str(exc_info.value.detail)
@pytest.mark.asyncio
async def test_process_output_response_multiple_content_items(self):
"""Test processing output response with multiple content items - should block, not mask
After unified_guardrail.py changes, responses can only be blocked/rejected, not masked.
This test verifies that the guardrail properly blocks responses with multiple content items.
"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
# Use dict format (works with current handler)
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[
{
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "Part 1"},
{"type": "output_text", "text": "Part 2"},
],
}
],
)
# Response should be blocked, not masked
with pytest.raises(HTTPException) as exc_info:
await handler.process_output_response(response, guardrail)
assert exc_info.value.status_code == 400
assert "Response blocked by guardrail" in str(exc_info.value.detail)
@pytest.mark.asyncio
async def test_process_output_response_with_dict_format(self):
"""Test processing output response with dict format - should block, not mask
After unified_guardrail.py changes, responses can only be blocked/rejected, not masked.
This test verifies blocking works even when content items are dicts instead of OutputText objects.
"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
# Simulate response with dict content (which can happen in some cases)
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[
{
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "Hello from dict"},
],
}
],
)
# Response should be blocked, not masked
with pytest.raises(HTTPException) as exc_info:
await handler.process_output_response(response, guardrail)
assert exc_info.value.status_code == 400
assert "Response blocked by guardrail" in str(exc_info.value.detail)
@pytest.mark.asyncio
async def test_process_output_response_no_text_content(self):
"""Test that handler skips processing when there's no text content"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[],
)
result = await handler.process_output_response(response, guardrail)
# Should return unchanged response
assert result == response
class TestOpenAIResponsesHandlerHelperMethods:
"""Test helper methods"""
def test_has_text_content_with_text(self):
"""Test _has_text_content returns True when text exists"""
handler = OpenAIResponsesHandler()
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[
GenericResponseOutputItem(
type="message",
id="msg_123",
status="completed",
role="assistant",
content=[
OutputText(type="output_text", text="Hello", annotations=None),
],
)
],
)
assert handler._has_text_content(response) is True
def test_has_text_content_without_text(self):
"""Test _has_text_content returns False when no text exists"""
handler = OpenAIResponsesHandler()
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[],
)
assert handler._has_text_content(response) is False
def test_has_text_content_with_empty_text(self):
"""Test _has_text_content with empty text values"""
handler = OpenAIResponsesHandler()
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[
GenericResponseOutputItem(
type="message",
id="msg_123",
status="completed",
role="assistant",
content=[
OutputText(type="output_text", text="", annotations=None),
],
)
],
)
# Empty string should still return False
assert handler._has_text_content(response) is False
def test_has_text_content_with_dict_format(self):
"""Test _has_text_content with dict-based output items"""
handler = OpenAIResponsesHandler()
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[
{
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "Hello"},
],
}
],
)
assert handler._has_text_content(response) is True
class TestOpenAIResponsesHandlerEdgeCases:
"""Test edge cases and error handling"""
@pytest.mark.asyncio
async def test_process_input_with_non_list_non_string(self):
"""Test processing when input is neither string nor list"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
data = {"input": 123, "model": "gpt-4"} # Invalid type
result = await handler.process_input_messages(data, guardrail)
# Should return data unchanged
assert result["input"] == 123
@pytest.mark.asyncio
async def test_process_input_mixed_content_types(self):
"""Test processing with mixed content types in list"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
data = {
"input": [
{"role": "user", "content": "String content", "type": "message"},
{
"role": "user",
"content": [
{"type": "text", "text": "List content"},
{
"type": "image_url",
"image_url": {"url": "http://example.com"},
},
],
"type": "message",
},
],
"model": "gpt-4",
}
result = await handler.process_input_messages(data, guardrail)
assert result["input"][0]["content"] == "String content [GUARDRAILED]"
assert result["input"][1]["content"][0]["text"] == "List content [GUARDRAILED]"
@pytest.mark.asyncio
async def test_process_output_with_none_text(self):
"""Test processing output when text field is None"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
response = ResponsesAPIResponse(
id="resp_123",
created_at=1234567890,
model="gpt-4",
object="response",
status="completed",
output=[
GenericResponseOutputItem(
type="message",
id="msg_123",
status="completed",
role="assistant",
content=[
OutputText(type="output_text", text=None, annotations=None),
],
)
],
)
result = await handler.process_output_response(response, guardrail)
# Should skip processing and return unchanged
assert result == response
class TestOpenAIResponsesHandlerToolCallExtraction:
"""Test tool call extraction functionality"""
def test_extract_tool_call_from_function_call_output(self):
"""Test extracting tool calls from ResponseFunctionToolCall in response output"""
handler = OpenAIResponsesHandler()
# Create output item matching the user's provided response structure
output_item = ResponseFunctionToolCall(
arguments='{"location":"Boston, MA","unit":"celsius"}',
call_id="call_4SjsMeA6DUHwGKaE87ZojgOF",
name="get_current_weather",
type="function_call",
id="fc_0a8bd293ceb771ca00693240cb185c8196b4b4d23948c6ac88",
status="completed",
)
texts_to_check: List[str] = []
images_to_check: List[str] = []
tool_calls_to_check: List[ChatCompletionToolCallChunk] = []
task_mappings: List[Tuple[int, int]] = []
# Extract tool calls
handler._extract_output_text_and_images(
output_item=output_item,
output_idx=0,
texts_to_check=texts_to_check,
images_to_check=images_to_check,
task_mappings=task_mappings,
tool_calls_to_check=tool_calls_to_check,
)
# Verify tool call was extracted
assert len(tool_calls_to_check) == 1
assert len(texts_to_check) == 0 # No text content in tool call
# Verify tool call structure
tool_call = tool_calls_to_check[0]
assert tool_call["id"] == "call_4SjsMeA6DUHwGKaE87ZojgOF"
assert tool_call["type"] == "function"
assert tool_call["function"]["name"] == "get_current_weather"
assert tool_call["function"]["arguments"] == '{"location":"Boston, MA","unit":"celsius"}'
assert tool_call["index"] == 0
def test_extract_tool_call_from_dict_format(self):
"""Test extracting tool calls from dict representation of function call"""
handler = OpenAIResponsesHandler()
# Create output item as dict (another format that may be encountered)
output_item = {
"arguments": '{"location":"Boston, MA","unit":"celsius"}',
"call_id": "call_4SjsMeA6DUHwGKaE87ZojgOF",
"name": "get_current_weather",
"type": "function_call",
"id": "fc_0a8bd293ceb771ca00693240cb185c8196b4b4d23948c6ac88",
"status": "completed",
}
texts_to_check: List[str] = []
images_to_check: List[str] = []
tool_calls_to_check: List[Any] = []
task_mappings: List[Tuple[int, int]] = []
# Extract tool calls
handler._extract_output_text_and_images(
output_item=output_item,
output_idx=0,
texts_to_check=texts_to_check,
images_to_check=images_to_check,
task_mappings=task_mappings,
tool_calls_to_check=tool_calls_to_check,
)
# Verify tool call was extracted
assert len(tool_calls_to_check) == 1
assert len(texts_to_check) == 0 # No text content in tool call
# Verify tool call structure
tool_call = tool_calls_to_check[0]
assert tool_call["id"] == "call_4SjsMeA6DUHwGKaE87ZojgOF"
assert tool_call["type"] == "function"
assert tool_call["function"]["name"] == "get_current_weather"
assert tool_call["function"]["arguments"] == '{"location":"Boston, MA","unit":"celsius"}'
@pytest.mark.parametrize(
"output_item",
[
dict(CUSTOM_TOOL_CALL_ITEM),
CustomToolCallOutputItem(**CUSTOM_TOOL_CALL_ITEM),
ResponseCustomToolCall(**{key: value for key, value in CUSTOM_TOOL_CALL_ITEM.items() if key != "status"}),
],
ids=["dict", "litellm_typed", "openai_typed"],
)
def test_extract_custom_tool_call_input_as_arguments(self, output_item):
handler = OpenAIResponsesHandler()
texts_to_check: List[str] = []
tool_calls_to_check: List[Any] = []
handler._extract_output_text_and_images(
output_item=output_item,
output_idx=2,
texts_to_check=texts_to_check,
images_to_check=[],
task_mappings=[],
tool_calls_to_check=tool_calls_to_check,
)
assert texts_to_check == []
assert tool_calls_to_check == [
{
"id": "call_exec_1",
"type": "function",
"function": {"name": "exec", "arguments": "echo persimmon"},
"index": 2,
}
]
@pytest.mark.asyncio
@pytest.mark.parametrize("typed", [False, True], ids=["dict", "typed"])
async def test_process_output_response_writes_tool_call_rewrites_back(self, typed):
handler = OpenAIResponsesHandler()
function_call = {
"type": "function_call",
"id": "fc_1",
"call_id": "call_fn_1",
"name": "lookup_fruit",
"arguments": '{"fruit": "persimmon"}',
"status": "completed",
}
message = {
"type": "message",
"id": "msg_1",
"role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": "running persimmon", "annotations": []}],
}
payload = {
"id": "resp_1",
"created_at": 1,
"model": "gpt-5.6",
"object": "response",
"status": "completed",
"output": [message, function_call, dict(CUSTOM_TOOL_CALL_ITEM)],
}
response = ResponsesAPIResponse.model_validate(payload) if typed else payload
result = await handler.process_output_response(response, PersimmonMaskingGuardrail(guardrail_name="mask"))
output = result.output if typed else result["output"]
function_item, custom_item = output[1], output[2]
assert (function_item.arguments if typed else function_item["arguments"]) == '{"fruit": "[MASKED]"}'
assert (custom_item.input if typed else custom_item["input"]) == "echo [MASKED]"
assert (custom_item.name if typed else custom_item["name"]) == "exec"
assert (output[0].content[0].text if typed else output[0]["content"][0]["text"]) == "running persimmon"
@staticmethod
def _custom_tool_call_response(item: dict) -> dict:
return {
"id": "resp_1",
"created_at": 1,
"model": "gpt-5.6",
"object": "response",
"status": "completed",
"output": [item],
}
@pytest.mark.asyncio
async def test_process_output_response_ignores_tool_call_rewrites_in_another_shape(self):
handler = OpenAIResponsesHandler()
response = self._custom_tool_call_response(dict(CUSTOM_TOOL_CALL_ITEM))
result = await handler.process_output_response(response, FlatShapeGuardrail(guardrail_name="flat"))
assert result["output"][0]["input"] == "echo persimmon"
assert result["output"][0]["name"] == "exec"
@pytest.mark.asyncio
async def test_process_output_response_warns_when_guardrail_drops_tool_calls(self, caplog):
handler = OpenAIResponsesHandler()
response = self._custom_tool_call_response(dict(CUSTOM_TOOL_CALL_ITEM))
with caplog.at_level(logging.WARNING, logger="LiteLLM Proxy"):
result = await handler.process_output_response(response, DroppingGuardrail(guardrail_name="dropper"))
assert result["output"][0]["input"] == "echo persimmon"
assert any(
"dropper" in record.getMessage() and "0 tool calls for the 1 scanned" in record.getMessage()
for record in caplog.records
)
@pytest.mark.asyncio
async def test_process_output_response_keeps_a_nameless_custom_tool_call_nameless(self):
handler = OpenAIResponsesHandler()
nameless_item = {key: value for key, value in CUSTOM_TOOL_CALL_ITEM.items() if key != "name"}
response = self._custom_tool_call_response(nameless_item)
result = await handler.process_output_response(response, PersimmonMaskingGuardrail(guardrail_name="mask"))
assert result["output"][0]["input"] == "echo [MASKED]"
assert "name" not in result["output"][0]
@pytest.mark.asyncio
async def test_process_output_response_scans_and_rewrites_a_tool_search_call(self):
handler = OpenAIResponsesHandler()
guardrail = RecordingPersimmonMaskingGuardrail(guardrail_name="mask")
response = ResponsesAPIResponse(
id="resp_1",
created_at=1,
model="claude-sonnet-5-5",
object="response",
status="completed",
output=[build_tool_search_call("srvtoolu_1", '{"query": "persimmon"}', "completed")],
)
result = await handler.process_output_response(response, guardrail)
assert guardrail.seen_tool_calls == [("tool_search", '{"query": "persimmon"}')]
search_call = result.output[0]
assert isinstance(search_call, OutputToolSearchCall)
assert search_call.arguments == {"query": "[MASKED]"}
assert search_call.call_id == "srvtoolu_1"
assert "name" not in search_call.model_dump()
@pytest.mark.asyncio
async def test_process_output_response_never_returns_original_search_arguments_after_a_non_json_rewrite(self):
response = ResponsesAPIResponse(
id="resp_1",
created_at=1,
model="claude-sonnet-5-5",
object="response",
status="completed",
output=[build_tool_search_call("srvtoolu_1", '{"query": "persimmon"}', "completed")],
)
result = await OpenAIResponsesHandler().process_output_response(
response, ArgumentsRedactingGuardrail(guardrail_name="redact")
)
search_call = result.output[0]
assert isinstance(search_call, OutputToolSearchCall)
assert search_call.arguments == {}
assert search_call.status == "incomplete"
assert "persimmon" not in result.model_dump_json()
@pytest.mark.asyncio
async def test_process_output_response_with_tool_calls(self):
"""Test processing output response containing function tool calls"""
handler = OpenAIResponsesHandler()
guardrail = MockGuardrail(guardrail_name="test")
# Create a full response matching user's provided structure
response = ResponsesAPIResponse(
id="resp_zlasw86v56zobnneYprKIagz33tpQeh7arqL9mrI1oec47HNQLGz0VL0PpM9z67EADHExs7UjtyGqpoBKcM9oR6icMGx826UsXnlvu3ZvIyrVA1CaMgeaMo9H5DdQMhvmXtriqXpikuyYbIsko97x8GvtBIoSCcovM9s5KCwJ4eWSjfr51d6-GwLIMkCNbQI6AN11uYyIKrIfCt_9j7FZdBnRHhZ0_zE7E1LYWQPm9G9_nPmTyh9FXNLUZ9Uib1SejrCetPargnpQeBibaXqPoj_pXFKvgc-_-znG5IWEsM8WH9Pjbm6uWEwpUiCxt8yfjQGEADqaluLAts1mnzQVEhCtZbU67QG3ebSG-rXtBw511f2pJPzZ8kI4hPISmZL8Co3LmIrdpmzzb02sQRoH3v4HCwzVGXgtRwRYkdpffebYElQWzvYDhqIHFHKNavfF8mC5AVPvPRA5h1Pf3utTf26",
created_at=1764901066,
model="gpt-4.1-mini-2025-04-14",
object="response",
status="completed",
output=[
ResponseFunctionToolCall(
arguments='{"location":"Boston, MA","unit":"celsius"}',
call_id="call_4SjsMeA6DUHwGKaE87ZojgOF",
name="get_current_weather",
type="function_call",
id="fc_0a8bd293ceb771ca00693240cb185c8196b4b4d23948c6ac88",
status="completed",
)
],
)
# Response should be blocked since MockGuardrail blocks responses
with pytest.raises(HTTPException) as exc_info:
await handler.process_output_response(response, guardrail)
assert exc_info.value.status_code == 400
assert "Response blocked by guardrail" in str(exc_info.value.detail)
@pytest.mark.asyncio
async def test_namespaced_function_call_reaches_output_guardrails_in_the_unchanged_tool_call_shape(self):
guardrail: Final = ToolCallRecordingGuardrail(guardrail_name="recorder")
response: Final = ResponsesAPIResponse(
id="resp_namespaced",
created_at=1764901066,
model="gpt-5.5",
object="response",
status="completed",
output=[
ResponseFunctionToolCall(
arguments='{"city":"Paris"}',
call_id="call_namespaced",
name="get_weather",
namespace="weather_tools",
type="function_call",
id="fc_namespaced",
status="completed",
)
],
)
await OpenAIResponsesHandler().process_output_response(response, guardrail)
assert guardrail.seen_tool_calls == [
{
"id": "call_namespaced",
"type": "function",
"function": {"name": "get_weather", "arguments": '{"city":"Paris"}'},
"index": 0,
}
]
def test_extract_mixed_content_with_text_and_tool_calls(self):
"""Test extracting both text and tool calls from response"""
handler = OpenAIResponsesHandler()
# Create a response with both text and tool call outputs
texts_to_check: List[str] = []
images_to_check: List[str] = []
tool_calls_to_check: List[Any] = []
task_mappings: List[Tuple[int, int]] = []
# First extract from a message output
text_output = {
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "I'll check the weather for you"},
],
}
handler._extract_output_text_and_images(
output_item=text_output,
output_idx=0,
texts_to_check=texts_to_check,
images_to_check=images_to_check,
task_mappings=task_mappings,
tool_calls_to_check=tool_calls_to_check,
)
# Then extract from a tool call output
tool_call_output = ResponseFunctionToolCall(
arguments='{"location":"Boston, MA","unit":"celsius"}',
call_id="call_4SjsMeA6DUHwGKaE87ZojgOF",
name="get_current_weather",
type="function_call",
id="fc_0a8bd293ceb771ca00693240cb185c8196b4b4d23948c6ac88",
status="completed",
)
handler._extract_output_text_and_images(
output_item=tool_call_output,
output_idx=1,
texts_to_check=texts_to_check,
images_to_check=images_to_check,
task_mappings=task_mappings,
tool_calls_to_check=tool_calls_to_check,
)
# Verify both were extracted
assert len(texts_to_check) == 1
assert texts_to_check[0] == "I'll check the weather for you"
assert len(tool_calls_to_check) == 1
assert tool_calls_to_check[0]["function"]["name"] == "get_current_weather"
def test_extract_text_from_basemodel_instance(self):
"""Test extracting text from GenericResponseOutputItem as BaseModel instance
This test verifies that _extract_output_text_and_images correctly handles
GenericResponseOutputItem when passed as a Pydantic BaseModel instance
(not as a dict). This addresses the issue where isinstance(output_item, BaseModel)
was failing because the handler was importing BaseModel from openai instead of pydantic.
"""
handler = OpenAIResponsesHandler()
# Create a proper GenericResponseOutputItem instance (Pydantic BaseModel)
output_item = GenericResponseOutputItem(
type="message",
id="msg_123",
status="completed",
role="assistant",
content=[
OutputText(
type="output_text",
text="Hi! My name is Ishaan.",
annotations=[],
)
],
)
texts_to_check: List[str] = []
images_to_check: List[str] = []
tool_calls_to_check: List[Any] = []
task_mappings: List[Tuple[int, int]] = []
# Extract text from the BaseModel instance
handler._extract_output_text_and_images(
output_item=output_item,
output_idx=0,
texts_to_check=texts_to_check,
images_to_check=images_to_check,
task_mappings=task_mappings,
tool_calls_to_check=tool_calls_to_check,
)
# Verify text was extracted correctly
assert len(texts_to_check) == 1
assert texts_to_check[0] == "Hi! My name is Ishaan."
assert len(task_mappings) == 1
assert task_mappings[0] == (0, 0) # (output_idx, content_idx)
assert len(tool_calls_to_check) == 0 # No tool calls in this output
def test_extract_text_from_basemodel_with_multiple_content_items(self):
"""Test extracting multiple text items from GenericResponseOutputItem BaseModel
This test verifies that the handler correctly processes a BaseModel instance
with multiple content items in the content array.
"""
handler = OpenAIResponsesHandler()
# Create GenericResponseOutputItem with multiple content items
output_item = GenericResponseOutputItem(
type="message",
id="msg_456",
status="completed",
role="assistant",
content=[
OutputText(
type="output_text",
text="First paragraph.",
annotations=[],
),
OutputText(
type="output_text",
text="Second paragraph.",
annotations=[],
),
OutputText(
type="output_text",
text="Third paragraph.",
annotations=[],
),
],
)
texts_to_check: List[str] = []
images_to_check: List[str] = []
tool_calls_to_check: List[Any] = []
task_mappings: List[Tuple[int, int]] = []
# Extract all text items
handler._extract_output_text_and_images(
output_item=output_item,
output_idx=0,
texts_to_check=texts_to_check,
images_to_check=images_to_check,
task_mappings=task_mappings,
tool_calls_to_check=tool_calls_to_check,
)
# Verify all text items were extracted
assert len(texts_to_check) == 3
assert texts_to_check[0] == "First paragraph."
assert texts_to_check[1] == "Second paragraph."
assert texts_to_check[2] == "Third paragraph."
assert len(task_mappings) == 3
assert task_mappings[0] == (0, 0)
assert task_mappings[1] == (0, 1)
assert task_mappings[2] == (0, 2)
class MockPassThroughGuardrail(CustomGuardrail):
"""Mock guardrail that passes through without blocking - for testing streaming fallback behavior"""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
"""Simply return inputs unchanged"""
return inputs
class MockRecordingGuardrail(MockPassThroughGuardrail):
"""Pass-through guardrail that records every apply_guardrail inputs payload"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.seen_inputs: List[GenericGuardrailAPIInputs] = []
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
self.seen_inputs.append(inputs)
return inputs
class TestOpenAIResponsesHandlerStreamingOutputProcessing:
"""Test streaming output processing functionality"""
@pytest.mark.asyncio
async def test_process_output_streaming_response_empty_output(self):
"""Test that streaming response with empty output doesn't raise IndexError
This test verifies the fix for the bug where accessing model_response_choices[0]
would raise IndexError when the response.completed event has an empty output array.
"""
handler = OpenAIResponsesHandler()
guardrail = MockPassThroughGuardrail(guardrail_name="test")
# Simulate a response.completed streaming event with empty output
responses_so_far = [
{
"type": "response.completed",
"response": {
"id": "resp_123",
"output": [], # Empty output - this was causing the IndexError
"status": "completed",
},
}
]
# This should not raise IndexError
result = await handler.process_output_streaming_response(
responses_so_far=responses_so_far,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
)
# Should return the responses unchanged
assert result == responses_so_far
@pytest.mark.asyncio
async def test_process_output_streaming_response_missing_output_key(self):
"""Test that streaming response with missing output key doesn't raise IndexError
This test verifies the handler gracefully handles when the response dict
doesn't contain an 'output' key at all.
"""
handler = OpenAIResponsesHandler()
guardrail = MockPassThroughGuardrail(guardrail_name="test")
# Simulate a response.completed streaming event with missing output key
responses_so_far = [
{
"type": "response.completed",
"response": {
"id": "resp_123",
"status": "completed",
# No 'output' key - get() will return []
},
}
]
# This should not raise IndexError
result = await handler.process_output_streaming_response(
responses_so_far=responses_so_far,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
)
# Should return the responses unchanged
assert result == responses_so_far
@pytest.mark.asyncio
async def test_process_output_streaming_response_null_response(self):
handler = OpenAIResponsesHandler()
guardrail = MockPassThroughGuardrail(guardrail_name="test")
responses_so_far = [{"type": "response.completed", "response": None}]
result = await handler.process_output_streaming_response(
responses_so_far=responses_so_far,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
)
assert result == responses_so_far
@pytest.mark.asyncio
async def test_process_output_streaming_response_unrecognized_output_type(self):
"""Test that streaming response with unrecognized output types doesn't raise IndexError
This test verifies the handler gracefully handles when output items are of
unrecognized types that _convert_response_output_to_choices skips over.
"""
handler = OpenAIResponsesHandler()
guardrail = MockPassThroughGuardrail(guardrail_name="test")
# Simulate a response.completed streaming event with unrecognized output type
responses_so_far = [
{
"type": "response.completed",
"response": {
"id": "resp_123",
"output": [
{
"type": "unknown_type", # Unrecognized type
"id": "item_123",
"data": "some data",
}
],
"status": "completed",
},
}
]
# This should not raise IndexError
result = await handler.process_output_streaming_response(
responses_so_far=responses_so_far,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
)
# Should return the responses unchanged
assert result == responses_so_far
@pytest.mark.asyncio
async def test_process_output_streaming_response_with_valid_output(self):
"""Test that streaming response with valid output still works correctly"""
handler = OpenAIResponsesHandler()
guardrail = MockPassThroughGuardrail(guardrail_name="test")
# Simulate a response.completed streaming event with valid message output
responses_so_far = [
{
"type": "response.created",
"response": {"id": "resp_123"},
},
{
"type": "response.output_item.added",
"item": {"type": "message", "id": "msg_123"},
},
{
"type": "response.content_part.added",
"part": {"type": "output_text", "text": ""},
},
{
"type": "response.output_text.delta",
"delta": "Hello",
},
{
"type": "response.output_text.delta",
"delta": " world",
},
{
"type": "response.completed",
"response": {
"id": "resp_123",
"output": [
{
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "Hello world"},
],
}
],
"status": "completed",
},
},
]
# This should process successfully
result = await handler.process_output_streaming_response(
responses_so_far=responses_so_far,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
)
# Should return the responses
assert result == responses_so_far
@pytest.mark.asyncio
async def test_process_output_streaming_response_writes_back_guardrailed_text(self):
"""Guardrailed text must be written back into the response.completed chunk in-place."""
class RewriteGuardrail(CustomGuardrail):
"""Replaces '<TOKEN_1>' with '[email protected]' to simulate PII unmasking."""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
texts = inputs.get("texts", [])
inputs["texts"] = [t.replace("<TOKEN_1>", "[email protected]") for t in texts]
return inputs
handler = OpenAIResponsesHandler()
guardrail = RewriteGuardrail(guardrail_name="test-rewrite")
responses_so_far = [
{"type": "response.output_text.delta", "delta": "send to "},
{"type": "response.output_text.delta", "delta": "<TOKEN_1>"},
{
"type": "response.completed",
"response": {
"id": "resp_123",
"model": "gpt-4o",
"output": [
{
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "text": "send to <TOKEN_1>"},
],
}
],
"status": "completed",
},
},
]
result = await handler.process_output_streaming_response(
responses_so_far=responses_so_far,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
)
completed_chunk = next(c for c in result if isinstance(c, dict) and c.get("type") == "response.completed")
output_text = completed_chunk["response"]["output"][0]["content"][0]["text"]
assert output_text == "send to [email protected]", (
f"Expected PII token to be unmasked in response.completed output, got: {output_text!r}"
)
@pytest.mark.asyncio
async def test_process_output_streaming_response_pass_through_unchanged(self):
"""A pass-through guardrail must not modify the output text."""
handler = OpenAIResponsesHandler()
guardrail = MockPassThroughGuardrail(guardrail_name="pass-through")
original_text = "No PII here, just normal text."
responses_so_far = [
{
"type": "response.completed",
"response": {
"id": "resp_456",
"model": "gpt-4o",
"output": [
{
"type": "message",
"id": "msg_456",
"status": "completed",
"role": "assistant",
"content": [{"type": "output_text", "text": original_text}],
}
],
"status": "completed",
},
}
]
result = await handler.process_output_streaming_response(
responses_so_far=responses_so_far,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
)
output_text = result[-1]["response"]["output"][0]["content"][0]["text"]
assert output_text == original_text
@staticmethod
def _ended_stream_events() -> List[dict]:
content = [{"type": "output_text", "text": "hello world"}]
item = {
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": content,
}
return [
{"type": "response.output_text.delta", "output_index": 0, "content_index": 0, "delta": "hello "},
{"type": "response.output_text.delta", "output_index": 0, "content_index": 0, "delta": "world"},
{"type": "response.output_text.done", "output_index": 0, "content_index": 0, "text": "hello world"},
{
"type": "response.content_part.done",
"output_index": 0,
"content_index": 0,
"part": {"type": "output_text", "text": "hello world"},
},
{
"type": "response.output_item.done",
"output_index": 0,
"item": {**item, "content": [dict(c) for c in content]},
},
{
"type": "response.completed",
"response": {
"id": "resp_123",
"model": "gpt-4o",
"output": [{**item, "content": [dict(c) for c in content]}],
"status": "completed",
},
},
]
@staticmethod
def _masking_guardrail() -> CustomGuardrail:
class MaskWorld(CustomGuardrail):
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
texts = inputs.get("texts", [])
return {**inputs, "texts": [t.replace("world", "[MASKED]") for t in texts]}
return MaskWorld(guardrail_name="test-mask")
@pytest.mark.asyncio
async def test_deliver_ended_stream_rewrites_syncs_all_stream_events(self):
handler = OpenAIResponsesHandler()
events = self._ended_stream_events()
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert result is events
assert events[0]["delta"] == "hello [MASKED]"
assert events[1]["delta"] == ""
assert events[2]["text"] == "hello [MASKED]"
assert events[3]["part"]["text"] == "hello [MASKED]"
assert events[4]["item"]["content"][0]["text"] == "hello [MASKED]"
assert events[5]["response"]["output"][0]["content"][0]["text"] == "hello [MASKED]"
@staticmethod
def _ended_function_call_stream_events() -> List[dict]:
def item(arguments: str, status: str) -> dict:
return {
"type": "function_call",
"id": "fc_123",
"call_id": "call_123",
"name": "lookup_fruit",
"arguments": arguments,
"status": status,
}
return [
{"type": "response.output_item.added", "output_index": 0, "item": item("", "in_progress")},
{
"type": "response.function_call_arguments.delta",
"item_id": "fc_123",
"output_index": 0,
"delta": '{"fruit":',
},
{
"type": "response.function_call_arguments.delta",
"item_id": "fc_123",
"output_index": 0,
"delta": ' "persimmon"}',
},
{
"type": "response.function_call_arguments.done",
"item_id": "fc_123",
"output_index": 0,
"arguments": '{"fruit": "persimmon"}',
},
{
"type": "response.output_item.done",
"output_index": 0,
"item": item('{"fruit": "persimmon"}', "completed"),
},
{
"type": "response.completed",
"response": {
"id": "resp_123",
"created_at": 1,
"model": "gpt-4o",
"output": [item('{"fruit": "persimmon"}', "completed")],
"status": "completed",
},
},
]
@staticmethod
def _argument_masking_guardrail() -> CustomGuardrail:
class MaskArguments(CustomGuardrail):
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
tool_calls = [
{**tool_call, "function": {**tool_call["function"], "arguments": '{"fruit": "[MASKED]"}'}}
for tool_call in inputs.get("tool_calls", [])
]
return {**inputs, "tool_calls": tool_calls}
return MaskArguments(guardrail_name="test-mask-arguments")
@pytest.mark.asyncio
async def test_deliver_ended_stream_rewrites_syncs_function_call_events(self):
handler = OpenAIResponsesHandler()
events = self._ended_function_call_stream_events()
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._argument_masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert result is events
assert events[0]["item"]["arguments"] == ""
assert events[1]["delta"] == '{"fruit": "[MASKED]"}'
assert events[2]["delta"] == ""
assert events[3]["arguments"] == '{"fruit": "[MASKED]"}'
assert events[4]["item"]["arguments"] == '{"fruit": "[MASKED]"}'
assert events[5]["response"]["output"][0]["arguments"] == '{"fruit": "[MASKED]"}'
assert events[5]["response"]["output"][0]["name"] == "lookup_fruit"
@pytest.mark.asyncio
async def test_deliver_ended_stream_rewrites_syncs_typed_function_call_events(self):
from litellm.types.llms.openai import (
FunctionCallArgumentsDeltaEvent,
FunctionCallArgumentsDoneEvent,
OutputItemAddedEvent,
OutputItemDoneEvent,
ResponseCompletedEvent,
ResponsesAPIResponse,
)
handler = OpenAIResponsesHandler()
typed_events: List[Any] = [
model.model_validate(event)
for model, event in zip(
(
OutputItemAddedEvent,
FunctionCallArgumentsDeltaEvent,
FunctionCallArgumentsDeltaEvent,
FunctionCallArgumentsDoneEvent,
OutputItemDoneEvent,
ResponseCompletedEvent,
),
self._ended_function_call_stream_events(),
)
]
completed_event = typed_events[5]
assert isinstance(completed_event, ResponseCompletedEvent)
assert isinstance(completed_event.response, ResponsesAPIResponse)
assert isinstance(completed_event.response.output[0], ResponseFunctionToolCall)
await handler.process_output_streaming_response(
responses_so_far=typed_events,
guardrail_to_apply=self._argument_masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert typed_events[1].delta == '{"fruit": "[MASKED]"}'
assert typed_events[2].delta == ""
assert typed_events[3].arguments == '{"fruit": "[MASKED]"}'
assert typed_events[4].item.arguments == '{"fruit": "[MASKED]"}'
assert completed_event.response.output[0].arguments == '{"fruit": "[MASKED]"}'
assert completed_event.response.output[0].name == "lookup_fruit"
@staticmethod
def _ended_custom_tool_call_stream_events() -> List[dict]:
def item(input_text: str, status: str) -> dict:
return {**CUSTOM_TOOL_CALL_ITEM, "input": input_text, "status": status}
return [
{"type": "response.output_item.added", "output_index": 0, "item": item("", "in_progress")},
{"type": "response.custom_tool_call_input.delta", "item_id": "ctc_1", "output_index": 0, "delta": "echo "},
{
"type": "response.custom_tool_call_input.delta",
"item_id": "ctc_1",
"output_index": 0,
"delta": "persimmon",
},
{
"type": "response.custom_tool_call_input.done",
"item_id": "ctc_1",
"output_index": 0,
"input": "echo persimmon",
},
{"type": "response.output_item.done", "output_index": 0, "item": item("echo persimmon", "completed")},
{
"type": "response.completed",
"response": {
"id": "resp_123",
"created_at": 1,
"model": "gpt-5.6",
"output": [item("echo persimmon", "completed")],
"status": "completed",
},
},
]
@pytest.mark.asyncio
async def test_deliver_ended_stream_rewrites_syncs_custom_tool_call_events(self):
handler = OpenAIResponsesHandler()
events = self._ended_custom_tool_call_stream_events()
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=PersimmonMaskingGuardrail(guardrail_name="mask"),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert result is events
assert events[0]["item"]["input"] == ""
assert events[1]["delta"] == "echo [MASKED]"
assert events[2]["delta"] == ""
assert events[3]["input"] == "echo [MASKED]"
assert events[4]["item"]["input"] == "echo [MASKED]"
assert events[5]["response"]["output"][0]["input"] == "echo [MASKED]"
assert events[5]["response"]["output"][0]["name"] == "exec"
assert "arguments" not in events[5]["response"]["output"][0]
@pytest.mark.asyncio
async def test_deliver_ended_stream_rewrites_keep_a_nameless_custom_tool_call_nameless(self):
handler = OpenAIResponsesHandler()
events = self._ended_custom_tool_call_stream_events()
items = [events[0]["item"], events[4]["item"], events[5]["response"]["output"][0]]
for item in items:
del item["name"]
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=PersimmonMaskingGuardrail(guardrail_name="mask"),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert events[3]["input"] == "echo [MASKED]"
assert events[5]["response"]["output"][0]["input"] == "echo [MASKED]"
assert all("name" not in item for item in items)
@pytest.mark.asyncio
async def test_deliver_ended_stream_rewrites_syncs_typed_custom_tool_call_events(self):
from litellm.types.llms.openai import (
OutputItemAddedEvent,
OutputItemDoneEvent,
ResponseCompletedEvent,
)
handler = OpenAIResponsesHandler()
typed_events: List[BaseModel] = [
model.model_validate({**event, "sequence_number": sequence_number})
for sequence_number, (model, event) in enumerate(
zip(
(
OutputItemAddedEvent,
ResponseCustomToolCallInputDeltaEvent,
ResponseCustomToolCallInputDeltaEvent,
ResponseCustomToolCallInputDoneEvent,
OutputItemDoneEvent,
ResponseCompletedEvent,
),
self._ended_custom_tool_call_stream_events(),
)
)
]
completed_event = typed_events[5]
assert isinstance(completed_event.response.output[0], CustomToolCallOutputItem)
await handler.process_output_streaming_response(
responses_so_far=typed_events,
guardrail_to_apply=PersimmonMaskingGuardrail(guardrail_name="mask"),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert typed_events[1].delta == "echo [MASKED]"
assert typed_events[2].delta == ""
assert typed_events[3].input == "echo [MASKED]"
assert typed_events[4].item.input == "echo [MASKED]"
assert completed_event.response.output[0].input == "echo [MASKED]"
assert completed_event.response.output[0].name == "exec"
@pytest.mark.asyncio
async def test_deliver_ended_stream_rewrites_a_tool_search_call(self):
from litellm.types.llms.base import BaseLiteLLMOpenAIResponseObject
from litellm.types.llms.openai import (
OutputItemAddedEvent,
OutputItemDoneEvent,
ResponseCompletedEvent,
ResponsesAPIStreamEvents,
)
def search_call(arguments: str, status: Literal["in_progress", "completed"]) -> OutputToolSearchCall:
return build_tool_search_call("srvtoolu_1", arguments, status)
handler = OpenAIResponsesHandler()
added = OutputItemAddedEvent(
type=ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED,
output_index=0,
item=BaseLiteLLMOpenAIResponseObject(**search_call("", "in_progress").model_dump()),
)
done = OutputItemDoneEvent(
type=ResponsesAPIStreamEvents.OUTPUT_ITEM_DONE,
output_index=0,
sequence_number=2,
item=BaseLiteLLMOpenAIResponseObject(**search_call('{"query": "persimmon"}', "completed").model_dump()),
)
completed = ResponseCompletedEvent(
type=ResponsesAPIStreamEvents.RESPONSE_COMPLETED,
response=ResponsesAPIResponse(
id="resp_1",
created_at=1,
model="claude-sonnet-5-5",
object="response",
status="completed",
output=[search_call('{"query": "persimmon"}', "completed")],
),
)
await handler.process_output_streaming_response(
responses_so_far=[added, done, completed],
guardrail_to_apply=PersimmonMaskingGuardrail(guardrail_name="mask"),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert added.item.model_dump()["arguments"] == {}
assert done.item.model_dump()["arguments"] == {"query": "[MASKED]"}
assert isinstance(completed.response.output[0], OutputToolSearchCall)
assert completed.response.output[0].arguments == {"query": "[MASKED]"}
assert "name" not in done.item.model_dump()
@pytest.mark.asyncio
async def test_deliver_ended_stream_rewrites_a_native_tool_search_call_that_has_no_call_id(self):
def search_call(arguments: dict, status: str) -> dict:
return {
"type": "tool_search_call",
"id": "tsc_1",
"call_id": None,
"arguments": arguments,
"execution": "server",
"status": status,
}
def function_call(arguments: str, status: str) -> dict:
return {
"type": "function_call",
"id": "fc_1",
"call_id": "call_1",
"name": "lookup_fruit",
"arguments": arguments,
"status": status,
}
handler = OpenAIResponsesHandler()
events = [
{"type": "response.output_item.added", "output_index": 0, "item": search_call({}, "in_progress")},
{
"type": "response.output_item.done",
"output_index": 0,
"item": search_call({"paths": ["persimmon"]}, "completed"),
},
{"type": "response.output_item.added", "output_index": 1, "item": function_call("", "in_progress")},
{
"type": "response.function_call_arguments.delta",
"item_id": "fc_1",
"output_index": 1,
"delta": '{"fruit": "persimmon"}',
},
{
"type": "response.function_call_arguments.done",
"item_id": "fc_1",
"output_index": 1,
"arguments": '{"fruit": "persimmon"}',
},
{
"type": "response.output_item.done",
"output_index": 1,
"item": function_call('{"fruit": "persimmon"}', "completed"),
},
{
"type": "response.completed",
"response": {
"id": "resp_1",
"model": "gpt-5.6",
"output": [
search_call({"paths": ["persimmon"]}, "completed"),
function_call('{"fruit": "persimmon"}', "completed"),
],
},
},
]
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=PersimmonMaskingGuardrail(guardrail_name="mask"),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert events[1]["item"]["arguments"] == {"paths": ["[MASKED]"]}
assert events[3]["delta"] == '{"fruit": "[MASKED]"}'
assert events[4]["arguments"] == '{"fruit": "[MASKED]"}'
assert events[5]["item"]["arguments"] == '{"fruit": "[MASKED]"}'
assert events[6]["response"]["output"][0]["arguments"] == {"paths": ["[MASKED]"]}
assert events[6]["response"]["output"][1]["arguments"] == '{"fruit": "[MASKED]"}'
@pytest.mark.asyncio
async def test_deliver_ended_stream_custom_tool_call_rewrite_without_matching_events_fails_closed(self):
from litellm.proxy.policy_engine.pipeline_executor import UndeliverableStreamRewrite
handler = OpenAIResponsesHandler()
events = self._ended_custom_tool_call_stream_events()
events[5]["response"]["output"] = [{**events[5]["response"]["output"][0], "call_id": "call_999"}]
with pytest.raises(UndeliverableStreamRewrite) as undeliverable:
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=PersimmonMaskingGuardrail(guardrail_name="mask"),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert undeliverable.value.reason == "no stream event carries the rewritten call_id call_999"
@pytest.mark.asyncio
async def test_masking_guardrail_hides_a_faked_custom_tool_call_input_from_every_event(self):
from litellm.responses.streaming_iterator import build_synthetic_response_events
events: Final = list(
build_synthetic_response_events(
transformed=ResponsesAPIResponse(
id="resp_faked",
created_at=0,
status="completed",
model="gpt-6-luna",
object="response",
output=[CUSTOM_TOOL_CALL_ITEM],
),
logging_obj=None,
chunk_size=5,
)
)
await OpenAIResponsesHandler().process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=PersimmonMaskingGuardrail(guardrail_name="mask"),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
sent: Final = "".join(event.model_dump_json() for event in events)
assert "persimmon" not in sent
assert "echo [MASKED]" in sent
@staticmethod
def _bridged_function_call_stream_events() -> List[dict]:
reasoning = {"type": "reasoning", "id": "rs_1", "summary": []}
text = {"type": "output_text", "text": "Looking that up", "annotations": []}
message = {"type": "message", "id": "msg_1", "role": "assistant", "status": "completed", "content": [text]}
def function_call(arguments: str, status: str) -> dict:
return {
"type": "function_call",
"id": "fc_1",
"call_id": "call_1",
"name": "lookup_fruit",
"arguments": arguments,
"status": status,
}
return [
{"type": "response.output_item.added", "output_index": 0, "item": dict(reasoning)},
{"type": "response.output_item.done", "output_index": 0, "item": dict(reasoning)},
{
"type": "response.output_item.added",
"output_index": 0,
"item": {**message, "status": "in_progress", "content": []},
},
{
"type": "response.output_text.delta",
"item_id": "msg_1",
"output_index": 0,
"content_index": 0,
"delta": "Looking that up",
},
{"type": "response.output_item.done", "output_index": 0, "item": {**message, "content": [dict(text)]}},
{"type": "response.output_item.added", "output_index": 1, "item": function_call("", "in_progress")},
{
"type": "response.function_call_arguments.delta",
"item_id": "fc_1",
"output_index": 1,
"delta": '{"fruit":',
},
{
"type": "response.function_call_arguments.delta",
"item_id": "fc_1",
"output_index": 1,
"delta": ' "persimmon"}',
},
{
"type": "response.function_call_arguments.done",
"item_id": "fc_1",
"output_index": 1,
"arguments": '{"fruit": "persimmon"}',
},
{
"type": "response.output_item.done",
"output_index": 1,
"item": function_call('{"fruit": "persimmon"}', "completed"),
},
{
"type": "response.completed",
"response": {
"id": "resp_1",
"model": "claude-haiku-4-5",
"output": [
dict(reasoning),
{**message, "content": [dict(text)]},
function_call('{"fruit": "persimmon"}', "completed"),
],
},
},
]
@pytest.mark.asyncio
async def test_deliver_ended_stream_rewrites_keys_bridged_function_call_events_by_call_id(self):
handler = OpenAIResponsesHandler()
events = self._bridged_function_call_stream_events()
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._argument_masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert events[6]["delta"] == '{"fruit": "[MASKED]"}'
assert events[7]["delta"] == ""
assert events[8]["arguments"] == '{"fruit": "[MASKED]"}'
assert events[5]["item"]["name"] == "lookup_fruit"
assert events[9]["item"]["arguments"] == '{"fruit": "[MASKED]"}'
assert events[10]["response"]["output"][2]["arguments"] == '{"fruit": "[MASKED]"}'
assert events[3]["delta"] == "Looking that up"
assert events[4]["item"]["content"][0]["text"] == "Looking that up"
assert events[10]["response"]["output"][1]["content"][0]["text"] == "Looking that up"
assert events[1]["item"] == {"type": "reasoning", "id": "rs_1", "summary": []}
@pytest.mark.asyncio
@pytest.mark.parametrize(
("mismatch", "expected_reason"),
[
("orphan_call_id", "no stream event carries the rewritten call_id call_999"),
("duplicate_call_id", "the stream's tool call items repeat a call_id"),
("missing_call_id", "1 of the stream's 1 tool call items carry no call_id"),
(
"unknown_argument_item_id",
"a tool call argument event names an item_id that no output_item event introduced",
),
],
)
async def test_deliver_ended_stream_function_call_rewrite_without_matching_events_fails_closed(
self, mismatch, expected_reason
):
from litellm.proxy.policy_engine.pipeline_executor import UndeliverableStreamRewrite
handler = OpenAIResponsesHandler()
events = self._ended_function_call_stream_events()
envelope_item = events[5]["response"]["output"][0]
if mismatch == "orphan_call_id":
events[5]["response"]["output"] = [{**envelope_item, "call_id": "call_999"}]
elif mismatch == "duplicate_call_id":
events[5]["response"]["output"] = [dict(envelope_item), dict(envelope_item)]
elif mismatch == "missing_call_id":
events[5]["response"]["output"] = [{key: value for key, value in envelope_item.items() if key != "call_id"}]
else:
events[1]["item_id"] = "fc_unknown"
with pytest.raises(UndeliverableStreamRewrite) as undeliverable:
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._argument_masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert undeliverable.value.reason == expected_reason
assert str(undeliverable.value).endswith(f"cannot be written back to the stream: {expected_reason}")
@pytest.mark.asyncio
async def test_ended_stream_function_call_rewrite_leaves_events_untouched_by_default(self):
handler = OpenAIResponsesHandler()
events = self._ended_function_call_stream_events()
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._argument_masking_guardrail(),
litellm_logging_obj=None,
)
assert events[1]["delta"] == '{"fruit":'
assert events[3]["arguments"] == '{"fruit": "persimmon"}'
assert events[5]["response"]["output"][0]["arguments"] == '{"fruit": "persimmon"}'
@pytest.mark.asyncio
@pytest.mark.parametrize("terminal_type", ["response.incomplete", "response.failed"])
async def test_deliver_ended_stream_rewrites_syncs_non_completed_terminals(self, terminal_type):
handler = OpenAIResponsesHandler()
events = self._ended_stream_events()
events[-1]["type"] = terminal_type
events[-1]["response"]["status"] = terminal_type.split(".")[-1]
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert result is events
assert events[0]["delta"] == "hello [MASKED]"
assert events[1]["delta"] == ""
assert events[2]["text"] == "hello [MASKED]"
assert events[3]["part"]["text"] == "hello [MASKED]"
assert events[4]["item"]["content"][0]["text"] == "hello [MASKED]"
assert events[5]["response"]["output"][0]["content"][0]["text"] == "hello [MASKED]"
@pytest.mark.asyncio
async def test_fallback_rewrite_with_delivery_expected_lands_in_the_delta_and_done_events(self):
handler = OpenAIResponsesHandler()
events = [
{"type": "response.output_text.delta", "output_index": 0, "content_index": 0, "delta": "hello "},
{"type": "response.output_text.done", "output_index": 0, "content_index": 0, "text": "hello world"},
]
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert result is events
assert events[0]["delta"] == "hello [MASKED]"
assert events[1]["text"] == "hello [MASKED]"
@pytest.mark.asyncio
async def test_fallback_delta_only_rewrite_with_delivery_expected_spreads_over_the_deltas(self):
handler = OpenAIResponsesHandler()
events = [
{"type": "response.output_text.delta", "output_index": 0, "content_index": 0, "delta": "hello "},
{"type": "response.output_text.delta", "output_index": 0, "content_index": 0, "delta": "world"},
]
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert result is events
assert [event["delta"] for event in events] == ["hello [MASKED]", ""]
@pytest.mark.asyncio
async def test_fallback_rewrite_across_parts_lands_whole_on_the_first_part(self):
handler = OpenAIResponsesHandler()
events = [
{"type": "response.output_text.delta", "output_index": 0, "content_index": 0, "delta": "hello "},
{"type": "response.output_text.done", "output_index": 0, "content_index": 0, "text": "hello "},
{"type": "response.output_text.delta", "output_index": 1, "content_index": 0, "delta": "wor"},
{"type": "response.output_text.delta", "output_index": 1, "content_index": 0, "delta": "ld"},
]
guardrail = MockRecordingGuardrail(guardrail_name="test")
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert [inputs.get("texts") for inputs in guardrail.seen_inputs] == [["hello world"]]
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert events[0]["delta"] == "hello [MASKED]"
assert events[1]["text"] == "hello [MASKED]"
assert [event["delta"] for event in events[2:]] == ["", ""]
@pytest.mark.asyncio
async def test_fallback_rewrite_over_an_unplaceable_scanned_event_fails_open(self):
from litellm.proxy.policy_engine.pipeline_executor import UndeliverableStreamRewrite
handler = OpenAIResponsesHandler()
events = [
{"type": "response.reasoning_summary_text.delta", "output_index": 0, "summary_index": 0, "delta": "hello "},
{"type": "response.output_text.delta", "output_index": 1, "content_index": 0, "delta": "world"},
]
with pytest.raises(UndeliverableStreamRewrite):
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert [event["delta"] for event in events] == ["hello ", "world"]
@pytest.mark.asyncio
async def test_output_item_done_last_rewrite_with_delivery_expected_syncs_every_text_event(self):
handler = OpenAIResponsesHandler()
events = self._ended_stream_events()[:-1]
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._masking_guardrail(),
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert result is events
assert events[0]["delta"] == "hello [MASKED]"
assert events[1]["delta"] == ""
assert events[2]["text"] == "hello [MASKED]"
assert events[3]["part"]["text"] == "hello [MASKED]"
assert events[4]["item"]["content"][0]["text"] == "hello [MASKED]"
@pytest.mark.asyncio
async def test_output_item_done_last_scans_text_with_delivery_expected(self):
handler = OpenAIResponsesHandler()
events = self._ended_stream_events()[:-1]
guardrail = MockRecordingGuardrail(guardrail_name="test")
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
deliver_ended_stream_rewrites=True,
)
assert result is events
assert [inputs.get("texts") for inputs in guardrail.seen_inputs] == [["hello world"]]
@pytest.mark.asyncio
async def test_output_item_done_last_without_delivery_expected_skips_text(self):
handler = OpenAIResponsesHandler()
events = self._ended_stream_events()[:-1]
guardrail = MockRecordingGuardrail(guardrail_name="test")
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
)
assert result is events
assert guardrail.seen_inputs == []
@pytest.mark.asyncio
async def test_fallback_rewrite_without_delivery_expected_does_not_raise(self):
handler = OpenAIResponsesHandler()
events = [
{"type": "response.output_text.done", "output_index": 0, "content_index": 0, "text": "hello world"},
]
result = await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._masking_guardrail(),
litellm_logging_obj=None,
)
assert result is events
@pytest.mark.asyncio
async def test_ended_stream_rewrite_leaves_delta_events_untouched_by_default(self):
handler = OpenAIResponsesHandler()
events = self._ended_stream_events()
await handler.process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._masking_guardrail(),
litellm_logging_obj=None,
)
assert events[0]["delta"] == "hello "
assert events[1]["delta"] == "world"
assert events[2]["text"] == "hello world"
assert events[5]["response"]["output"][0]["content"][0]["text"] == "hello [MASKED]"
@pytest.mark.asyncio
async def test_failed_stream_scans_delta_text(self):
"""A stream ending in response.failed has text only in delta events; the
fallback scan must assemble and scan it instead of skipping on an empty string."""
handler = OpenAIResponsesHandler()
guardrail = MockRecordingGuardrail(guardrail_name="test")
responses_so_far = [
{"type": "response.created", "response": {"id": "resp_123"}},
{"type": "response.output_item.added", "item": {"type": "message", "id": "msg_123"}},
{
"type": "response.output_text.delta",
"item_id": "msg_123",
"output_index": 0,
"content_index": 0,
"delta": "Hello",
},
{
"type": "response.output_text.delta",
"item_id": "msg_123",
"output_index": 0,
"content_index": 0,
"delta": " world",
},
{"type": "response.failed", "response": {"id": "resp_123", "status": "failed"}},
]
result = await handler.process_output_streaming_response(
responses_so_far=responses_so_far,
guardrail_to_apply=guardrail,
litellm_logging_obj=None,
)
assert result == responses_so_far
assert [inputs.get("texts") for inputs in guardrail.seen_inputs] == [["Hello world"]]
def test_get_streaming_string_so_far_prefers_done_text_over_deltas(self):
"""The done event repeats the whole part, so deltas must not be double counted;
a part with no done event yet still contributes its joined deltas."""
handler = OpenAIResponsesHandler()
events = [
{
"type": "response.output_text.delta",
"item_id": "msg_1",
"output_index": 0,
"content_index": 0,
"delta": "Hello",
},
{
"type": "response.output_text.delta",
"item_id": "msg_1",
"output_index": 0,
"content_index": 0,
"delta": " world",
},
{
"type": "response.output_text.done",
"item_id": "msg_1",
"output_index": 0,
"content_index": 0,
"text": "Hello world",
},
{
"type": "response.output_text.delta",
"item_id": "msg_2",
"output_index": 1,
"content_index": 0,
"delta": "; unfinished",
},
]
assert handler.get_streaming_string_so_far(events) == "Hello world; unfinished"
class TestGetStructuredMessages:
"""Test the get_structured_messages method for Responses API handler."""
def test_should_convert_string_input_to_messages(self):
"""Test that a simple string input is converted to OpenAI messages."""
handler = OpenAIResponsesHandler()
data = {"input": "What is the capital of France?"}
result = handler.get_structured_messages(data)
assert result is not None
assert len(result) >= 1
found_user = False
for msg in result:
if isinstance(msg, dict) and msg.get("role") == "user":
found_user = True
break
assert found_user, f"Expected a user message, got: {result}"
def test_should_convert_list_input_to_messages(self):
"""Test that list input (ResponseInputParam) is converted to OpenAI messages."""
handler = OpenAIResponsesHandler()
data = {
"input": [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"},
{"role": "user", "content": "How are you?"},
]
}
result = handler.get_structured_messages(data)
assert result is not None
assert len(result) >= 3
def test_should_include_instructions_as_system_message(self):
"""Test that instructions are included as a system message."""
handler = OpenAIResponsesHandler()
data = {
"input": "Roll a d20",
"instructions": "You are a helpful dungeon master.",
}
result = handler.get_structured_messages(data)
assert result is not None
has_system = any(isinstance(msg, dict) and msg.get("role") == "system" for msg in result)
assert has_system, f"Expected system message from instructions, got: {result}"
def test_should_return_none_when_no_input(self):
"""Test that None is returned when input key is missing."""
handler = OpenAIResponsesHandler()
data = {"model": "gpt-4o"}
result = handler.get_structured_messages(data)
assert result is None
def test_should_return_none_for_none_input(self):
"""Test that None is returned when input is explicitly None."""
handler = OpenAIResponsesHandler()
data = {"input": None}
result = handler.get_structured_messages(data)
assert result is None
class ToolAppendingGuardrail(CustomGuardrail):
"""Guardrail that appends a new function tool, mimicking a guardrail that
injects a retrieval/recovery tool the model can later call."""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
tools = list(inputs.get("tools") or [])
tools.append(
{
"type": "function",
"function": {
"name": "injected_tool",
"description": "injected by guardrail",
"parameters": {"type": "object", "properties": {}},
},
}
)
inputs["tools"] = tools
return inputs
class TestOpenAIResponsesHandlerToolInjection:
"""A tool a guardrail injects must survive the write-back to Responses format."""
def test_merge_keeps_guardrail_appended_tool(self):
"""merge_guardrailed_tools must not drop the extra appended tool."""
original = [{"type": "function", "name": "a"}]
groups = [form.chat_tools for form in LiteLLMCompletionResponsesConfig.responses_tools_to_chat_forms(original)]
guardrailed = [
*groups[0],
{"type": "function", "function": {"name": "b", "description": "", "parameters": {"type": "object"}}},
]
merged = merge_guardrailed_tools(original, groups, guardrailed)
assert [t["name"] for t in merged] == ["a", "b"]
@pytest.mark.asyncio
async def test_injected_tool_survives_when_request_already_has_tools(self):
"""Regression: the merge dropped the injected tool whenever the request
already carried tools, so the model never saw it."""
handler = OpenAIResponsesHandler()
guardrail = ToolAppendingGuardrail(guardrail_name="test")
data = {
"input": [{"role": "user", "content": "hi", "type": "message"}],
"tools": [
{
"type": "function",
"name": "get_weather",
"parameters": {"type": "object", "properties": {}},
}
],
"model": "gpt-4",
}
result = await handler.process_input_messages(data, guardrail)
names = [t.get("name") for t in result["tools"]]
assert "get_weather" in names
assert "injected_tool" in names
COMPRESSED_MARKER = "[compressed document; retrieve the full text with hash=b573993006976af767214fac]"
class StructuredRewriteGuardrail(CustomGuardrail):
"""Guardrail that rewrites whole messages via structured_messages and leaves
texts untouched, the way message-compressing guardrails do."""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
messages = list(inputs.get("structured_messages") or [])
first_user = next(i for i, m in enumerate(messages) if m.get("role") == "user")
rewritten = [{**m, "content": COMPRESSED_MARKER} if i == first_user else m for i, m in enumerate(messages)]
return {**inputs, "structured_messages": rewritten}
class ScopedRowsFullCoverageGuardrail(StructuredRewriteGuardrail):
"""Claims its structured_messages span the whole request but, like CrowdStrike AIDR on a
Responses body (no `messages` to rebuild from), only ever returns the scoped rows it was given."""
def structured_messages_cover_full_request(self) -> bool:
return True
class RebuildingFullCoverageGuardrail(CustomGuardrail):
"""Claims full coverage and honours it: rebuilds every conversation row from the raw request,
compressing the first user turn, the way CrowdStrike AIDR does on a chat body."""
def structured_messages_cover_full_request(self) -> bool:
return True
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict[str, object],
input_type: Literal["request", "response"],
logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
raw_input = request_data["input"]
assert isinstance(raw_input, list)
full: list[dict[str, object]] = [{"role": "system", "content": request_data["instructions"]}, *raw_input]
first_user = next(i for i, m in enumerate(full) if m.get("role") == "user")
rewritten = [{**m, "content": COMPRESSED_MARKER} if i == first_user else m for i, m in enumerate(full)]
return {**inputs, "structured_messages": rewritten}
class ToolOutputRewriteGuardrail(CustomGuardrail):
"""Guardrail that compresses the first tool-result row, the way Headroom does."""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
messages = list(inputs.get("structured_messages") or [])
first_tool = next(i for i, m in enumerate(messages) if isinstance(m, dict) and m.get("role") == "tool")
rewritten = [{**m, "content": COMPRESSED_MARKER} if i == first_tool else m for i, m in enumerate(messages)]
return {**inputs, "structured_messages": rewritten}
class DroppingRewriteGuardrail(CustomGuardrail):
"""Guardrail that rewrites the first user row and drops the last row, so the
rewrite can only land through the full-conversion fallback."""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
messages = list(inputs.get("structured_messages") or [])
first_user = next(i for i, m in enumerate(messages) if isinstance(m, dict) and m.get("role") == "user")
rewritten = [{**m, "content": COMPRESSED_MARKER} if i == first_user else m for i, m in enumerate(messages)]
return {**inputs, "structured_messages": rewritten[:-1]}
def _texts(item: dict) -> list[str]:
content = item.get("content")
if isinstance(content, str):
return [content]
return [part["text"] for part in content]
class TestStructuredMessagesWriteBack:
"""A guardrail's structured_messages rewrite must land in the Responses request,
not only the per-text mapping the chat handler shares with it."""
@pytest.mark.asyncio
async def test_list_input_gets_rewritten_messages_and_keeps_instructions(self):
handler = OpenAIResponsesHandler()
data = {
"model": "gpt-5.6",
"instructions": "Answer from the memo only.",
"input": [
{"role": "user", "content": "memo " * 400},
{"role": "assistant", "content": "Understood."},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, StructuredRewriteGuardrail())
assert result["instructions"] == "Answer from the memo only."
user_items = [item for item in result["input"] if item.get("role") == "user"]
assert [_texts(item) for item in user_items] == [[COMPRESSED_MARKER], ["What is the codename?"]]
assert not any(item.get("role") == "system" for item in result["input"])
assert _texts(next(item for item in result["input"] if item.get("role") == "assistant")) == ["Understood."]
@pytest.mark.asyncio
async def test_string_input_becomes_rewritten_message_list(self):
handler = OpenAIResponsesHandler()
data = {"model": "gpt-5.6", "input": "memo " * 400}
result = await handler.process_input_messages(data, StructuredRewriteGuardrail())
assert [_texts(item) for item in result["input"]] == [[COMPRESSED_MARKER]]
assert "instructions" not in result
@pytest.mark.asyncio
async def test_rows_the_guardrail_left_alone_keep_their_cache_control_past_the_provider_cap(self):
handler = OpenAIResponsesHandler()
ephemeral = {"type": "ephemeral"}
marked_turns = [{"role": "user", "content": f"turn {index}", "cache_control": ephemeral} for index in range(4)]
data = {
"model": "bedrock/us.anthropic.claude-sonnet-4-6",
"input": [{"role": "user", "content": "memo " * 400, "cache_control": ephemeral}, *marked_turns],
}
result = await handler.process_input_messages(data, StructuredRewriteGuardrail())
assert _texts(result["input"][0]) == [COMPRESSED_MARKER]
assert result["input"][1:] == marked_turns
@pytest.mark.asyncio
async def test_developer_item_preserved_verbatim_by_row_patch(self):
handler = OpenAIResponsesHandler()
developer_item = {"role": "developer", "content": "Always answer in French."}
data = {
"model": "gpt-5.6",
"input": [
developer_item,
{"role": "user", "content": "memo " * 400},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, StructuredRewriteGuardrail())
assert result["input"][0] is developer_item
assert developer_item["content"] == "Always answer in French."
assert _texts(result["input"][1]) == [COMPRESSED_MARKER]
assert _texts(result["input"][2]) == ["What is the codename?"]
@pytest.mark.asyncio
async def test_reasoning_and_function_call_items_survive_tool_output_compression(self):
handler = OpenAIResponsesHandler()
reasoning_item = {
"id": "rs_123",
"type": "reasoning",
"summary": [],
"encrypted_content": "gAAAAA-signed-reasoning",
}
function_call_item = {
"id": "fc_123",
"type": "function_call",
"call_id": "call_abc",
"name": "read_document",
"arguments": '{"path": "memo.txt"}',
"status": "completed",
}
data = {
"model": "gpt-5.6",
"instructions": "Answer from the memo only.",
"input": [
reasoning_item,
function_call_item,
{"type": "function_call_output", "call_id": "call_abc", "output": "memo " * 400},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, ToolOutputRewriteGuardrail())
assert result["instructions"] == "Answer from the memo only."
assert result["input"][0] is reasoning_item
assert reasoning_item["encrypted_content"] == "gAAAAA-signed-reasoning"
assert result["input"][1] is function_call_item
assert function_call_item["id"] == "fc_123"
assert result["input"][2] == {
"type": "function_call_output",
"call_id": "call_abc",
"output": COMPRESSED_MARKER,
}
assert result["input"][3] == {"role": "user", "content": "What is the codename?"}
@pytest.mark.asyncio
async def test_codex_custom_tool_items_survive_tool_output_compression(self):
handler = OpenAIResponsesHandler()
additional_tools_item = {
"type": "additional_tools",
"tools": [{"type": "custom", "name": "exec", "description": "Run a JavaScript snippet"}],
}
reasoning_item = {
"id": "rs_456",
"type": "reasoning",
"summary": [],
"encrypted_content": "gAAAAA-signed-reasoning",
}
custom_tool_call_item = {
"id": "ctc_456",
"type": "custom_tool_call",
"call_id": "call_exec",
"name": "exec",
"input": 'const r = await tools.exec_command({"cmd": "cat memo.txt"});\ntext(r.output);',
"status": "completed",
}
data = {
"model": "gpt-5.6",
"input": [
additional_tools_item,
{"role": "user", "content": "What is the codename?"},
reasoning_item,
custom_tool_call_item,
{
"type": "custom_tool_call_output",
"call_id": "call_exec",
"output": [
{"type": "input_text", "text": "Script completed\nOutput:\n"},
{"type": "input_text", "text": "memo " * 400},
],
},
],
}
result = await handler.process_input_messages(data, ToolOutputRewriteGuardrail())
assert result["input"][0] is additional_tools_item
assert result["input"][1] == {"role": "user", "content": "What is the codename?"}
assert result["input"][2] is reasoning_item
assert result["input"][3] is custom_tool_call_item
assert result["input"][4]["type"] == "custom_tool_call_output"
assert result["input"][4]["call_id"] == "call_exec"
assert COMPRESSED_MARKER in str(result["input"][4]["output"])
assert len(result["input"]) == 5
@pytest.mark.asyncio
async def test_web_search_call_item_preserved_verbatim(self):
handler = OpenAIResponsesHandler()
web_search_item = {
"id": "ws_123",
"type": "web_search_call",
"status": "completed",
"action": {"type": "search", "query": "codename memo"},
}
data = {
"model": "gpt-5.6",
"input": [
web_search_item,
{"role": "user", "content": "memo " * 400},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, StructuredRewriteGuardrail())
assert result["input"][0] is web_search_item
assert _texts(result["input"][1]) == [COMPRESSED_MARKER]
assert _texts(result["input"][2]) == ["What is the codename?"]
@pytest.mark.asyncio
async def test_row_count_change_falls_back_to_full_conversion(self):
handler = OpenAIResponsesHandler()
data = {
"model": "gpt-5.6",
"input": [
{"role": "developer", "content": "Always answer in French."},
{"role": "user", "content": "memo " * 400},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, DroppingRewriteGuardrail())
assert len(result["input"]) == 2
developer = next(item for item in result["input"] if item.get("role") == "developer")
assert developer["content"] == [{"type": "input_text", "text": "Always answer in French."}]
assert _texts(next(item for item in result["input"] if item.get("role") == "user")) == [COMPRESSED_MARKER]
@pytest.mark.asyncio
async def test_same_inputs_object_back_keeps_the_text_mapping(self):
handler = OpenAIResponsesHandler()
original_input = [
{"role": "user", "content": "Hello"},
{"role": "user", "content": [{"type": "input_text", "text": "Again"}]},
]
data = {"model": "gpt-5.6", "input": original_input}
result = await handler.process_input_messages(data, MockGuardrail())
assert result["input"] is original_input
assert [_texts(item) for item in result["input"]] == [["Hello [GUARDRAILED]"], ["Again [GUARDRAILED]"]]
class AllToolOutputsRewriteGuardrail(CustomGuardrail):
"""Guardrail that compresses every tool-result row."""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
messages = list(inputs.get("structured_messages") or [])
rewritten = [
{**m, "content": COMPRESSED_MARKER} if isinstance(m, dict) and m.get("role") == "tool" else m
for m in messages
]
return {**inputs, "structured_messages": rewritten}
class AssistantRewriteGuardrail(CustomGuardrail):
"""Guardrail that rewrites the first assistant row's content."""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
messages = list(inputs.get("structured_messages") or [])
first = next(i for i, m in enumerate(messages) if isinstance(m, dict) and m.get("role") == "assistant")
rewritten = [{**m, "content": COMPRESSED_MARKER} if i == first else m for i, m in enumerate(messages)]
return {**inputs, "structured_messages": rewritten}
class DictStructuredMessagesGuardrail(CustomGuardrail):
"""Guardrail that hands back a raw evaluation dict instead of a message list,
the way HiddenLayer v2 does."""
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
return {**inputs, "structured_messages": {"evaluation": "allowed", "messages": []}}
class NameMaskingStructuredGuardrail(CustomGuardrail):
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
rewritten: Final = [
{**m, "content": m["content"].replace("Biscuit", "[NAME]")} if isinstance(m.get("content"), str) else m
for m in inputs.get("structured_messages") or []
]
return {**inputs, "structured_messages": rewritten}
def _parallel_tool_call_input() -> list:
return [
{"id": "fc_1", "type": "function_call", "call_id": "call_1", "name": "read_a", "arguments": "{}"},
{"id": "fc_2", "type": "function_call", "call_id": "call_2", "name": "read_b", "arguments": "{}"},
{"type": "function_call_output", "call_id": "call_1", "output": "memo " * 400},
{"type": "function_call_output", "call_id": "call_2", "output": "note " * 400},
{"role": "user", "content": "What is the codename?"},
]
SSN = "123-45-6789"
REDACTED_SSN = "<US_SSN>"
def _redacted(value: object) -> object:
if isinstance(value, str):
return value.replace(SSN, REDACTED_SSN)
if isinstance(value, list):
return [{**part, "text": _redacted(part["text"])} if "text" in part else part for part in value]
return value
def _per_message_guardrail_server(structured_messages_in_answer: bool) -> Callable[..., MagicMock]:
"""Answers one redacted text per chat row it was shown, the way a guardrail
that scans per message does, and optionally the rewritten rows themselves."""
def post(url: str, json: dict, headers: dict, timeout=None) -> MagicMock:
rows = json["structured_messages"]
answer: dict = {
"action": "GUARDRAIL_INTERVENED",
"texts": [_redacted(row["content"]) if isinstance(row.get("content"), str) else "" for row in rows],
}
if structured_messages_in_answer:
answer["structured_messages"] = [{**row, "content": _redacted(row.get("content"))} for row in rows]
response = MagicMock()
response.json.return_value = answer
response.raise_for_status = MagicMock()
return response
return post
def _per_message_redactor() -> GenericGuardrailAPI:
return GenericGuardrailAPI(
api_base="https://guardrail.test",
guardrail_name="per-message-redactor",
event_hook="pre_call",
default_on=True,
)
def _tool_replay_request() -> dict:
return {
"model": "gpt-5.6",
"instructions": "Never repeat the SSN " + SSN + " back.",
"input": [
{"role": "user", "content": "Look up " + SSN + " for me."},
{"type": "function_call", "call_id": "call_1", "name": "lookup_customer", "arguments": '{"id": "42"}'},
{"type": "function_call_output", "call_id": "call_1", "output": '{"ssn": "' + SSN + '"}'},
],
}
def _string_input_request() -> dict:
return {
"model": "gpt-5.6",
"instructions": "Never repeat the SSN " + SSN + " back.",
"input": "My SSN is " + SSN + ".",
}
class TestPerMessageRewriteWriteBack:
"""A guardrail that rewrites per chat row hands the rows back as
structured_messages, and the handler lands them on the instructions and the
input items they came from; the same rewrite handed back as texts alone lands
only where every row has a scanned text (instructions plus a string input) and
is otherwise rejected by name instead of sent unrewritten."""
@pytest.mark.asyncio
@pytest.mark.parametrize(
"cache_control", [{"type": "ephemeral", "ttl": "30m"}, {"type": "persistent"}], ids=["ttl-30m", "unknown-type"]
)
async def test_an_item_mark_no_chat_row_can_hold_still_lets_the_guardrail_run(self, cache_control: dict):
guardrail = _per_message_redactor()
marked_item = {"role": "user", "content": "Look up " + SSN + " for me.", "cache_control": cache_control}
data = {"model": "gemini/gemini-2.5-flash", "input": [marked_item]}
with patch.object(guardrail.async_handler, "post", side_effect=_per_message_guardrail_server(True)):
result = await OpenAIResponsesHandler().process_input_messages(data, guardrail)
assert result["input"] == [{**marked_item, "content": "Look up " + REDACTED_SSN + " for me."}]
@pytest.mark.asyncio
async def test_structured_rows_land_on_instructions_and_tool_output(self):
guardrail = _per_message_redactor()
data = _tool_replay_request()
function_call_item = data["input"][1]
with patch.object(guardrail.async_handler, "post", side_effect=_per_message_guardrail_server(True)):
result = await OpenAIResponsesHandler().process_input_messages(data, guardrail)
assert result["instructions"] == "Never repeat the SSN " + REDACTED_SSN + " back."
assert _texts(result["input"][0]) == ["Look up " + REDACTED_SSN + " for me."]
assert result["input"][1] == function_call_item
assert result["input"][2] == {
"type": "function_call_output",
"call_id": "call_1",
"output": '{"ssn": "' + REDACTED_SSN + '"}',
}
@pytest.mark.asyncio
async def test_texts_only_per_message_answer_is_rejected_by_name(self):
from litellm.llms.base_llm.guardrail_translation.utils import UnappliableRequestRewrite
guardrail = _per_message_redactor()
data = _tool_replay_request()
original = copy.deepcopy(data)
with patch.object(guardrail.async_handler, "post", side_effect=_per_message_guardrail_server(False)):
with pytest.raises(UnappliableRequestRewrite) as excinfo:
await OpenAIResponsesHandler().process_input_messages(data, guardrail)
assert excinfo.value.guardrail_name == "per-message-redactor"
assert data["input"] == original["input"]
assert data["instructions"] == original["instructions"]
@pytest.mark.asyncio
async def test_structured_rows_land_on_instructions_and_string_input(self):
guardrail = _per_message_redactor()
data = _string_input_request()
with patch.object(guardrail.async_handler, "post", side_effect=_per_message_guardrail_server(True)):
result = await OpenAIResponsesHandler().process_input_messages(data, guardrail)
assert result["instructions"] == "Never repeat the SSN " + REDACTED_SSN + " back."
assert [_texts(item) for item in result["input"]] == [["My SSN is " + REDACTED_SSN + "."]]
@pytest.mark.asyncio
async def test_texts_only_per_message_answer_over_a_string_input_lands_on_instructions_and_input(self) -> None:
guardrail = _per_message_redactor()
data = _string_input_request()
with patch.object(guardrail.async_handler, "post", side_effect=_per_message_guardrail_server(False)):
result = await OpenAIResponsesHandler().process_input_messages(data, guardrail)
assert result["instructions"] == "Never repeat the SSN " + REDACTED_SSN + " back."
assert result["input"] == "My SSN is " + REDACTED_SSN + "."
class TestProvenancePatching:
"""The O(n) provenance pass must keep patching rewritten rows in place for the
shapes real agent loops produce, and fall back safely everywhere else."""
@pytest.mark.asyncio
async def test_parallel_tool_call_outputs_both_patched(self):
handler = OpenAIResponsesHandler()
raw_input = _parallel_tool_call_input()
fc_1, fc_2 = raw_input[0], raw_input[1]
data = {"model": "gpt-5.6", "input": raw_input}
result = await handler.process_input_messages(data, AllToolOutputsRewriteGuardrail())
assert result["input"][0] is fc_1
assert result["input"][1] is fc_2
assert result["input"][2] == {"type": "function_call_output", "call_id": "call_1", "output": COMPRESSED_MARKER}
assert result["input"][3] == {"type": "function_call_output", "call_id": "call_2", "output": COMPRESSED_MARKER}
assert result["input"][4] == {"role": "user", "content": "What is the codename?"}
@pytest.mark.asyncio
async def test_assistant_turn_with_tool_call_keeps_items_verbatim(self):
handler = OpenAIResponsesHandler()
assistant_item = {"role": "assistant", "content": "Let me read the memo."}
function_call_item = {
"id": "fc_9",
"type": "function_call",
"call_id": "call_9",
"name": "read_document",
"arguments": '{"path": "memo.txt"}',
}
data = {
"model": "gpt-5.6",
"input": [
assistant_item,
function_call_item,
{"type": "function_call_output", "call_id": "call_9", "output": "memo " * 400},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, ToolOutputRewriteGuardrail())
assert result["input"][0] is assistant_item
assert result["input"][1] is function_call_item
assert result["input"][2] == {"type": "function_call_output", "call_id": "call_9", "output": COMPRESSED_MARKER}
@pytest.mark.asyncio
async def test_rewrite_of_merged_tool_call_message_falls_back(self):
handler = OpenAIResponsesHandler()
raw_input = _parallel_tool_call_input()
data = {"model": "gpt-5.6", "input": raw_input}
result = await handler.process_input_messages(data, AssistantRewriteGuardrail())
assert not any(item is original for item in result["input"] for original in raw_input)
assistant_items = [item for item in result["input"] if item.get("role") == "assistant"]
assert [_texts(item) for item in assistant_items] == [[COMPRESSED_MARKER]]
@pytest.mark.asyncio
async def test_rewrite_of_lone_function_call_message_falls_back(self):
handler = OpenAIResponsesHandler()
data = {
"model": "gpt-5.6",
"input": [
{"id": "fc_1", "type": "function_call", "call_id": "call_1", "name": "read_a", "arguments": "{}"},
{"type": "function_call_output", "call_id": "call_1", "output": "memo memo"},
{"role": "user", "content": "What is the codename?"},
],
}
raw_input = data["input"]
result = await handler.process_input_messages(data, AssistantRewriteGuardrail())
assert not any(item is original for item in result["input"] for original in raw_input)
assistant_items = [item for item in result["input"] if item.get("role") == "assistant"]
assert [_texts(item) for item in assistant_items] == [[COMPRESSED_MARKER]]
@pytest.mark.asyncio
async def test_masked_compaction_summary_stays_a_compaction_item_that_drops_older_history(self) -> None:
data: Final = {
"model": "claude-sonnet-5-5",
"input": [
{"role": "user", "content": "My cat is Biscuit. " * 200},
{"role": "assistant", "content": "Noted."},
{
"type": "compaction",
"id": "cmp_1",
"encrypted_content": json.dumps({"type": "compaction", "content": "Cat is Biscuit."}),
},
{"role": "user", "content": "Cat name?"},
],
}
result: Final = await OpenAIResponsesHandler().process_input_messages(data, NameMaskingStructuredGuardrail())
assert compaction_summary(result["input"][2]) == "Cat is [NAME]."
provider_messages: Final = LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages(
input=result["input"], responses_api_request={}, only_since_compaction=True
)
assert [message["content"] for message in provider_messages] == ["Cat is [NAME].", "Cat name?"]
def test_provenance_bails_on_non_mapping_item(self):
from litellm.llms.openai.responses.guardrail_translation.handler import _input_item_provenance
assert _input_item_provenance(["not a mapping"], []) is None
def test_provenance_bails_when_expected_messages_disagree(self):
from litellm.llms.openai.responses.guardrail_translation.handler import _input_item_provenance
assert _input_item_provenance([{"role": "user", "content": "hi"}], [{"role": "user", "content": "bye"}]) is None
def test_provenance_bails_on_unpredicted_merge(self):
from litellm.llms.openai.responses.guardrail_translation.handler import _input_item_provenance
from litellm.responses.litellm_completion_transformation.transformation import (
LiteLLMCompletionResponsesConfig,
)
raw_input = [
{"id": "fc_1", "type": "function_call", "call_id": "call_1", "name": "read_a", "arguments": "{}"},
{"role": "assistant", "content": "Reading the memo now."},
]
expected = LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages(
input=raw_input, responses_api_request={}
)
assert len(expected) == 1
assert _input_item_provenance(raw_input, expected) is None
def test_provenance_maps_and_taints_parallel_tool_calls(self):
from litellm.llms.openai.responses.guardrail_translation.handler import _input_item_provenance
from litellm.responses.litellm_completion_transformation.transformation import (
LiteLLMCompletionResponsesConfig,
)
raw_input = _parallel_tool_call_input()
expected = LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages(
input=raw_input, responses_api_request={}
)
provenance = _input_item_provenance(raw_input, expected)
assert provenance is not None
item_for_message, tainted = provenance
assert tainted == {0}
assert dict(item_for_message) == {1: 2, 2: 3, 3: 4}
class TestDictStructuredMessagesGuard:
"""A guardrail handing back a non-list structured_messages payload must not
blow up the request; the write-back is skipped instead."""
@pytest.mark.asyncio
async def test_list_input_survives_dict_structured_messages(self):
handler = OpenAIResponsesHandler()
original_input = [{"role": "user", "content": "Hello"}]
data = {"model": "gpt-5.6", "input": original_input}
result = await handler.process_input_messages(data, DictStructuredMessagesGuardrail())
assert result["input"] is original_input
assert result["input"] == [{"role": "user", "content": "Hello"}]
@pytest.mark.asyncio
async def test_string_input_survives_dict_structured_messages(self):
handler = OpenAIResponsesHandler()
data = {"model": "gpt-5.6", "input": "Hello there"}
result = await handler.process_input_messages(data, DictStructuredMessagesGuardrail())
assert result["input"] == "Hello there"
class SystemRewriteGuardrail(CustomGuardrail):
"""Guardrail that rewrites the system row, the way prompt-hardening guardrails do."""
def __init__(self, rewritten_content: Any = COMPRESSED_MARKER):
super().__init__()
self.rewritten_content = rewritten_content
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
) -> GenericGuardrailAPIInputs:
messages = list(inputs.get("structured_messages") or [])
first = next(i for i, m in enumerate(messages) if isinstance(m, dict) and m.get("role") == "system")
rewritten = [{**m, "content": self.rewritten_content} if i == first else m for i, m in enumerate(messages)]
return {**inputs, "structured_messages": rewritten}
class TestPatchEdgeBranches:
@pytest.mark.asyncio
async def test_multimodal_user_item_rewritten_through_conversion(self):
handler = OpenAIResponsesHandler()
data = {
"model": "gpt-5.6",
"input": [
{"role": "user", "content": [{"type": "input_text", "text": "memo " * 400}]},
{"role": "user", "content": "What is the codename?"},
],
}
result = await handler.process_input_messages(data, StructuredRewriteGuardrail())
assert _texts(result["input"][0]) == [COMPRESSED_MARKER]
assert result["input"][1] == {"role": "user", "content": "What is the codename?"}
@pytest.mark.asyncio
async def test_instructions_rewrite_lands_in_instructions_field(self):
handler = OpenAIResponsesHandler()
user_item = {"role": "user", "content": "What is the codename?"}
data = {
"model": "gpt-5.6",
"instructions": "Answer from the memo only.",
"input": [user_item],
}
result = await handler.process_input_messages(data, SystemRewriteGuardrail())
assert result["instructions"] == COMPRESSED_MARKER
assert result["input"][0] is user_item
@pytest.mark.asyncio
async def test_non_string_instructions_rewrite_falls_back(self):
handler = OpenAIResponsesHandler()
user_item = {"role": "user", "content": "What is the codename?"}
data = {
"model": "gpt-5.6",
"instructions": "Answer from the memo only.",
"input": [user_item],
}
result = await handler.process_input_messages(
data, SystemRewriteGuardrail(rewritten_content=[{"type": "text", "text": COMPRESSED_MARKER}])
)
assert result["input"][0] is not user_item
@pytest.mark.asyncio
async def test_unpredicted_merge_falls_back_through_patch(self):
handler = OpenAIResponsesHandler()
raw_input = [
{"id": "fc_1", "type": "function_call", "call_id": "call_1", "name": "read_a", "arguments": "{}"},
{"role": "assistant", "content": "Reading the memo now."},
{"type": "function_call_output", "call_id": "call_1", "output": "memo memo"},
{"role": "user", "content": "memo " * 400},
]
data = {"model": "gpt-5.6", "input": raw_input}
result = await handler.process_input_messages(data, StructuredRewriteGuardrail())
assert not any(item is original for item in result["input"] for original in raw_input)
user_items = [item for item in result["input"] if item.get("role") == "user"]
assert _texts(user_items[0]) == [COMPRESSED_MARKER]
def test_item_rewrite_field_ignores_non_string_type(self):
from litellm.llms.openai.responses.guardrail_translation.handler import _item_rewrite_field
assert _item_rewrite_field({"type": 123, "content": "hello"}) is None
class ToolEditingGuardrail(CustomGuardrail):
"""Guardrail that rewrites the flattened chat tools it was handed through ``edit``"""
def __init__(self, edit: Callable[[list[dict]], list[dict]], **kwargs):
super().__init__(**kwargs)
self.edit = edit
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Any | None = None,
) -> GenericGuardrailAPIInputs:
inputs["tools"] = self.edit(list(inputs.get("tools") or []))
return inputs
def _codex_request(input_value):
"""A Responses API request shaped like what the Codex CLI sends when an MCP server is configured"""
return {
"model": "gpt-5.3-codex",
"input": input_value,
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "Weather lookup",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}},
"strict": False,
},
{
"type": "namespace",
"name": "mcp__confluence",
"description": "Confluence tools",
"tools": [
{
"type": "function",
"name": "confluence_get_page",
"description": "Get a page",
"parameters": {"type": "object", "properties": {"id": {"type": "string"}}, "required": ["id"]},
"strict": False,
},
{
"type": "function",
"name": "confluence_search",
"description": "Search pages",
"parameters": {"type": "object", "properties": {"q": {"type": "string"}}},
"strict": False,
},
],
},
{
"type": "custom",
"name": "apply_patch",
"description": "Apply a patch",
"format": {"type": "grammar", "syntax": "lark", "definition": 'start: "x"'},
},
{"type": "web_search"},
],
}
def _tool_named(tools, name):
return next(tool for tool in tools if tool.get("name") == name)
class TestOpenAIResponsesHandlerNamespaceTools:
"""Codex sends MCP tools as ``namespace`` tools; a guardrail must never flatten them (GH #39183)"""
@pytest.mark.asyncio
@pytest.mark.parametrize(
"input_value",
["hi", [{"role": "user", "content": "hi", "type": "message"}]],
ids=["string_input", "list_input"],
)
async def test_pass_through_guardrail_leaves_tools_untouched(self, input_value):
data = _codex_request(input_value)
expected_tools = copy.deepcopy(data["tools"])
result = await OpenAIResponsesHandler().process_input_messages(
data, MockPassThroughGuardrail(guardrail_name="test")
)
assert result["tools"] == expected_tools
@pytest.mark.asyncio
async def test_scanning_tools_with_malformed_flags_leaves_them_for_the_route_to_judge(self):
data: Final = _codex_request("hi")
data["tools"][0]["defer_loading"] = "false"
data["tools"][1]["tools"][0]["allowed_callers"] = "direct"
data["tools"][2]["defer_loading"] = 1
data["tools"].append({"type": "tool_search", "execution": ""})
expected_tools: Final = copy.deepcopy(data["tools"])
guardrail: Final = MockRecordingGuardrail(guardrail_name="test")
result: Final = await OpenAIResponsesHandler().process_input_messages(data, guardrail)
assert result["tools"] == expected_tools
assert [tool["function"]["name"] for tool in guardrail.seen_inputs[0]["tools"]] == [
"get_weather",
"mcp__confluence__confluence_get_page",
"mcp__confluence__confluence_search",
"apply_patch",
]
@pytest.mark.asyncio
async def test_a_function_only_guardrail_keeps_the_tool_search_tool_it_never_scans(self):
data: Final = _codex_request("hi")
data["tools"] = [{"type": "tool_search"}, {**data["tools"][0], "defer_loading": True}]
expected_tools: Final = copy.deepcopy(data["tools"])
scanned: Final[list[list[dict]]] = []
def keep_functions(tools):
scanned.append(copy.deepcopy(tools))
return [tool for tool in tools if tool["type"] == "function"]
result: Final = await OpenAIResponsesHandler().process_input_messages(
data, ToolEditingGuardrail(edit=keep_functions, guardrail_name="test")
)
assert result["tools"] == expected_tools
assert [tool["type"] for tool in scanned[0]] == ["function"]
@pytest.mark.asyncio
async def test_appending_guardrail_keeps_namespace_and_adds_tool(self):
data = _codex_request("hi")
expected_tools = copy.deepcopy(data["tools"])
result = await OpenAIResponsesHandler().process_input_messages(
data, ToolAppendingGuardrail(guardrail_name="test")
)
assert result["tools"][:-1] == expected_tools
assert result["tools"][-1]["type"] == "function"
assert result["tools"][-1]["name"] == "injected_tool"
@pytest.mark.asyncio
async def test_dropping_one_member_prunes_only_that_member(self):
data = _codex_request("hi")
expected_tools = copy.deepcopy(data["tools"])
guardrail = ToolEditingGuardrail(
edit=lambda tools: [t for t in tools if t["function"]["name"] != "mcp__confluence__confluence_search"],
guardrail_name="test",
)
result = await OpenAIResponsesHandler().process_input_messages(data, guardrail)
namespace = _tool_named(result["tools"], "mcp__confluence")
assert [member["name"] for member in namespace["tools"]] == ["confluence_get_page"]
assert namespace["tools"][0] == expected_tools[1]["tools"][0]
assert [t for t in result["tools"] if t is not namespace] == [expected_tools[0], *expected_tools[2:]]
@pytest.mark.asyncio
async def test_editing_a_member_lands_on_that_member_without_the_namespace_prefix(self):
data = _codex_request("hi")
expected_tools = copy.deepcopy(data["tools"])
def redact_search(tools):
for tool in tools:
if tool["function"]["name"] == "mcp__confluence__confluence_search":
tool["function"]["description"] = "Confluence tools\n\nREDACTED"
return tools
result = await OpenAIResponsesHandler().process_input_messages(
data, ToolEditingGuardrail(edit=redact_search, guardrail_name="test")
)
namespace = _tool_named(result["tools"], "mcp__confluence")
assert namespace["tools"][0] == expected_tools[1]["tools"][0]
assert namespace["tools"][1] == {**expected_tools[1]["tools"][1], "description": "REDACTED"}
assert {k: v for k, v in namespace.items() if k != "tools"} == {
k: v for k, v in expected_tools[1].items() if k != "tools"
}
@pytest.mark.asyncio
async def test_dropping_every_member_drops_the_namespace(self):
data = _codex_request("hi")
expected_tools = copy.deepcopy(data["tools"])
guardrail = ToolEditingGuardrail(
edit=lambda tools: [t for t in tools if not t["function"]["name"].startswith("mcp__confluence__")],
guardrail_name="test",
)
result = await OpenAIResponsesHandler().process_input_messages(data, guardrail)
assert result["tools"] == [expected_tools[0], *expected_tools[2:]]
@pytest.mark.asyncio
async def test_edited_top_level_function_is_rewritten_in_place(self):
data = _codex_request("hi")
expected_tools = copy.deepcopy(data["tools"])
def rename_weather(tools):
for tool in tools:
if tool["function"]["name"] == "get_weather":
tool["function"]["description"] = "Weather lookup (guarded)"
return tools
result = await OpenAIResponsesHandler().process_input_messages(
data, ToolEditingGuardrail(edit=rename_weather, guardrail_name="test")
)
assert result["tools"][0] == {**expected_tools[0], "description": "Weather lookup (guarded)"}
assert result["tools"][1:] == expected_tools[1:]
class TestOpenAIResponsesHandlerMalformedTools:
@pytest.mark.asyncio
async def test_request_tools_that_are_not_a_list_never_reach_the_guardrail(self):
handler = OpenAIResponsesHandler()
seen: list[list[dict]] = []
def record(tools):
seen.append(tools)
return tools
guardrail = ToolEditingGuardrail(edit=record, guardrail_name="test")
data = {"input": "hi", "tools": {"type": "function", "name": "get_weather"}}
result = await handler.process_input_messages(data, guardrail)
assert seen == [[]]
assert result["input"] == "hi"
class TestBuildBlockSseChunks:
"""build_block_sse_chunks turns a streaming ModifyResponseException into 200 SSE events"""
def _exc(self, original_response=None):
from litellm.exceptions import ModifyResponseException
return ModifyResponseException(
message="Blocked by policy.",
model="gpt-5.4-mini",
request_data={},
guardrail_name="test",
original_response=original_response,
)
def _payloads(self, chunks):
import json
return [json.loads(chunk.decode().removeprefix("data: ").strip()) for chunk in chunks]
def test_standalone_block_emits_complete_synthetic_stream(self):
handler = OpenAIResponsesHandler()
payloads = self._payloads(handler.build_block_sse_chunks(self._exc(), stream_started=False))
types = [payload["type"] for payload in payloads]
assert types[0] == "response.created"
assert types[-1] == "response.completed"
completed = payloads[-1]["response"]
assert completed["id"].startswith("resp_")
assert completed["model"] == "gpt-5.4-mini"
assert completed["output"][0]["content"][0]["text"] == "Blocked by policy."
@pytest.mark.parametrize("stream_started", [False, True])
def test_block_message_reaches_an_sdk_reader_once(self, stream_started):
import httpx
import openai
created_event = {
"type": "response.created",
"response": {
"id": "resp_live",
"object": "response",
"created_at": 1,
"model": "gpt-5.4-mini",
"status": "in_progress",
"output": [],
},
}
yielded = [created_event] if stream_started else []
already_sent = b"".join(f"data: {json.dumps(event)}\n\n".encode() for event in yielded)
block_chunks = OpenAIResponsesHandler().build_block_sse_chunks(
self._exc(original_response=yielded), stream_started=stream_started, responses_so_far=yielded
)
sse_body = already_sent + b"".join(block_chunks)
client = openai.OpenAI(
api_key="sk-test",
base_url="http://guardrail.test/v1",
http_client=httpx.Client(
transport=httpx.MockTransport(
lambda request: httpx.Response(200, headers={"content-type": "text/event-stream"}, content=sse_body)
)
),
)
with client.responses.stream(model="gpt-5.4-mini", input="hi") as stream:
text_snapshots = [event.snapshot for event in stream if event.type == "response.output_text.delta"]
assert text_snapshots[-1] == "Blocked by policy."
def test_continuation_appends_item_at_next_output_index_with_real_usage(self):
handler = OpenAIResponsesHandler()
yielded = [
{"type": "response.created", "response": {"id": "resp_live", "model": "gpt-5.4-mini-2026-01-01"}},
{"type": "response.output_item.added", "output_index": 2, "item": {"id": "msg_orig"}},
]
original = yielded + [
{
"type": "response.completed",
"response": {
"id": "resp_live",
"model": "gpt-5.4-mini-2026-01-01",
"output": [],
"usage": {"input_tokens": 7, "output_tokens": 21, "total_tokens": 28},
},
}
]
payloads = self._payloads(
handler.build_block_sse_chunks(
self._exc(original_response=original), stream_started=True, responses_so_far=yielded
)
)
types = [payload["type"] for payload in payloads]
assert "response.created" not in types
assert types[0] == "response.output_item.done"
assert payloads[0]["output_index"] == 2
assert payloads[0]["item"]["id"] == "msg_orig"
assert payloads[0]["item"]["status"] == "completed"
assert types[1] == "response.output_item.added"
assert payloads[1]["output_index"] == 3
completed = payloads[-1]["response"]
assert completed["id"] == "resp_live"
assert completed["model"] == "gpt-5.4-mini-2026-01-01"
assert completed["output"][0]["content"][0]["text"] == "Blocked by policy."
assert completed["usage"] == {"input_tokens": 7, "output_tokens": 21, "total_tokens": 28}
def test_continuation_reads_usage_from_typed_completed_event(self):
from litellm.types.llms.openai import (
ResponseCompletedEvent,
ResponsesAPIResponse,
ResponsesAPIStreamEvents,
)
handler = OpenAIResponsesHandler()
original = [
ResponseCompletedEvent(
type=ResponsesAPIStreamEvents.RESPONSE_COMPLETED,
response=ResponsesAPIResponse.model_validate(
{
"id": "resp_live",
"created_at": 1,
"model": "gpt-5.4-mini",
"output": [],
"usage": {"input_tokens": 7, "output_tokens": 21, "total_tokens": 28},
}
),
)
]
payloads = self._payloads(
handler.build_block_sse_chunks(
self._exc(original_response=original), stream_started=True, responses_so_far=[]
)
)
completed = payloads[-1]["response"]
assert completed["usage"]["input_tokens"] == 7
assert completed["usage"]["output_tokens"] == 21
assert completed["usage"]["total_tokens"] == 28
def test_continuation_closes_open_item_given_pydantic_events_with_enum_types(self):
from litellm.types.llms.openai import (
BaseLiteLLMOpenAIResponseObject,
ContentPartAddedEvent,
OutputItemAddedEvent,
OutputTextDeltaEvent,
ResponsesAPIStreamEvents,
)
handler = OpenAIResponsesHandler()
open_item = GenericResponseOutputItem.model_validate(
{"type": "message", "id": "msg_live", "status": "in_progress", "role": "assistant", "content": []}
)
yielded = [
OutputItemAddedEvent(type=ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED, output_index=0, item=open_item),
ContentPartAddedEvent(
type=ResponsesAPIStreamEvents.CONTENT_PART_ADDED,
item_id="msg_live",
output_index=0,
content_index=0,
part=BaseLiteLLMOpenAIResponseObject.model_validate(
{"type": "output_text", "text": "", "annotations": []}
),
),
OutputTextDeltaEvent(
type=ResponsesAPIStreamEvents.OUTPUT_TEXT_DELTA,
item_id="msg_live",
output_index=0,
content_index=0,
delta="partial ",
),
OutputTextDeltaEvent(
type=ResponsesAPIStreamEvents.OUTPUT_TEXT_DELTA,
item_id="msg_live",
output_index=0,
content_index=0,
delta="text",
),
]
payloads = self._payloads(
handler.build_block_sse_chunks(
self._exc(original_response=yielded), stream_started=True, responses_so_far=yielded
)
)
types = [payload["type"] for payload in payloads]
assert types[:3] == [
"response.output_text.done",
"response.content_part.done",
"response.output_item.done",
]
assert payloads[0]["text"] == "partial text"
assert payloads[2]["item"]["id"] == "msg_live"
assert payloads[2]["item"]["status"] == "completed"
assert payloads[2]["item"]["content"][0]["text"] == "partial text"
assert types[3] == "response.output_item.added"
assert payloads[3]["output_index"] == 1
def test_continuation_closes_open_function_call_as_incomplete(self):
handler = OpenAIResponsesHandler()
yielded = [
{"type": "response.created", "response": {"id": "resp_live", "model": "gpt-5.4-mini"}},
{
"type": "response.output_item.added",
"output_index": 0,
"item": {
"id": "fc_live",
"type": "function_call",
"status": "in_progress",
"call_id": "call_1",
"name": "run_payment",
"arguments": "",
},
},
{
"type": "response.function_call_arguments.delta",
"item_id": "fc_live",
"output_index": 0,
"delta": '{"amount": 100}',
},
]
payloads = self._payloads(
handler.build_block_sse_chunks(
self._exc(original_response=yielded), stream_started=True, responses_so_far=yielded
)
)
types = [payload["type"] for payload in payloads]
assert types[0] == "response.output_item.done"
closed = payloads[0]["item"]
assert closed["id"] == "fc_live"
assert closed["type"] == "function_call"
assert closed["status"] == "incomplete"
assert closed["name"] == "run_payment"
assert "content" not in closed
assert types[1] == "response.output_item.added"
assert payloads[1]["output_index"] == 1
assert types[-1] == "response.completed"
def test_continuation_without_open_item_emits_no_closing_events(self):
handler = OpenAIResponsesHandler()
yielded = [
{"type": "response.created", "response": {"id": "resp_live", "model": "gpt-5.4-mini"}},
{"type": "response.in_progress", "response": {"id": "resp_live"}},
]
payloads = self._payloads(
handler.build_block_sse_chunks(
self._exc(original_response=yielded), stream_started=True, responses_so_far=yielded
)
)
types = [payload["type"] for payload in payloads]
assert types[0] == "response.output_item.added"
assert types[-1] == "response.completed"
dones = [payload for payload in payloads if payload["type"] == "response.output_item.done"]
assert len(dones) == 1
assert dones[0]["item"]["content"][0]["text"] == "Blocked by policy."
class TestOpenAIResponsesHandlerStreamingScanKey:
"""get_streaming_scan_key mirrors what process_output_streaming_response would scan"""
@staticmethod
def _delta(sequence_number, text):
return {
"type": "response.output_text.delta",
"sequence_number": sequence_number,
"item_id": "msg_1",
"output_index": 0,
"content_index": 0,
"delta": text,
}
def test_no_events_yields_no_key(self):
assert OpenAIResponsesHandler().get_streaming_scan_key([]) is None
def test_key_accumulates_deltas_while_the_stream_is_open(self):
from litellm.llms.base_llm.guardrail_translation.base_translation import StreamingScanKey
key = OpenAIResponsesHandler().get_streaming_scan_key([self._delta(0, "hel"), self._delta(1, "lo")])
assert key == StreamingScanKey(texts=("hello",))
def test_typed_delta_events_accumulate_like_dicts(self):
from litellm.types.llms.openai import OutputTextDeltaEvent
events = [
OutputTextDeltaEvent(
type="response.output_text.delta",
item_id="msg_1",
output_index=0,
content_index=0,
delta=text,
sequence_number=i,
)
for i, text in enumerate(("hel", "lo"))
]
key = OpenAIResponsesHandler().get_streaming_scan_key(events)
assert key.texts == ("hello",)
assert key.stream_ended is False
def test_events_without_text_leave_the_key_unchanged(self):
handler = OpenAIResponsesHandler()
events = [self._delta(0, "hi")]
quiet = events + [{"type": "response.in_progress", "sequence_number": 1}]
assert handler.get_streaming_scan_key(quiet) == handler.get_streaming_scan_key(events)
@staticmethod
def _completed(sequence_number, output):
return {"type": "response.completed", "sequence_number": sequence_number, "response": {"output": output}}
def test_completed_event_keys_on_the_final_output_text(self):
handler = OpenAIResponsesHandler()
message = {"type": "message", "content": [{"type": "output_text", "text": "hi"}]}
open_key = handler.get_streaming_scan_key([self._delta(0, "hi")])
ended_key = handler.get_streaming_scan_key([self._delta(0, "hi"), self._completed(1, [message])])
assert ended_key.stream_ended is True
assert ended_key == open_key
def test_completed_event_with_a_function_call_changes_the_key(self):
handler = OpenAIResponsesHandler()
message = {"type": "message", "content": [{"type": "output_text", "text": "hi"}]}
function_call = {"type": "function_call", "call_id": "call_1", "name": "get_weather", "arguments": "{}"}
open_key = handler.get_streaming_scan_key([self._delta(0, "hi")])
ended_key = handler.get_streaming_scan_key([self._delta(0, "hi"), self._completed(1, [message, function_call])])
assert ended_key.texts == ("hi",)
assert len(ended_key.tool_calls) == 1 and "get_weather" in ended_key.tool_calls[0]
assert ended_key != open_key
def test_completed_event_with_a_custom_tool_call_changes_the_key(self):
handler = OpenAIResponsesHandler()
message = {"type": "message", "content": [{"type": "output_text", "text": "hi"}]}
ended_key = handler.get_streaming_scan_key(
[self._delta(0, "hi"), self._completed(1, [message, dict(CUSTOM_TOOL_CALL_ITEM)])]
)
rewritten_key = handler.get_streaming_scan_key(
[self._delta(0, "hi"), self._completed(1, [message, {**CUSTOM_TOOL_CALL_ITEM, "input": "echo kumquat"}])]
)
assert ended_key.texts == ("hi",)
assert len(ended_key.tool_calls) == 1 and "echo persimmon" in ended_key.tool_calls[0]
assert rewritten_key != ended_key
def test_completed_event_reads_every_output_text_part(self):
from litellm.types.responses.main import CustomToolCallOutputItem, GenericResponseOutputItem, OutputText
item = GenericResponseOutputItem(
type="message",
id="msg_1",
status="completed",
role="assistant",
content=[
OutputText(type="output_text", text="one", annotations=[]),
OutputText(type="output_text", text="two", annotations=[]),
],
)
key = OpenAIResponsesHandler().get_streaming_scan_key([self._completed(0, [item])])
assert key.texts == ("one", "two")
def test_output_item_done_round_is_never_deduped(self):
done = {"type": "response.output_item.done", "sequence_number": 1, "item": {"type": "function_call"}}
assert OpenAIResponsesHandler().get_streaming_scan_key([self._delta(0, "hi"), done]) is None
@pytest.mark.parametrize("terminal_type", ["response.incomplete", "response.failed"])
def test_non_completed_terminal_envelopes_key_their_output_items(self, terminal_type):
handler = OpenAIResponsesHandler()
arguments_delta = {
"type": "response.function_call_arguments.delta",
"sequence_number": 1,
"item_id": "fc_1",
"delta": '{"city":',
}
function_call = {"type": "function_call", "call_id": "call_1", "name": "get_weather", "arguments": '{"city":'}
terminal = {
"type": terminal_type,
"sequence_number": 2,
"response": {"id": "resp_1", "output": [function_call]},
}
mid_stream_key = handler.get_streaming_scan_key([arguments_delta])
ended_key = handler.get_streaming_scan_key([arguments_delta, terminal])
assert ended_key.stream_ended is True
assert ended_key.tool_calls_in_flight is False
assert len(ended_key.tool_calls) == 1
assert ended_key != mid_stream_key
def test_streamed_tool_call_events_flag_tool_calls_in_flight_until_the_stream_ends(self):
handler = OpenAIResponsesHandler()
added = {
"type": "response.output_item.added",
"sequence_number": 1,
"item": {"type": "function_call", "id": "fc_1", "call_id": "call_1", "name": "get_weather"},
}
arguments_delta = {
"type": "response.function_call_arguments.delta",
"sequence_number": 2,
"item_id": "fc_1",
"delta": '{"city":',
}
function_call = {"type": "function_call", "call_id": "call_1", "name": "get_weather", "arguments": "{}"}
assert handler.get_streaming_scan_key([self._delta(0, "hi")]).tool_calls_in_flight is False
assert handler.get_streaming_scan_key([self._delta(0, "hi"), added]).tool_calls_in_flight is True
assert handler.get_streaming_scan_key([self._delta(0, "hi"), arguments_delta]).tool_calls_in_flight is True
ended_key = handler.get_streaming_scan_key([self._delta(0, "hi"), added, self._completed(3, [function_call])])
assert ended_key.tool_calls_in_flight is False
assert len(ended_key.tool_calls) == 1
def test_released_stream_as_ended_keys_the_tool_call_the_client_already_received(self):
handler = OpenAIResponsesHandler()
added = {
"type": "response.output_item.added",
"sequence_number": 1,
"item": {
"type": "function_call",
"id": "fc_1",
"call_id": "call_1",
"name": "get_weather",
"arguments": "",
},
}
arguments_delta = {
"type": "response.function_call_arguments.delta",
"sequence_number": 2,
"item_id": "fc_1",
"delta": '{"city": "Paris"',
}
ended_key = handler.get_streaming_scan_key(
handler.released_stream_as_ended([self._delta(0, "hi"), added, arguments_delta])
)
assert ended_key.stream_ended is True
assert ended_key.texts == ("hi",)
assert len(ended_key.tool_calls) == 1 and "Paris" in ended_key.tool_calls[0], ended_key
def test_released_stream_as_ended_leaves_a_text_only_stream_as_released(self):
released = (self._delta(0, "hi"), self._delta(1, " there"))
ended = OpenAIResponsesHandler().released_stream_as_ended(released)
assert ended == released and all(a is b for a, b in zip(ended, released, strict=True))
@staticmethod
def _finished_function_call(sequence_number: int, item_id: str, city: str) -> tuple[dict[str, object], ...]:
arguments = json.dumps({"city": city})
pending = {"type": "function_call", "id": item_id, "call_id": "call_" + item_id, "name": "get_weather"}
return (
{
"type": "response.output_item.added",
"sequence_number": sequence_number,
"item": {**pending, "arguments": ""},
},
{
"type": "response.function_call_arguments.delta",
"sequence_number": sequence_number + 1,
"item_id": item_id,
"delta": arguments,
},
{
"type": "response.output_item.done",
"sequence_number": sequence_number + 2,
"item": {**pending, "arguments": arguments, "status": "completed"},
},
)
def test_released_stream_as_ended_keys_every_tool_call_finished_before_the_disconnect(self):
handler = OpenAIResponsesHandler()
released = (
self._delta(0, "hi"),
*self._finished_function_call(1, "fc_1", "Paris"),
*self._finished_function_call(4, "fc_2", "Rome"),
)
ended_key = handler.get_streaming_scan_key(handler.released_stream_as_ended(released))
assert ended_key.stream_ended is True
assert ended_key.texts == ("hi",)
cities = tuple(
city for fingerprint in ended_key.tool_calls for city in ("Paris", "Rome") if city in fingerprint
)
assert cities == ("Paris", "Rome"), ended_key
def test_released_stream_as_ended_keys_a_message_whose_item_already_finished(self):
handler = OpenAIResponsesHandler()
message_done = {
"type": "response.output_item.done",
"sequence_number": 1,
"item": {"type": "message", "id": "msg_1", "content": [{"type": "output_text", "text": "hi"}]},
}
ended_key = handler.get_streaming_scan_key(
handler.released_stream_as_ended((self._delta(0, "hi"), message_done))
)
assert ended_key.stream_ended is True
assert ended_key.texts == ("hi",)
@pytest.mark.asyncio
async def test_scan_of_a_stream_released_through_a_finished_tool_call_covers_its_text_too(self):
handler = OpenAIResponsesHandler()
guardrail = MockRecordingGuardrail(guardrail_name="test")
released = (self._delta(0, "hi"), *self._finished_function_call(1, "fc_1", "Paris"))
await handler.process_output_streaming_response(
responses_so_far=list(handler.released_stream_as_ended(released)),
guardrail_to_apply=guardrail,
request_data={},
)
assert [inputs.get("texts") for inputs in guardrail.seen_inputs] == [["hi"]], guardrail.seen_inputs
tool_calls = guardrail.seen_inputs[0].get("tool_calls") or []
assert [call["function"]["arguments"] for call in tool_calls] == ['{"city": "Paris"}'], tool_calls
@pytest.mark.asyncio
async def test_a_tool_call_finished_mid_stream_reaches_the_guardrail_without_texts(self):
guardrail: Final = MockRecordingGuardrail(guardrail_name="test")
await OpenAIResponsesHandler().process_output_streaming_response(
responses_so_far=[self._finished_function_call(1, "fc_1", "Paris")[-1]],
guardrail_to_apply=guardrail,
request_data={},
)
assert [
(sorted(inputs), [call["function"]["arguments"] for call in inputs.get("tool_calls") or []])
for inputs in guardrail.seen_inputs
] == [(["tool_calls"], ['{"city": "Paris"}'])]
class TestCompactionOutputSummaryReachesOutputGuardrails:
SUMMARY: Final = "The user lives at 1 Secret Lane."
@classmethod
def _compaction_item(cls, summary: str | None = None) -> dict[str, str]:
return {
"type": "compaction",
"id": "cmp_1",
"encrypted_content": json.dumps({"type": "compaction", "content": summary or cls.SUMMARY}),
}
@staticmethod
def _summary(item: object) -> str:
encrypted_content: Final = item["encrypted_content"] if isinstance(item, dict) else item.encrypted_content
return json.loads(encrypted_content)["content"]
@staticmethod
def _lane_masking_guardrail() -> CustomGuardrail:
class MaskLane(CustomGuardrail):
async def apply_guardrail(
self,
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: LiteLLMLoggingObj | None = None,
) -> GenericGuardrailAPIInputs:
return {
**inputs,
"texts": [text.replace("Secret Lane", "[MASKED]") for text in inputs.get("texts", [])],
}
return MaskLane(guardrail_name="mask-lane")
@pytest.mark.asyncio
async def test_non_streaming_summary_is_masked_inside_its_item(self) -> None:
response: Final = ResponsesAPIResponse(
id="resp_1",
created_at=1,
model="claude-sonnet-5-5",
object="response",
status="completed",
output=[self._compaction_item()],
)
result: Final = await OpenAIResponsesHandler().process_output_response(response, self._lane_masking_guardrail())
assert self._summary(result.output[0]) == "The user lives at 1 [MASKED]."
@pytest.mark.asyncio
async def test_ended_stream_summary_is_masked_in_the_done_item_and_the_envelope(self) -> None:
events: Final = [
{
"type": "response.output_item.added",
"output_index": 0,
"item": {**self._compaction_item(), "encrypted_content": ""},
},
{"type": "response.output_item.done", "output_index": 0, "item": self._compaction_item()},
{
"type": "response.completed",
"response": {
"id": "resp_1",
"created_at": 1,
"model": "claude-sonnet-5-5",
"output": [self._compaction_item()],
"status": "completed",
},
},
]
await OpenAIResponsesHandler().process_output_streaming_response(
responses_so_far=events,
guardrail_to_apply=self._lane_masking_guardrail(),
deliver_ended_stream_rewrites=True,
)
assert self._summary(events[1]["item"]) == "The user lives at 1 [MASKED]."
assert self._summary(events[2]["response"]["output"][0]) == "The user lives at 1 [MASKED]."
@pytest.mark.asyncio
async def test_a_blocking_guardrail_rejects_a_finished_compaction_item_mid_stream(self) -> None:
with pytest.raises(HTTPException) as exc_info:
await OpenAIResponsesHandler().process_output_streaming_response(
responses_so_far=[
{"type": "response.output_item.done", "output_index": 0, "item": self._compaction_item()}
],
guardrail_to_apply=MockGuardrail(guardrail_name="block"),
)
assert exc_info.value.detail["texts"] == [self.SUMMARY]
def test_a_changed_summary_changes_the_ended_stream_scan_key(self) -> None:
handler: Final = OpenAIResponsesHandler()
def ended_key(summary: str) -> object:
return handler.get_streaming_scan_key(
[{"type": "response.completed", "response": {"output": [self._compaction_item(summary)]}}]
)
assert ended_key(self.SUMMARY) != ended_key("The user lives elsewhere.")