Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,8 @@ def _format_user_message(self, message: Message) -> list[OllamaMessage]:
user_message = OllamaMessage(role="user", content=message.text)
data_contents = [c for c in message.contents if c.type == "data"]
if data_contents:
if not any(c.has_top_level_media_type("image") for c in data_contents):
# Every data item is sent in `images`, so reject the message if any of them is not an image.
if not all(c.has_top_level_media_type("image") for c in data_contents):
raise ChatClientInvalidRequestException(
"Only image data content is supported for user messages in Ollama."
)
Expand Down
26 changes: 26 additions & 0 deletions python/packages/ollama/tests/test_ollama_chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,32 @@ async def test_cmc_with_invalid_data_content_media_type(
await ollama_client.get_response(messages=chat_history)


@patch.object(AsyncClient, "chat", new_callable=AsyncMock)
async def test_cmc_with_image_and_non_image_data_content(
mock_chat: AsyncMock,
ollama_unit_test_env: dict[str, str],
chat_history: list[Message],
mock_chat_completion_response: OllamaChatResponse,
) -> None:
mock_chat.return_value = mock_chat_completion_response
# An image must not let other data content (here a PDF) be sent to Ollama as an image
chat_history.append(
Message(
contents=[
Content.from_uri(uri="data:image/png;base64,xyz", media_type="image/png"),
Content.from_uri(uri="data:application/pdf;base64,abc", media_type="application/pdf"),
],
role="user",
)
)

ollama_client = OllamaChatClient()

with pytest.raises(ChatClientInvalidRequestException):
await ollama_client.get_response(messages=chat_history)
mock_chat.assert_not_called()


@patch.object(AsyncClient, "chat", new_callable=AsyncMock)
async def test_cmc_with_invalid_content_type(
mock_chat: AsyncMock,
Expand Down
Loading