#1318·eino

[Bug] schema: Message.Format modifies the template media parts and races under concurrent use

Author: vitolee-1018Created Sep 24, 2026Updated Sep 24, 2026

Describe the bug

Message.Format copies MultiContent and UserInputMultiContent with copy(), which is shallow. The media fields are pointers (ImageURL / AudioURL / VideoURL / FileURL on ChatMessagePart, and Image / Audio / Video / File on MessageInputPart). The rendered URL and Base64Data are then written through those shared pointers:

go
copiedMC[i].ImageURL.URL = url
copiedUIMC[i].Image.URL = &url

So formatting modifies the template itself:

  1. The first Format replaces the placeholders in the template, and every later call returns the first call's values.
  2. A prompt template is normally shared by all requests (prompt.FromMessages stores the same *schema.Message and calls Format on it). Concurrent Format calls race, and go test -race reports DATA RACE on schema/message.go:717 and :772.

Content and text parts are strings written on the copied value, so they are not affected. AgenticMessage.Format already clones the pointed-to struct before writing.

To Reproduce

go
msg := &schema.Message{
Role: schema.User,
MultiContent: []schema.ChatMessagePart{
{Type: schema.ChatMessagePartTypeImageURL, ImageURL: &schema.ChatMessageImageURL{URL: "http://img/{id}.png"}},
},
}
_, _ = msg.Format(ctx, map[string]any{"id": "1"}, schema.FString)
b, _ := msg.Format(ctx, map[string]any{"id": "2"}, schema.FString)
fmt.Println(msg.MultiContent[0].ImageURL.URL)      // http://img/1.png
fmt.Println(b[0].MultiContent[0].ImageURL.URL)     // http://img/1.png

UserInputMultiContent (Image.URL and Base64Data) behaves the same way. Audio, video and file parts do too.

Expected behavior

Format leaves the template unchanged, and every call renders from the original placeholders.

Version:

alpha/10 @ 6fc7859.