From 9f231af27036f5f8ea74a8cf0b99610ecf46993e Mon Sep 17 00:00:00 2001 From: Ultronen <82553854+Ultronen@users.noreply.github.com> Date: Sun, 6 Sep 2026 18:22:18 +0800 Subject: [PATCH] refactor(proxy): keep guardrail projection immutable --- litellm/proxy/utils.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index e1ec9444a6e..9c9f643d626 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -907,23 +907,18 @@ def _streaming_guardrail_response_text(*, complete_response: str, response: obje if not isinstance(response, (ModelResponse, ModelResponseStream)): return complete_response response_dict: Final = response.model_dump(mode="json", exclude_none=True) - structured_choices: Final = tuple( - {key: delta[key] for key in ("tool_calls", "function_call") if delta.get(key) is not None} - for choice in response_dict.get("choices", []) + structured_fields: Final = tuple( + f"{key}:{json.dumps(delta[key], ensure_ascii=False, separators=(',', ':'), sort_keys=True)}" + for choice in response_dict.get("choices", ()) if isinstance(choice, dict) for delta in (choice.get("delta"),) - if isinstance(delta, dict) and any(delta.get(key) is not None for key in ("tool_calls", "function_call")) + if isinstance(delta, dict) + for key in ("tool_calls", "function_call") + if delta.get(key) is not None ) - if not structured_choices: + if not structured_fields: return complete_response - return _StreamingHookResponseText( - json.dumps( - {"content": str(complete_response), "choices": structured_choices}, - ensure_ascii=False, - separators=(",", ":"), - sort_keys=True, - ) - ) + return _StreamingHookResponseText("\n".join((str(complete_response), *structured_fields))) def _is_unchanged_structured_streaming_hook_response(