@@ -88,7 +88,7 @@ func (p *LlamaCppProvider) SendMessage(messages []Message, stream bool, images [
8888 }
8989
9090 request := map [string ]interface {}{
91- "messages" : buildRequestMessages (messages , images ),
91+ "messages" : mergeImagesIntoLastUser (messages , images ),
9292 }
9393 if effectiveModel != "" {
9494 request ["model" ] = effectiveModel
@@ -355,3 +355,51 @@ func parseLlamaCppModels(body []byte) []Model {
355355 }}
356356}
357357
358+ // mergeImagesIntoLastUser returns a JSON-ready messages slice that matches
359+ // the standard OpenAI vision format: image blocks live inside the same user
360+ // turn as the prompt text. The last user message's content is replaced with
361+ // a combined [text, image_url...] block array; if no user message exists yet
362+ // a new one is appended. Shared by all OpenAI-compatible providers (OpenAI,
363+ // llama.cpp, ...) that accept structured content blocks for images.
364+ func mergeImagesIntoLastUser (messages []Message , images []string ) []interface {} {
365+ out := make ([]interface {}, 0 , len (messages )+ 1 )
366+ for _ , m := range messages {
367+ out = append (out , m )
368+ }
369+ if len (images ) == 0 {
370+ return out
371+ }
372+
373+ blocks := make ([]ContentBlock , 0 , len (images )+ 1 )
374+ for _ , uri := range images {
375+ blocks = append (blocks , ContentBlock {
376+ Type : "image_url" ,
377+ ImageURL : & struct {
378+ URL string `json:"url"`
379+ }{URL : uri },
380+ })
381+ }
382+
383+ for i := len (out ) - 1 ; i >= 0 ; i -- {
384+ msg , ok := out [i ].(Message )
385+ if ! ok || msg .Role != "user" {
386+ continue
387+ }
388+ combined := blocks
389+ if strings .TrimSpace (msg .Content ) != "" {
390+ combined = append ([]ContentBlock {{Type : "text" , Text : msg .Content }}, blocks ... )
391+ }
392+ out [i ] = map [string ]interface {}{
393+ "role" : "user" ,
394+ "content" : combined ,
395+ }
396+ return out
397+ }
398+
399+ out = append (out , map [string ]interface {}{
400+ "role" : "user" ,
401+ "content" : blocks ,
402+ })
403+ return out
404+ }
405+
0 commit comments