Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge branch 'litellm_internal_staging' into litellm_oss_staging

Cursor Agent1b6ab9facff6
diff --cc litellm/litellm_core_utils/prompt_templates/factory.py
index a9df0895572,fe8387476ee..3a83162fb20
--- a/litellm/litellm_core_utils/prompt_templates/factory.py
+++ b/litellm/litellm_core_utils/prompt_templates/factory.py
@@@ -4042,41 -3977,6 +4042,55 @@@ def _convert_to_bedrock_tool_call_resul
tool_result_content_blocks.append(
BedrockToolResultContentBlock(image=_block["image"])
)
+ elif "document" in _block:
+ tool_result_content_blocks.append(
+ BedrockToolResultContentBlock(document=_block["document"])
+ )
++ else:
++ verbose_logger.warning(
++ "Bedrock Converse: unrecognized BedrockContentBlock keys "
++ "%s for image_url tool-result block %s; dropping.",
++ list(_block.keys()),
++ content,
++ )
+ elif content["type"] == "file":
+ # Match the user-message path (_process_file_message): accept
+ # either file_data (base64 data URI) or file_id (server-side
+ # reference / URL) and hand off to BedrockImageProcessor. Raise
+ # BadRequestError on both-None rather than silently dropping.
+ file_obj = content.get("file") or {}
+ file_data = file_obj.get("file_data")
+ file_id = file_obj.get("file_id")
+ if file_data is None and file_id is None:
+ raise litellm.BadRequestError(
+ message="file_data and file_id cannot both be None. Got={}".format(
+ content
+ ),
+ model="",
+ llm_provider="bedrock",
+ )
+ file_format = file_obj.get("format")
+ _file_block: BedrockContentBlock = (
+ BedrockImageProcessor.process_image_sync(
+ image_url=cast(str, file_id or file_data),
+ format=file_format,
+ )
+ )
+ if "document" in _file_block:
+ tool_result_content_blocks.append(
+ BedrockToolResultContentBlock(document=_file_block["document"])
+ )
+ elif "image" in _file_block:
+ tool_result_content_blocks.append(
+ BedrockToolResultContentBlock(image=_file_block["image"])
+ )
++ else:
++ verbose_logger.warning(
++ "Bedrock Converse: unrecognized BedrockContentBlock keys "
++ "%s for file tool-result block %s; dropping.",
++ list(_file_block.keys()),
++ content,
++ )
message.get("name", "")
id = str(message.get("tool_call_id", str(uuid.uuid4())))
diff --cc tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py
index 7ee2f0aa37c,633aed38a08..589bcb4fc0e
--- a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py
+++ b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py
@@@ -4148,219 -4148,37 +4148,255 @@@ def test_transform_response_finish_reas
assert result.choices[0].finish_reason == "stop"
+def test_bedrock_tool_message_openai_file_pdf_becomes_document():
+ """
+ OpenAI Chat Completions `{type: "file", file: {file_data: "data:application/pdf;...", filename}}`
+ inside a tool message content list should translate to a Bedrock
+ toolResult.content[].document block.
+ """
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ _bedrock_converse_messages_pt,
+ )
+
+ pdf_b64 = "JVBERi0xLjQKJeLjz9MK" # tiny "%PDF-1.4\n" header
+ messages = [
+ {"role": "user", "content": "Summarize the attached PDF."},
+ {
+ "tool_call_id": "tooluse_pdf_1",
+ "role": "tool",
+ "name": "fetch_document",
+ "content": [
+ {
+ "type": "file",
+ "file": {
+ "file_data": f"data:application/pdf;base64,{pdf_b64}",
+ "filename": "summary.pdf",
+ },
+ },
+ ],
+ },
+ ]
+
+ translated_msg = _bedrock_converse_messages_pt(
+ messages=messages, model="", llm_provider=""
+ )
+
+ tool_result = translated_msg[-1]["content"][-1]["toolResult"]
+ assert tool_result["toolUseId"] == "tooluse_pdf_1"
+ assert len(tool_result["content"]) == 1
+ block = tool_result["content"][0]
+ assert "document" in block, f"expected document block, got {block}"
+ assert block["document"]["format"] == "pdf"
+ assert block["document"]["source"]["bytes"] == pdf_b64
+ assert block["document"]["name"].startswith("DocumentPDFmessages_")
+ assert block["document"]["name"].endswith("_pdf")
+
+
+def test_bedrock_tool_message_image_url_pdf_data_uri_becomes_document():
+ """
+ Regression for the processor-returns-document-but-wrapper-drops-it bug:
+ when a caller sends a PDF as an `image_url` data URI on the tool-result path,
+ BedrockImageProcessor correctly returns a {"document": ...} block, but the
+ tool-result wrapper only appended the "image" case, silently dropping documents.
+ """
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ _bedrock_converse_messages_pt,
+ )
+
+ pdf_b64 = "JVBERi0xLjQKJeLjz9MK"
+ messages = [
+ {"role": "user", "content": "Summarize the attached PDF."},
+ {
+ "tool_call_id": "tooluse_pdf_img_1",
+ "role": "tool",
+ "name": "fetch_document",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": f"data:application/pdf;base64,{pdf_b64}",
+ },
+ },
+ ],
+ },
+ ]
+
+ translated_msg = _bedrock_converse_messages_pt(
+ messages=messages, model="", llm_provider=""
+ )
+
+ tool_result = translated_msg[-1]["content"][-1]["toolResult"]
+ assert tool_result["toolUseId"] == "tooluse_pdf_img_1"
+ assert len(tool_result["content"]) == 1
+ block = tool_result["content"][0]
+ assert "document" in block, f"expected document block, got {block}"
+ assert block["document"]["format"] == "pdf"
+ assert block["document"]["source"]["bytes"] == pdf_b64
+
+
+def test_bedrock_tool_message_file_id_http_url_becomes_document():
+ """
+ OpenAI `file.file_id` is a server-side file reference. The Bedrock
+ user-message path (_process_file_message at factory.py:4796) accepts either
+ `file_data` or `file_id` and forwards to BedrockImageProcessor. The
+ tool-result path must match: when `file_id` is an http(s) PDF URL, it
+ should resolve to a Bedrock document block, not be silently dropped.
+ """
+ from unittest.mock import patch
+
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ BedrockImageProcessor,
+ _bedrock_converse_messages_pt,
+ )
+
+ pdf_url = "https://example.com/whitepaper.pdf"
+ fake_document_block = {
+ "document": {
+ "format": "pdf",
+ "name": "fake_doc",
+ "source": {"bytes": "ZmFrZQ=="},
+ }
+ }
+ messages = [
+ {"role": "user", "content": "Summarize the attached PDF."},
+ {
+ "tool_call_id": "tooluse_fid_1",
+ "role": "tool",
+ "name": "fetch_document",
+ "content": [
+ {
+ "type": "file",
+ "file": {
+ "file_id": pdf_url,
+ "filename": "whitepaper.pdf",
+ },
+ },
+ ],
+ },
+ ]
+
+ with patch.object(
+ BedrockImageProcessor,
+ "process_image_sync",
+ return_value=fake_document_block,
+ ) as mock_proc:
+ translated_msg = _bedrock_converse_messages_pt(
+ messages=messages, model="", llm_provider=""
+ )
+
+ mock_proc.assert_called_once()
+ assert mock_proc.call_args.kwargs["image_url"] == pdf_url
+
+ tool_result = translated_msg[-1]["content"][-1]["toolResult"]
+ assert len(tool_result["content"]) == 1
+ block = tool_result["content"][0]
+ assert "document" in block, f"expected document block, got {block}"
+ assert block["document"]["source"]["bytes"] == "ZmFrZQ=="
+
+
+def test_bedrock_tool_message_file_without_data_or_id_raises():
+ """
+ The user-message path raises BadRequestError when a `type: "file"` block
+ has neither `file_data` nor `file_id` (factory.py:4802-4809). The
+ tool-result path must match — silently dropping the block makes the model
+ see an empty tool result and obscures the caller bug.
+ """
+ import litellm
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ _bedrock_converse_messages_pt,
+ )
+
+ messages = [
+ {"role": "user", "content": "Summarize."},
+ {
+ "tool_call_id": "tooluse_bad_1",
+ "role": "tool",
+ "name": "fetch_document",
+ "content": [
+ {
+ "type": "file",
+ "file": {"filename": "nothing.pdf"},
+ },
+ ],
+ },
+ ]
+
+ with pytest.raises(litellm.BadRequestError):
+ _bedrock_converse_messages_pt(messages=messages, model="", llm_provider="")
+
+
+def test_bedrock_tool_message_image_url_png_still_becomes_image():
+ """
+ Regression: image_url with an image mime type must continue to translate
+ to a Bedrock image block (not document). Locks in existing behavior after
+ the document-passthrough fix.
+ """
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ _bedrock_converse_messages_pt,
+ )
+
+ png_b64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNgYGBgAAAABQABXvMqOgAAAABJRU5ErkJggg=="
+ messages = [
+ {"role": "user", "content": "Describe the attached image."},
+ {
+ "tool_call_id": "tooluse_png_1",
+ "role": "tool",
+ "name": "fetch_image",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": {
+ "url": f"data:image/png;base64,{png_b64}",
+ },
+ },
+ ],
+ },
+ ]
+
+ translated_msg = _bedrock_converse_messages_pt(
+ messages=messages, model="", llm_provider=""
+ )
+
+ tool_result = translated_msg[-1]["content"][-1]["toolResult"]
+ assert len(tool_result["content"]) == 1
+ block = tool_result["content"][0]
+ assert "image" in block, f"expected image block, got {block}"
+ assert "document" not in block
+ assert block["image"]["format"] == "png"
+ assert block["image"]["source"]["bytes"] == png_b64
++
++
+ def test_transform_response_does_not_leak_body_on_parse_failure():
+ from litellm.llms.bedrock.common_utils import BedrockError
+
+ leaky_body = {"output": {"message": {"content": [{"text": "secret content"}]}}}
+
+ class MockResponse:
+ def json(self):
+ return leaky_body
+
+ @property
+ def text(self):
+ return json.dumps(leaky_body)
+
+ with patch(
+ "litellm.llms.bedrock.chat.converse_transformation.ConverseResponseBlock",
+ side_effect=KeyError("missing required field"),
+ ):
+ with pytest.raises(BedrockError) as exc_info:
+ AmazonConverseConfig()._transform_response(
+ model="bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",
+ response=MockResponse(),
+ model_response=ModelResponse(),
+ stream=False,
+ logging_obj=None,
+ optional_params={},
+ api_key=None,
+ data=None,
+ messages=[],
+ encoding=None,
+ )
+
+ msg = str(exc_info.value)
+ assert "secret content" not in msg
+ assert "Error converting to valid response block" in msg