From 8330be32216eb0740daeed6cd783741043c4252a Mon Sep 17 00:00:00 2001 From: rain <1504569896@qq.com> Date: Mon, 21 Sep 2026 00:10:24 +0000 Subject: [PATCH] refactor(responses bridge): build system text extraction without a mutable list _extract_system_text accumulated text parts with append; build the joined text from an immutable tuple comprehension instead, keeping the same semantics (a non-text or cache-bearing block still falls back to an input item). Signed-off-by: rain <1504569896@qq.com> --- .../transformation.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.py index 3bdf93f5b0e..0c29f4ae0a7 100644 --- a/litellm/completion_extras/litellm_responses_transformation/transformation.py +++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py @@ -515,15 +515,16 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge): if isinstance(content, str): return content if isinstance(content, list): - text_parts: list[str] = [] - for block in content: - if not isinstance(block, dict) or block.get("type") != "text": - return None - if "cache_control" in block: - return None - text = block.get("text") - if isinstance(text, str): - text_parts.append(text) + if any( + not isinstance(block, dict) + or block.get("type") != "text" + or "cache_control" in block + for block in content + ): + return None + text_parts: Final = tuple( + block["text"] for block in content if isinstance(block.get("text"), str) + ) return "".join(text_parts) if text_parts else None return None