@@ -346,7 +346,7 @@ def _convert_messages_to_ollama_messages(
346
346
) -> Sequence [Message ]:
347
347
ollama_messages : List = []
348
348
for message in messages :
349
- role = ""
349
+ role : Literal [ "user" , "assistant" , "system" , "tool" ]
350
350
tool_call_id : Optional [str ] = None
351
351
tool_calls : Optional [List [Dict [str , Any ]]] = None
352
352
if isinstance (message , HumanMessage ):
@@ -383,11 +383,13 @@ def _convert_messages_to_ollama_messages(
383
383
image_url = None
384
384
temp_image_url = content_part .get ("image_url" )
385
385
if isinstance (temp_image_url , str ):
386
- image_url = content_part [ "image_url" ]
386
+ image_url = temp_image_url
387
387
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 )
389
391
):
390
- image_url = temp_image_url
392
+ image_url = temp_image_url [ "url" ]
391
393
else :
392
394
raise ValueError (
393
395
"Only string image_url or dict with string 'url' "
@@ -408,15 +410,16 @@ def _convert_messages_to_ollama_messages(
408
410
"Must either have type 'text' or type 'image_url' "
409
411
"with a string 'image_url' field."
410
412
)
411
- msg = {
413
+ # Should convert to ollama.Message once role includes tool, and tool_call_id is in Message # noqa: E501
414
+ msg : dict = {
412
415
"role" : role ,
413
416
"content" : content ,
414
417
"images" : images ,
415
418
}
419
+ if tool_calls :
420
+ msg ["tool_calls" ] = tool_calls # type: ignore
416
421
if tool_call_id :
417
422
msg ["tool_call_id" ] = tool_call_id
418
- if tool_calls :
419
- msg ["tool_calls" ] = tool_calls
420
423
ollama_messages .append (msg )
421
424
422
425
return ollama_messages
0 commit comments