Repository navigation
Fix fail-open for grayswan and pass metadata to cygnal api endpoint #19837
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 2 commits
292f924
9b6b3ca
cb1ed19
4eece15
7800389
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 |
|---|---|---|
|
|
@@ -9,8 +9,10 @@ | |
| from litellm._logging import verbose_proxy_logger | ||
| from litellm.integrations.custom_guardrail import ( | ||
| CustomGuardrail, | ||
| ModifyResponseException | ||
| ) | ||
| from litellm.litellm_core_utils.safe_json_dumps import safe_dumps | ||
| from litellm.litellm_core_utils.safe_json_loads import safe_json_loads | ||
| from litellm.llms.custom_httpx.http_handler import ( | ||
| get_async_httpx_client, | ||
| httpxSpecialProvider, | ||
|
|
@@ -205,9 +207,13 @@ async def apply_guardrail( | |
|
|
||
| # Get dynamic params from request metadata | ||
| dynamic_body = self.get_guardrail_dynamic_request_body_params(request_data) or {} | ||
| if dynamic_body: | ||
| verbose_proxy_logger.debug( | ||
| "Gray Swan Guardrail: dynamic extra_body=%s", safe_dumps(dynamic_body) | ||
| ) | ||
|
|
||
| # Prepare and send payload | ||
| payload = self._prepare_payload(messages, dynamic_body) | ||
| payload = self._prepare_payload(messages, dynamic_body, request_data) | ||
| if payload is None: | ||
| return inputs | ||
|
|
||
|
|
@@ -222,6 +228,32 @@ async def apply_guardrail( | |
| is_output=is_output, | ||
| ) | ||
| return result | ||
| except ModifyResponseException: | ||
| # Guardrail decision (passthrough) should always propagate, | ||
| # regardless of fail_open. | ||
| raise | ||
| except HTTPException as exc: | ||
| # HTTP Exception could be from "blocked", should not be fail-open in this case. | ||
| if self._is_grayswan_block_exception(exc): | ||
| raise | ||
| end_time = time.time() | ||
| status_code = getattr(exc, "status_code", None) or getattr( | ||
| exc, "exception_status_code", None | ||
| ) | ||
| self._log_guardrail_failure( | ||
| exc=exc, | ||
| request_data=request_data or {}, | ||
| start_time=start_time, | ||
| end_time=end_time, | ||
| status_code=status_code, | ||
| ) | ||
| if self.fail_open: | ||
| verbose_proxy_logger.warning( | ||
| "Gray Swan Guardrail: fail_open=True. Allowing request to proceed despite error: %s", | ||
| exc, | ||
| ) | ||
| return inputs | ||
| raise GraySwanGuardrailAPIError(str(exc), status_code=status_code) from exc | ||
| except Exception as exc: | ||
| end_time = time.time() | ||
| status_code = getattr(exc, "status_code", None) or getattr( | ||
|
|
@@ -240,8 +272,16 @@ async def apply_guardrail( | |
| exc, | ||
| ) | ||
| return inputs | ||
| if isinstance(exc, GraySwanGuardrailAPIError): | ||
| raise exc | ||
| raise GraySwanGuardrailAPIError(str(exc), status_code=status_code) from exc | ||
|
|
||
| def _is_grayswan_block_exception(self, exc: HTTPException) -> bool: | ||
| detail = getattr(exc, "detail", None) | ||
| if isinstance(detail, dict): | ||
| return detail.get("error") == "Blocked by Gray Swan Guardrail" | ||
|
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.
this should be in constants.py file
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. can grayswan guarantee this won't change ?
Contributor
Author
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. In global constants.py might be not the best idea? I put it as a local constant so when the error is raised when this is checked it is always going to use the same const so should be fine now. |
||
| return False | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Legacy Test Interface (for backward compatibility) | ||
| # ------------------------------------------------------------------ | ||
|
|
@@ -494,7 +534,7 @@ def _prepare_headers(self) -> Dict[str, str]: | |
| } | ||
|
|
||
| def _prepare_payload( | ||
| self, messages: List[Dict[str, str]], dynamic_body: dict | ||
| self, messages: List[Dict[str, str]], dynamic_body: dict, request_data: dict | ||
| ) -> Optional[Dict[str, Any]]: | ||
| payload: Dict[str, Any] = {"messages": messages} | ||
|
|
||
|
|
@@ -510,6 +550,18 @@ def _prepare_payload( | |
| if reasoning_mode: | ||
| payload["reasoning_mode"] = reasoning_mode | ||
|
|
||
| # Pass through arbitrary metadata when provided via dynamic extra_body. | ||
| if "metadata" in dynamic_body: | ||
| payload["metadata"] = dynamic_body["metadata"] | ||
|
|
||
| litellm_metadata = request_data.get("litellm_metadata") | ||
| if isinstance(litellm_metadata, dict) and litellm_metadata: | ||
| cleaned_litellm_metadata = dict(litellm_metadata) | ||
| # cleaned_litellm_metadata.pop("user_api_key_auth", None) | ||
| sanitized = safe_json_loads(safe_dumps(cleaned_litellm_metadata), default={}) | ||
| if isinstance(sanitized, dict) and sanitized: | ||
| payload["litellm_metadata"] = sanitized | ||
|
|
||
| return payload | ||
|
|
||
| def _format_violation_message( | ||
|
|
||
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.
can this section be a function itself. handle_grayswan_exception()
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.
optimize the exception handling to be much cleaner