[Bug] schema: Message.Format modifies the template media parts and races under concurrent use
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:
copiedMC[i].ImageURL.URL = url
copiedUIMC[i].Image.URL = &urlSo formatting modifies the template itself:
- The first
Formatreplaces the placeholders in the template, and every later call returns the first call's values. - A prompt template is normally shared by all requests (
prompt.FromMessagesstores the same*schema.Messageand callsFormaton it). ConcurrentFormatcalls race, andgo test -racereportsDATA RACEonschema/message.go:717and: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
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.pngUserInputMultiContent (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.
Source: cloudwego/eino