guard empty input_file parts to avoid hollow file blocks

This commit is contained in:
Ines Magessi 2026-05-19 10:49:25 +02:00
parent 7ce0bf2c7b
commit 0f61117e4b
2 changed files with 25 additions and 4 deletions

View file

@ -1086,11 +1086,11 @@ class LiteLLMCompletionResponsesConfig:
}
)
elif part_type in ("input_file", "file"):
normalized_blocks.append(
LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(
part
)
file_item = LiteLLMCompletionResponsesConfig._transform_input_file_item_to_file_item(
part
)
if file_item.get("file"):
normalized_blocks.append(file_item)
# Prefer structured blocks if we have any non-text part
# (images or files); otherwise return a string.

View file

@ -107,3 +107,24 @@ def test_function_call_output_input_file_with_file_id_is_preserved():
file_blocks = [b for b in content if b.get("type") == "file"]
assert len(file_blocks) == 1
assert file_blocks[0]["file"]["file_id"] == "file-abc123"
def test_function_call_output_input_file_without_data_is_skipped():
"""An input_file part with no file_id, file_url, or file_data carries no
payload — appending it as ``{"type": "file", "file": {}}`` would activate
the structured-content path and emit a hollow block that downstream
adapters (Gemini, Bedrock) reject or silently ignore. Skip it instead."""
out = LiteLLMCompletionResponsesConfig._transform_responses_api_tool_call_output_to_chat_completion_message(
tool_call_output={
"type": "function_call_output",
"call_id": "call_empty_file",
"output": [
{"type": "input_text", "text": "Just text here."},
{"type": "input_file", "filename": "missing.pdf"},
],
}
)
assert len(out) == 1
# No usable file payload -> fall back to the plain-string text path.
assert out[0]["content"] == "Just text here."