Skip to content

Commit 152427e

Browse files
authored
make image inputs compatible with langchain_ollama (langchain-ai#24619)
1 parent 0535d72 commit 152427e

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

libs/partners/ollama/langchain_ollama/chat_models.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def _convert_messages_to_ollama_messages(
346346
) -> Sequence[Message]:
347347
ollama_messages: List = []
348348
for message in messages:
349-
role = ""
349+
role: Literal["user", "assistant", "system", "tool"]
350350
tool_call_id: Optional[str] = None
351351
tool_calls: Optional[List[Dict[str, Any]]] = None
352352
if isinstance(message, HumanMessage):
@@ -383,11 +383,13 @@ def _convert_messages_to_ollama_messages(
383383
image_url = None
384384
temp_image_url = content_part.get("image_url")
385385
if isinstance(temp_image_url, str):
386-
image_url = content_part["image_url"]
386+
image_url = temp_image_url
387387
elif (
388-
isinstance(temp_image_url, dict) and "url" in temp_image_url
388+
isinstance(temp_image_url, dict)
389+
and "url" in temp_image_url
390+
and isinstance(temp_image_url["url"], str)
389391
):
390-
image_url = temp_image_url
392+
image_url = temp_image_url["url"]
391393
else:
392394
raise ValueError(
393395
"Only string image_url or dict with string 'url' "
@@ -408,15 +410,16 @@ def _convert_messages_to_ollama_messages(
408410
"Must either have type 'text' or type 'image_url' "
409411
"with a string 'image_url' field."
410412
)
411-
msg = {
413+
# Should convert to ollama.Message once role includes tool, and tool_call_id is in Message # noqa: E501
414+
msg: dict = {
412415
"role": role,
413416
"content": content,
414417
"images": images,
415418
}
419+
if tool_calls:
420+
msg["tool_calls"] = tool_calls # type: ignore
416421
if tool_call_id:
417422
msg["tool_call_id"] = tool_call_id
418-
if tool_calls:
419-
msg["tool_calls"] = tool_calls
420423
ollama_messages.append(msg)
421424

422425
return ollama_messages

libs/partners/ollama/tests/integration_tests/test_chat_models.py

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import Type
44

5+
import pytest
6+
from langchain_core.language_models import BaseChatModel
57
from langchain_standard_tests.integration_tests import ChatModelIntegrationTests
68

79
from langchain_ollama.chat_models import ChatOllama
@@ -15,3 +17,23 @@ def chat_model_class(self) -> Type[ChatOllama]:
1517
@property
1618
def chat_model_params(self) -> dict:
1719
return {"model": "llama3-groq-tool-use"}
20+
21+
@property
22+
def supports_image_inputs(self) -> bool:
23+
return True
24+
25+
@pytest.mark.xfail(
26+
reason=(
27+
"Fails with 'AssertionError'. Ollama does not support 'tool_choice' yet."
28+
)
29+
)
30+
def test_structured_output(self, model: BaseChatModel) -> None:
31+
super().test_structured_output(model)
32+
33+
@pytest.mark.xfail(
34+
reason=(
35+
"Fails with 'AssertionError'. Ollama does not support 'tool_choice' yet."
36+
)
37+
)
38+
def test_structured_output_pydantic_2_v1(self, model: BaseChatModel) -> None:
39+
super().test_structured_output_pydantic_2_v1(model)

0 commit comments

Comments
 (0)