From a5d6dece59aeed794330046d69b8c281aa79d121 Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Wed, 30 Sep 2026 13:29:33 +0300 Subject: [PATCH 1/6] fix(responses): drop empty text content items in chat translation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Responses→chat kept output_text blocks with text="", so resumed tool-only turns became content=[{"type":"text","text":""}] and backends like Z.ai rejected them (error 1210). Skip empty text parts and use content="" when nothing remains. Fixes #43848 Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../transformation.py | 13 ++++- .../test_litellm_completion_responses.py | 49 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index bd239922fd3..b8ab8997a5e 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -1812,6 +1812,8 @@ class LiteLLMCompletionResponsesConfig: content_list: Final[list[str | dict[str, object]]] = [] for item in content: if isinstance(item, str): + if item == "": + continue content_list.append(item) elif isinstance(item, dict): if item.get("type") == "input_file": @@ -1832,9 +1834,12 @@ class LiteLLMCompletionResponsesConfig: OpenAIChatCompletionTextObject(type="text", text=str(encrypted_content)) ) else: - # Skip text blocks with None text to avoid downstream errors + # Skip text blocks with None/empty text. Empty string items + # (common on tool-only Responses turns) become + # content=[{"type":"text","text":""}], which several + # OpenAI-compatible backends reject (e.g. Z.ai 1210). text_value = item.get("text") - if text_value is None: + if text_value is None or text_value == "": continue content_block: dict[str, object] = { "type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type( @@ -1845,6 +1850,10 @@ class LiteLLMCompletionResponsesConfig: if "cache_control" in item: content_block["cache_control"] = item["cache_control"] content_list.append(content_block) + # Prefer string empty content over an empty list of text parts: the + # latter is what upstreams flag as invalid, while content="" is fine. + if not content_list: + return "" return content_list else: raise ValueError(f"Invalid content type: {type(content)}") diff --git a/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py b/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py index 7b9de4644b4..1693cd2258b 100644 --- a/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py +++ b/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py @@ -1606,6 +1606,55 @@ class TestContentTypeTransformation: assert result[0]["text"] == "valid text" assert result[1]["text"] == "another valid" + def test_empty_string_text_blocks_filtered_out(self): + """Empty text parts must not become content=[{"type":"text","text":""}]. + + Codex/Responses replays of tool-only turns use output_text with text="". + Backends such as Z.ai reject that shape (error 1210); content="" is OK. + """ + mixed = [ + {"type": "output_text", "text": "keep me"}, + {"type": "output_text", "text": ""}, + {"type": "text", "text": "and me"}, + ] + result = LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content( + mixed + ) + assert result == [ + {"type": "text", "text": "keep me"}, + {"type": "text", "text": "and me"}, + ] + + only_empty = [{"type": "output_text", "text": ""}] + assert ( + LiteLLMCompletionResponsesConfig._transform_responses_api_content_to_chat_completion_content( + only_empty + ) + == "" + ) + + def test_assistant_message_with_empty_output_text_uses_string_content(self): + """Responses→chat must not emit an empty text item for a silent turn.""" + messages = LiteLLMCompletionResponsesConfig._transform_responses_api_input_item_to_chat_completion_message( + input_item={ + "type": "message", + "role": "assistant", + "content": [{"type": "output_text", "text": ""}], + }, + replay_reasoning=True, + ) + assert len(messages) == 1 + assert messages[0]["role"] == "assistant" + assert messages[0]["content"] == "" + content = messages[0]["content"] + assert not ( + isinstance(content, list) + and any( + isinstance(part, dict) and part.get("type") == "text" and part.get("text") == "" + for part in content + ) + ) + def test_encrypted_content_blocks_preserved_as_text(self): """ OpenAI Responses agent messages can include encrypted_content blocks. From 2dedab4a01452e8e70390f77de2c564cfaa31f27 Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:21:03 +0300 Subject: [PATCH 2/6] fix(responses): keep empty trailing merges from reintroducing blank text Assistant merge after tool_calls wrapped content="" as {"type":"text","text":""}, undoing the empty-part filter and recreating the Z.ai 1210 shape. Skip empty trailing content on merge and extract the content-dict mapper to stay within the C901 strict budget. Fixes #43848 Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../transformation.py | 106 +++++++++--------- .../test_litellm_completion_responses.py | 40 +++++++ 2 files changed, 96 insertions(+), 50 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index b8ab8997a5e..bdcc89f52fb 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -907,6 +907,12 @@ class LiteLLMCompletionResponsesConfig: new_content: Final = new_message.get("content") if new_content is None: return None + # Empty trailing assistant turns (tool-only Responses replays) must not + # be wrapped as {"type":"text","text":""} when merged onto prior text. + if new_content in ("", []): + return cast( # cast-ok: trailing assistant unchanged + ChatCompletionAssistantMessage, last_message + ) previous_content: Final = last_message.get("content") content: Final = ( new_content @@ -915,7 +921,7 @@ class LiteLLMCompletionResponsesConfig: block for value in (previous_content, new_content) for block in ( - (ChatCompletionTextObject(type="text", text=value),) + (() if value == "" else (ChatCompletionTextObject(type="text", text=value),)) if isinstance(value, str) else _OBJECT_LIST_ADAPTER.validate_python(value) ) @@ -1792,6 +1798,41 @@ class LiteLLMCompletionResponsesConfig: return ChatCompletionImageObject(type="image_url", image_url=image_url_obj) + @staticmethod + def _chat_completion_part_from_responses_dict_item( + item: dict[str, object], + ) -> str | dict[str, object] | None: + """Map one Responses content dict to a chat part, or None to drop it. + + Empty/None text parts are dropped so tool-only turns do not become + content=[{"type":"text","text":""}] (rejected by backends like Z.ai 1210). + """ + item_type = item.get("type") + if item_type == "input_file": + return LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(item) + if item_type == "input_image": + image_block = _STR_KEY_DICT_ADAPTER.validate_python( + dict(LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(item)) + ) + if "cache_control" in item: + image_block["cache_control"] = item["cache_control"] + return image_block + if item_type == "encrypted_content": + encrypted_content = item.get("encrypted_content") + if encrypted_content is None: + return None + return OpenAIChatCompletionTextObject(type="text", text=str(encrypted_content)) + text_value = item.get("text") + if text_value is None or text_value == "": + return None + content_block: dict[str, object] = { + "type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type(item_type or "text"), + "text": text_value, + } + if "cache_control" in item: + content_block["cache_control"] = item["cache_control"] + return content_block + @staticmethod def _transform_responses_api_content_to_chat_completion_content( content: object, @@ -1806,57 +1847,22 @@ class LiteLLMCompletionResponsesConfig: # Defensive check: should not happen if callers check first # Return empty string as fallback to avoid type errors return "" - elif isinstance(content, str): + if isinstance(content, str): return content - elif isinstance(content, list): - content_list: Final[list[str | dict[str, object]]] = [] - for item in content: - if isinstance(item, str): - if item == "": - continue - content_list.append(item) - elif isinstance(item, dict): - if item.get("type") == "input_file": - content_list.append( - LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(item) - ) - elif item.get("type") == "input_image": - image_block = _STR_KEY_DICT_ADAPTER.validate_python( - dict(LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(item)) - ) - if "cache_control" in item: - image_block["cache_control"] = item["cache_control"] - content_list.append(image_block) - elif item.get("type") == "encrypted_content": - encrypted_content = item.get("encrypted_content") - if encrypted_content is not None: - content_list.append( - OpenAIChatCompletionTextObject(type="text", text=str(encrypted_content)) - ) - else: - # Skip text blocks with None/empty text. Empty string items - # (common on tool-only Responses turns) become - # content=[{"type":"text","text":""}], which several - # OpenAI-compatible backends reject (e.g. Z.ai 1210). - text_value = item.get("text") - if text_value is None or text_value == "": - continue - content_block: dict[str, object] = { - "type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type( - item.get("type") or "text" - ), - "text": text_value, - } - if "cache_control" in item: - content_block["cache_control"] = item["cache_control"] - content_list.append(content_block) - # Prefer string empty content over an empty list of text parts: the - # latter is what upstreams flag as invalid, while content="" is fine. - if not content_list: - return "" - return content_list - else: + if not isinstance(content, list): raise ValueError(f"Invalid content type: {type(content)}") + content_list: Final[list[str | dict[str, object]]] = [] + for item in content: + if isinstance(item, str): + if item != "": + content_list.append(item) + continue + if isinstance(item, dict): + part = LiteLLMCompletionResponsesConfig._chat_completion_part_from_responses_dict_item(item) + if part is not None: + content_list.append(part) + # Prefer content="" over [] — empty text-item lists are flagged invalid. + return content_list if content_list else "" @staticmethod def _get_chat_completion_request_content_type( diff --git a/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py b/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py index 1693cd2258b..87c68eeb575 100644 --- a/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py +++ b/tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py @@ -1655,6 +1655,46 @@ class TestContentTypeTransformation: ) ) + def test_trailing_empty_assistant_merge_does_not_reintroduce_empty_text(self): + """Merge after tool_calls must not wrap content="" as an empty text part. + + History: assistant text + function_call, then assistant output_text="". + The content transform yields "", but _merged_trailing_assistant_message + used to turn that back into {"type":"text","text":""} beside prior text. + """ + messages = LiteLLMCompletionResponsesConfig.transform_responses_api_input_to_messages( + input=[ + { + "type": "message", + "role": "assistant", + "content": [{"type": "output_text", "text": "earlier text"}], + }, + { + "type": "function_call", + "call_id": "call_1", + "name": "lookup", + "arguments": "{}", + }, + { + "type": "message", + "role": "assistant", + "content": [{"type": "output_text", "text": ""}], + }, + ], + responses_api_request={}, + replay_reasoning=True, + ) + assistant_msgs = [m for m in messages if isinstance(m, dict) and m.get("role") == "assistant"] + assert assistant_msgs, messages + for msg in assistant_msgs: + content = msg.get("content") + if isinstance(content, list): + assert not any( + isinstance(part, dict) and part.get("type") == "text" and part.get("text") == "" + for part in content + ), msg + assert content != [{"type": "text", "text": ""}] + def test_encrypted_content_blocks_preserved_as_text(self): """ OpenAI Responses agent messages can include encrypted_content blocks. From 1ed04103b4f61b95d14a95b2bd3ad4b7e5f285c9 Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:27:26 +0300 Subject: [PATCH 3/6] fix(responses): silence LIT002 on empty-merge / content-part helpers Drop the empty-list literal from the trailing-merge guard and annotate the moved content-part dict/list constructions with mutable-ok so the type- discipline gate stays within budget. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../transformation.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index bdcc89f52fb..0add265eb29 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -909,7 +909,7 @@ class LiteLLMCompletionResponsesConfig: return None # Empty trailing assistant turns (tool-only Responses replays) must not # be wrapped as {"type":"text","text":""} when merged onto prior text. - if new_content in ("", []): + if new_content == "": return cast( # cast-ok: trailing assistant unchanged ChatCompletionAssistantMessage, last_message ) @@ -1812,7 +1812,9 @@ class LiteLLMCompletionResponsesConfig: return LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(item) if item_type == "input_image": image_block = _STR_KEY_DICT_ADAPTER.validate_python( - dict(LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(item)) + dict( # mutable-ok: adapter needs a plain dict for cache_control mutation + LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(item) + ) ) if "cache_control" in item: image_block["cache_control"] = item["cache_control"] @@ -1825,7 +1827,7 @@ class LiteLLMCompletionResponsesConfig: text_value = item.get("text") if text_value is None or text_value == "": return None - content_block: dict[str, object] = { + content_block: dict[str, object] = { # mutable-ok: outbound chat content part "type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type(item_type or "text"), "text": text_value, } @@ -1851,7 +1853,7 @@ class LiteLLMCompletionResponsesConfig: return content if not isinstance(content, list): raise ValueError(f"Invalid content type: {type(content)}") - content_list: Final[list[str | dict[str, object]]] = [] + content_list: Final[list[str | dict[str, object]]] = [] # mutable-ok: accumulator for item in content: if isinstance(item, str): if item != "": From 631ca6584019244621b8d073549b0156ced47ee2 Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:24:32 +0100 Subject: [PATCH 4/6] fix(responses): keep invalid-content raise outside an isinstance guard Restores the original if/raise shape so the strict ruff gate's TRY004 count does not grow. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../transformation.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 61c4177f12a..5d2d41f1e40 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -2349,20 +2349,20 @@ class LiteLLMCompletionResponsesConfig: return "" if isinstance(content, str): return content - if not isinstance(content, list): - raise ValueError(f"Invalid content type: {type(content)}") - content_list: Final[list[str | dict[str, object]]] = [] # mutable-ok: accumulator - for item in content: - if isinstance(item, str): - if item != "": - content_list.append(item) - continue - if isinstance(item, dict): - part = LiteLLMCompletionResponsesConfig._chat_completion_part_from_responses_dict_item(item) - if part is not None: - content_list.append(part) - # Prefer content="" over [] — empty text-item lists are flagged invalid. - return content_list if content_list else "" + if isinstance(content, list): + content_list: Final[list[str | dict[str, object]]] = [] # mutable-ok: accumulator + for item in content: + if isinstance(item, str): + if item != "": + content_list.append(item) + continue + if isinstance(item, dict): + part = LiteLLMCompletionResponsesConfig._chat_completion_part_from_responses_dict_item(item) + if part is not None: + content_list.append(part) + # Prefer content="" over [] — empty text-item lists are flagged invalid. + return content_list if content_list else "" + raise ValueError(f"Invalid content type: {type(content)}") @staticmethod def _get_chat_completion_request_content_type( From 1256bb4a3a10fe0870ae0f906199339c04093f42 Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:33:36 +0100 Subject: [PATCH 5/6] fix(responses): declare Final on new chat-part locals Satisfies the type-discipline gate (LIT010/LIT013) for the helper added in this PR. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../transformation.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 5d2d41f1e40..ec6f26458c6 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -2305,27 +2305,25 @@ class LiteLLMCompletionResponsesConfig: Empty/None text parts are dropped so tool-only turns do not become content=[{"type":"text","text":""}] (rejected by backends like Z.ai 1210). """ - item_type = item.get("type") + item_type: Final = item.get("type") if item_type == "input_file": return LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(item) if item_type == "input_image": - image_block = _STR_KEY_DICT_ADAPTER.validate_python( - dict( # mutable-ok: adapter needs a plain dict for cache_control mutation - LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(item) - ) + image_block: Final = _STR_KEY_DICT_ADAPTER.validate_python( + dict(LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(item)) ) if "cache_control" in item: image_block["cache_control"] = item["cache_control"] return image_block if item_type == "encrypted_content": - encrypted_content = item.get("encrypted_content") + encrypted_content: Final = item.get("encrypted_content") if encrypted_content is None: return None return OpenAIChatCompletionTextObject(type="text", text=str(encrypted_content)) - text_value = item.get("text") + text_value: Final = item.get("text") if text_value is None or text_value == "": return None - content_block: dict[str, object] = { # mutable-ok: outbound chat content part + content_block: Final[dict[str, object]] = { # mutable-ok: outbound chat content part "type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type(item_type or "text"), "text": text_value, } From 7fcacdda18acd9783f42bbc2157280ffbcce729c Mon Sep 17 00:00:00 2001 From: GokayAI <60583610+gokay-ai@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:46:55 +0100 Subject: [PATCH 6/6] refactor(responses): keep empty-text filtering inline in the content loop Drops the extracted helper and keeps the original loop shape, so the change is limited to skipping empty strings / empty text parts and returning "" when nothing is left. Keeps basedpyright, strict ruff and type-discipline counts at the merge-base level. Signed-off-by: GokayAI <60583610+gokay-ai@users.noreply.github.com> --- .../transformation.py | 86 +++++++++---------- 1 file changed, 40 insertions(+), 46 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index ec6f26458c6..31d1e6cad88 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -2296,41 +2296,6 @@ class LiteLLMCompletionResponsesConfig: return ChatCompletionImageObject(type="image_url", image_url=image_url_obj) - @staticmethod - def _chat_completion_part_from_responses_dict_item( - item: dict[str, object], - ) -> str | dict[str, object] | None: - """Map one Responses content dict to a chat part, or None to drop it. - - Empty/None text parts are dropped so tool-only turns do not become - content=[{"type":"text","text":""}] (rejected by backends like Z.ai 1210). - """ - item_type: Final = item.get("type") - if item_type == "input_file": - return LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(item) - if item_type == "input_image": - image_block: Final = _STR_KEY_DICT_ADAPTER.validate_python( - dict(LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(item)) - ) - if "cache_control" in item: - image_block["cache_control"] = item["cache_control"] - return image_block - if item_type == "encrypted_content": - encrypted_content: Final = item.get("encrypted_content") - if encrypted_content is None: - return None - return OpenAIChatCompletionTextObject(type="text", text=str(encrypted_content)) - text_value: Final = item.get("text") - if text_value is None or text_value == "": - return None - content_block: Final[dict[str, object]] = { # mutable-ok: outbound chat content part - "type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type(item_type or "text"), - "text": text_value, - } - if "cache_control" in item: - content_block["cache_control"] = item["cache_control"] - return content_block - @staticmethod def _transform_responses_api_content_to_chat_completion_content( content: object, @@ -2345,22 +2310,51 @@ class LiteLLMCompletionResponsesConfig: # Defensive check: should not happen if callers check first # Return empty string as fallback to avoid type errors return "" - if isinstance(content, str): + elif isinstance(content, str): return content - if isinstance(content, list): - content_list: Final[list[str | dict[str, object]]] = [] # mutable-ok: accumulator + elif isinstance(content, list): + content_list: Final[list[str | dict[str, object]]] = [] for item in content: if isinstance(item, str): - if item != "": + if item: content_list.append(item) - continue - if isinstance(item, dict): - part = LiteLLMCompletionResponsesConfig._chat_completion_part_from_responses_dict_item(item) - if part is not None: - content_list.append(part) - # Prefer content="" over [] — empty text-item lists are flagged invalid. + elif isinstance(item, dict): + if item.get("type") == "input_file": + content_list.append( + LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(item) + ) + elif item.get("type") == "input_image": + image_block = _STR_KEY_DICT_ADAPTER.validate_python( + dict(LiteLLMCompletionResponsesConfig._transform_input_image_item_to_image_item(item)) + ) + if "cache_control" in item: + image_block["cache_control"] = item["cache_control"] + content_list.append(image_block) + elif item.get("type") == "encrypted_content": + encrypted_content = item.get("encrypted_content") + if encrypted_content is not None: + content_list.append( + OpenAIChatCompletionTextObject(type="text", text=str(encrypted_content)) + ) + else: + # Skip None/empty text blocks: content=[{"type":"text","text":""}] + # is rejected by some backends (e.g. Z.ai 1210). + text_value = item.get("text") + if text_value is None or text_value == "": + continue + content_block: dict[str, object] = { + "type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type( + item.get("type") or "text" + ), + "text": text_value, + } + if "cache_control" in item: + content_block["cache_control"] = item["cache_control"] + content_list.append(content_block) + # Prefer content="" over [] when every part was dropped. return content_list if content_list else "" - raise ValueError(f"Invalid content type: {type(content)}") + else: + raise ValueError(f"Invalid content type: {type(content)}") @staticmethod def _get_chat_completion_request_content_type(