This commit is contained in:
GokayAI 2026-10-24 16:31:46 +00:00 • committed by GitHub
commit 52bc491dcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 102 additions and 4 deletions

View file

@ -1616,6 +1616,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 == "":
return cast( # cast-ok: trailing assistant unchanged
ChatCompletionAssistantMessage, last_message
)
previous_content: Final = last_message.get("content")
previous_cache_control: Final = _input_item_cache_control(last_message)
new_cache_control: Final = _input_item_cache_control(new_message)
@ -2539,7 +2545,8 @@ class LiteLLMCompletionResponsesConfig:
content_list: Final[list[str | dict[str, object]]] = []
for item in content:
if isinstance(item, str):
content_list.append(item)
if item:
content_list.append(item)
elif isinstance(item, dict):
if item.get("type") == "input_file":
content_list.append(
@ -2559,9 +2566,10 @@ class LiteLLMCompletionResponsesConfig:
OpenAIChatCompletionTextObject(type="text", text=str(encrypted_content))
)
else:
# Skip text blocks with None text to avoid downstream errors
# 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:
if text_value is None or text_value == "":
continue
content_block: dict[str, object] = {
"type": LiteLLMCompletionResponsesConfig._get_chat_completion_request_content_type(
@ -2572,7 +2580,8 @@ class LiteLLMCompletionResponsesConfig:
if "cache_control" in item:
content_block["cache_control"] = item["cache_control"]
content_list.append(content_block)
return content_list
# Prefer content="" over [] when every part was dropped.
return content_list if content_list else ""
else:
raise ValueError(f"Invalid content type: {type(content)}")

View file

@ -1577,6 +1577,95 @@ 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_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.