From 1c4a69bfbe34c6c7d4b716837abe50dc8d341fe3 Mon Sep 17 00:00:00 2001 From: kerry Date: Tue, 6 Oct 2026 01:17:36 +0000 Subject: [PATCH] fix(caching): pick the Responses text field without a Final inside a loop Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../prompt_templates/common_utils.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py index 3e5aa4747cf..683e02e9e8f 100644 --- a/litellm/litellm_core_utils/prompt_templates/common_utils.py +++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py @@ -297,14 +297,16 @@ def _responses_text_parts(value: object, positions: Mapping[str, int]) -> Iterat def _responses_first_text_field(item: object, positions: Mapping[str, int]) -> Iterator[str]: - for key in ("text", "output", "input_text", "output_text"): - text: Final = _field(item, key) - if isinstance(text, (list, tuple)): - yield from _responses_text_parts(text, positions) - return - if isinstance(text, str) and text.strip(): - yield text.strip() - return + fields: Final = (_field(item, key) for key in ("text", "output", "input_text", "output_text")) + text: Final = next((field for field in fields if _is_text_field(field)), None) + if isinstance(text, (list, tuple)): + yield from _responses_text_parts(text, positions) + elif isinstance(text, str): + yield text.strip() + + +def _is_text_field(field: object) -> bool: + return isinstance(field, (list, tuple)) or (isinstance(field, str) and bool(field.strip())) def _tool_call_text(name: object, arguments: object) -> str: