Repository navigation
fix(realtime): guardrails with pre_call/post_call mode now work on realtime WebSocket #22161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,6 +43,7 @@ def __init__( | |||||||||
| provider_config: Optional[BaseRealtimeConfig] = None, | ||||||||||
| model: str = "", | ||||||||||
| user_api_key_dict: Optional[Any] = None, | ||||||||||
| request_data: Optional[Dict] = None, | ||||||||||
| ): | ||||||||||
| self.websocket = websocket | ||||||||||
| self.backend_ws = backend_ws | ||||||||||
|
|
@@ -68,6 +69,10 @@ def __init__( | |||||||||
| self.current_delta_type: Optional[ALL_DELTA_TYPES] = None | ||||||||||
| self.session_configuration_request: Optional[str] = None | ||||||||||
| self.user_api_key_dict = user_api_key_dict | ||||||||||
| self.request_data: Dict = request_data or {} | ||||||||||
| # Set to True after a text-input guardrail block so we can swallow the client's | ||||||||||
| # subsequent response.create (which would conflict with the block response). | ||||||||||
| self._swallow_next_response_create: bool = False | ||||||||||
|
|
||||||||||
| def _should_store_message( | ||||||||||
| self, | ||||||||||
|
|
@@ -231,15 +236,23 @@ async def _send_to_backend(self, message: str) -> None: | |||||||||
| await self.backend_ws.send(message) | ||||||||||
|
|
||||||||||
| def _has_realtime_guardrails(self) -> bool: | ||||||||||
| """Return True if any callback is registered for realtime_input_transcription.""" | ||||||||||
| """Return True if any callback is registered for realtime guardrail event types.""" | ||||||||||
| from litellm.integrations.custom_guardrail import CustomGuardrail | ||||||||||
| from litellm.types.guardrails import GuardrailEventHooks | ||||||||||
|
|
||||||||||
| _realtime_event_types = [ | ||||||||||
| GuardrailEventHooks.realtime_input_transcription, | ||||||||||
| GuardrailEventHooks.pre_call, | ||||||||||
| GuardrailEventHooks.post_call, | ||||||||||
| ] | ||||||||||
| return any( | ||||||||||
| isinstance(cb, CustomGuardrail) | ||||||||||
| and cb.should_run_guardrail( | ||||||||||
| data={}, | ||||||||||
| event_type=GuardrailEventHooks.realtime_input_transcription, | ||||||||||
| and any( | ||||||||||
| cb.should_run_guardrail( | ||||||||||
| data=self.request_data, | ||||||||||
| event_type=et, | ||||||||||
| ) | ||||||||||
| for et in _realtime_event_types | ||||||||||
| ) | ||||||||||
| for cb in litellm.callbacks | ||||||||||
| ) | ||||||||||
|
|
@@ -258,17 +271,25 @@ async def run_realtime_guardrails( | |||||||||
| from litellm.integrations.custom_guardrail import CustomGuardrail | ||||||||||
| from litellm.types.guardrails import GuardrailEventHooks | ||||||||||
|
|
||||||||||
| _realtime_event_types = [ | ||||||||||
| GuardrailEventHooks.realtime_input_transcription, | ||||||||||
| GuardrailEventHooks.pre_call, | ||||||||||
| GuardrailEventHooks.post_call, | ||||||||||
| ] | ||||||||||
| _check_data = {**self.request_data, "transcript": transcript} | ||||||||||
| _already_run: set = set() | ||||||||||
|
|
||||||||||
| for callback in litellm.callbacks: | ||||||||||
| if not isinstance(callback, CustomGuardrail): | ||||||||||
| continue | ||||||||||
| if ( | ||||||||||
| callback.should_run_guardrail( | ||||||||||
| data={"transcript": transcript}, | ||||||||||
| event_type=GuardrailEventHooks.realtime_input_transcription, | ||||||||||
| ) | ||||||||||
| is not True | ||||||||||
| if id(callback) in _already_run: | ||||||||||
| continue | ||||||||||
| if not any( | ||||||||||
| callback.should_run_guardrail(data=_check_data, event_type=et) | ||||||||||
| for et in _realtime_event_types | ||||||||||
| ): | ||||||||||
| continue | ||||||||||
| _already_run.add(id(callback)) | ||||||||||
| try: | ||||||||||
| await callback.apply_guardrail( | ||||||||||
| inputs={"texts": [transcript], "images": []}, | ||||||||||
|
|
@@ -293,20 +314,15 @@ async def run_realtime_guardrails( | |||||||||
| safe_msg = str(detail) | ||||||||||
| else: | ||||||||||
| safe_msg = str(e) or "I'm sorry, that request was blocked by the content filter." | ||||||||||
| # Cancel any in-flight response before speaking the warning. | ||||||||||
| # This handles the race where create_response fired before we could intercept. | ||||||||||
| await self._send_to_backend(json.dumps({"type": "response.cancel"})) | ||||||||||
| # Ask the model to speak the warning — TTS audio plays naturally in the client | ||||||||||
| await self._send_to_backend( | ||||||||||
| # Return the error directly to the WebSocket consumer. | ||||||||||
| await self.websocket.send_text( | ||||||||||
| json.dumps( | ||||||||||
| { | ||||||||||
| "type": "response.create", | ||||||||||
| "response": { | ||||||||||
| "modalities": ["text", "audio"], | ||||||||||
| "instructions": ( | ||||||||||
| f"Say exactly and only: \"{safe_msg}\". " | ||||||||||
| "Do not add anything else." | ||||||||||
| ), | ||||||||||
| "type": "error", | ||||||||||
| "error": { | ||||||||||
| "type": "guardrail_violation", | ||||||||||
| "message": safe_msg, | ||||||||||
| "code": "content_policy_violation", | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| ) | ||||||||||
|
|
@@ -348,23 +364,6 @@ async def _handle_provider_config_message(self, raw_response) -> None: | |||||||||
| if isinstance(transformed_response, list) | ||||||||||
| else [transformed_response] | ||||||||||
| ) | ||||||||||
| for event in events: | ||||||||||
| ## GUARDRAIL: inject create_response=false on session.created | ||||||||||
| if isinstance(event, dict) and event.get("type") == "session.created": | ||||||||||
| if self._has_realtime_guardrails(): | ||||||||||
| await self._send_to_backend( | ||||||||||
| json.dumps( | ||||||||||
| { | ||||||||||
| "type": "session.update", | ||||||||||
| "session": { | ||||||||||
| "turn_detection": { | ||||||||||
| "type": "server_vad", | ||||||||||
| "create_response": False, | ||||||||||
| } | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| ) | ||||||||||
| ) | ||||||||||
| for event in events: | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removal of The previous code injected With this code removed, when server VAD detects speech completion, the backend will auto-generate a response ( For the new text-input ( |
||||||||||
| event_str = json.dumps(event) | ||||||||||
| ## GUARDRAIL: run on transcription events in provider_config path too | ||||||||||
|
|
@@ -397,28 +396,6 @@ async def _handle_raw_backend_message(self, raw_response) -> bool: | |||||||||
| try: | ||||||||||
| event_obj = json.loads(raw_response) | ||||||||||
|
|
||||||||||
| if event_obj.get("type") == "session.created": | ||||||||||
| # If any realtime guardrails are registered, proactively | ||||||||||
| # set create_response=false so the LLM never auto-responds | ||||||||||
| # before our guardrail has a chance to run. | ||||||||||
| if self._has_realtime_guardrails(): | ||||||||||
| await self._send_to_backend( | ||||||||||
| json.dumps( | ||||||||||
| { | ||||||||||
| "type": "session.update", | ||||||||||
| "session": { | ||||||||||
| "turn_detection": { | ||||||||||
| "type": "server_vad", | ||||||||||
| "create_response": False, | ||||||||||
| } | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| ) | ||||||||||
| ) | ||||||||||
| verbose_logger.debug( | ||||||||||
| "[realtime guardrail] injected create_response=false into session" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| if ( | ||||||||||
| event_obj.get("type") | ||||||||||
| == "conversation.item.input_audio_transcription.completed" | ||||||||||
|
|
@@ -490,6 +467,11 @@ async def client_ack_messages(self): | |||||||||
| msg_obj = json.loads(message) | ||||||||||
| msg_type = msg_obj.get("type") | ||||||||||
|
|
||||||||||
| # Swallow the client's response.create if we just blocked an item. | ||||||||||
| if msg_type == "response.create" and self._swallow_next_response_create: | ||||||||||
| self._swallow_next_response_create = False | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the client sends two Consider using a counter instead of a boolean:
Suggested change
(with corresponding change to set |
||||||||||
| continue # block response already sent by guardrail | ||||||||||
|
|
||||||||||
| if msg_type == "conversation.item.create": | ||||||||||
| # Check user text messages for prompt injection | ||||||||||
| item = msg_obj.get("item", {}) | ||||||||||
|
|
@@ -506,6 +488,7 @@ async def client_ack_messages(self): | |||||||||
| combined_text | ||||||||||
| ) | ||||||||||
| if blocked: | ||||||||||
| self._swallow_next_response_create = True | ||||||||||
| continue # don't forward to backend | ||||||||||
|
|
||||||||||
| except (json.JSONDecodeError, AttributeError): | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,6 +150,11 @@ async def _arealtime( | |
| or get_secret_str("OPENAI_API_KEY") | ||
| ) | ||
|
|
||
| # Build metadata for guardrail checking. | ||
| _litellm_metadata: dict = {**(kwargs.get("litellm_metadata") or {})} | ||
| _guardrails = (kwargs.get("metadata") or {}).get("guardrails") or kwargs.get("guardrails") or [] | ||
| if _guardrails: | ||
| _litellm_metadata["guardrails"] = _guardrails | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guardrail metadata only plumbed for OpenAI provider The If this is intentional (OpenAI-only for now), it might be worth a code comment. Otherwise, consider extracting the metadata build to a shared helper before the provider branches so all providers benefit. |
||
| await openai_realtime.async_realtime( | ||
| model=model, | ||
| websocket=websocket, | ||
|
|
@@ -160,6 +165,7 @@ async def _arealtime( | |
| timeout=timeout, | ||
| query_params=query_params, | ||
| user_api_key_dict=kwargs.get("user_api_key_dict"), | ||
| litellm_metadata=_litellm_metadata, | ||
| ) | ||
| elif _custom_llm_provider == "bedrock": | ||
| # Extract AWS parameters from kwargs | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
post_callguardrails run on user input, not LLM outputrun_realtime_guardrailschecks all three event types (pre_call,post_call,realtime_input_transcription) when deciding whether to run a guardrail. This means a guardrail configured withmode: post_call— which is semantically meant to run on the LLM's response — will instead be called on the user's input text during both theconversation.item.createpath and the audio transcription path.A
post_callguardrail author may have written detection logic that only makes sense for response content (e.g., checking for hallucinations, formatting violations, or PII in the assistant's reply). Running that sameapply_guardrailon user input text could produce false positives or false negatives.Consider either:
pre_callandrealtime_input_transcriptionfor the input-side check (since those are semantically about guarding input), orpost_callguardrails will also gate user input in the realtime context so guardrail authors can account for it.