Skip to content

Commit 085baef

Browse files
authored
ollama[patch]: support standard image format (#30864)
Following #30746
1 parent 47ded80 commit 085baef

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

libs/partners/ollama/langchain_ollama/chat_models.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
SystemMessage,
3131
ToolCall,
3232
ToolMessage,
33+
is_data_content_block,
3334
)
3435
from langchain_core.messages.ai import UsageMetadata
3536
from langchain_core.messages.tool import tool_call
@@ -173,6 +174,20 @@ def _lc_tool_call_to_openai_tool_call(tool_call: ToolCall) -> dict:
173174
}
174175

175176

177+
def _get_image_from_data_content_block(block: dict) -> str:
178+
"""Format standard data content block to format expected by Ollama."""
179+
if block["type"] == "image":
180+
if block["source_type"] == "base64":
181+
return block["data"]
182+
else:
183+
error_message = "Image data only supported through in-line base64 format."
184+
raise ValueError(error_message)
185+
186+
else:
187+
error_message = f"Blocks of type {block['type']} not supported."
188+
raise ValueError(error_message)
189+
190+
176191
def _is_pydantic_class(obj: Any) -> bool:
177192
return isinstance(obj, type) and is_basemodel_subclass(obj)
178193

@@ -553,7 +568,9 @@ def _convert_messages_to_ollama_messages(
553568
images.append(image_url_components[1])
554569
else:
555570
images.append(image_url_components[0])
556-
571+
elif is_data_content_block(content_part):
572+
image = _get_image_from_data_content_block(content_part)
573+
images.append(image)
557574
else:
558575
raise ValueError(
559576
"Unsupported message content type. "

libs/partners/ollama/tests/integration_tests/chat_models/test_chat_models_standard.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,29 @@ def chat_model_class(self) -> type[ChatOllama]:
1414
def chat_model_params(self) -> dict:
1515
return {"model": "llama3.1"}
1616

17-
@property
18-
def supports_image_inputs(self) -> bool:
19-
return True
20-
2117
@property
2218
def supports_json_mode(self) -> bool:
2319
return True
2420

2521
@property
2622
def has_tool_choice(self) -> bool:
2723
return False
24+
25+
26+
def test_image_model() -> None:
27+
class ImageModelTests(ChatModelIntegrationTests):
28+
@property
29+
def chat_model_class(self) -> type[ChatOllama]:
30+
return ChatOllama
31+
32+
@property
33+
def chat_model_params(self) -> dict:
34+
return {"model": "gemma3:4b"}
35+
36+
@property
37+
def supports_image_inputs(self) -> bool:
38+
return True
39+
40+
test_instance = ImageModelTests()
41+
model = test_instance.chat_model_class(**test_instance.chat_model_params)
42+
ImageModelTests().test_image_inputs(model)

0 commit comments

Comments
 (0)