litellm/llms/openai/responses/guardrail_translation/README.md
This module provides guardrail translation support for the OpenAI Responses API format.
The OpenAIResponsesHandler class handles the translation of guardrail operations for both input and output of the Responses API. It follows the same pattern as the Chat Completions handler but is adapted for the Responses API's specific data structures.
The Responses API accepts input in two formats:
String input: Simple text string
{"input": "Hello world", "model": "gpt-4"}
List input: Array of message objects (ResponseInputParam)
{
"input": [
{
"role": "user",
"content": "Hello", # Can be string or list of content items
"type": "message"
}
],
"model": "gpt-4"
}
The Responses API returns a ResponsesAPIResponse object with:
{
"id": "resp_123",
"output": [
{
"type": "message",
"id": "msg_123",
"status": "completed",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "Assistant response",
"annotations": []
}
]
}
]
}
The handler is automatically discovered and registered for CallTypes.responses and CallTypes.aresponses.
from litellm.llms import get_guardrail_translation_mapping
from litellm.types.utils import CallTypes
# Get the handler
handler_class = get_guardrail_translation_mapping(CallTypes.responses)
handler = handler_class()
# Process input
data = {"input": "User message", "model": "gpt-4"}
processed_data = await handler.process_input_messages(data, guardrail_instance)
# Process output
response = await litellm.aresponses(**processed_data)
processed_response = await handler.process_output_response(response, guardrail_instance)
process_input_messages(data, guardrail_to_apply)Processes input data by:
process_output_response(response, guardrail_to_apply)Processes output response by:
The handler can be customized by overriding these methods:
_extract_input_text_and_create_tasks(): Customize input text extraction logic_apply_guardrail_responses_to_input(): Customize how guardrail responses are applied to input_extract_output_text_and_create_tasks(): Customize output text extraction logic_apply_guardrail_responses_to_output(): Customize how guardrail responses are applied to output_has_text_content(): Customize text content detectionComprehensive tests are available in tests/llm_translation/test_openai_responses_guardrail_handler.py:
pytest tests/llm_translation/test_openai_responses_guardrail_handler.py -v
asyncio.gather()