Skip to documentation
Documentation navigation

Documentation navigation

Documentation / guides

Construct messages

Construct message values with explicit roles and ordered blocks.

developer

There are no message constructors in core/content. Construction is deliberately visible: embed a Message and set the matching Role constant, then add non-nil block pointers in order.

API surface

type Role string

const (
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleSystem    Role = "system"
	RoleTool      Role = "tool"
)
package main

import "github.com/looprig/core/content"

func main() {
	user := &content.UserMessage{Message: content.Message{
		Role: content.RoleUser,
		Blocks: []content.Block{
			&content.TextBlock{Text: "What changed?"},
			&content.ImageBlock{
				MediaType: content.MediaTypeImagePNG,
				Source: content.ImageSource{URL: "https://example.test/diff.png"},
			},
		},
	}}
	_ = user
}

Embedded fields are promoted, so user.Role and user.Blocks work after construction. A nil block interface or typed-nil payload is not a valid value for JSON encoding; MarshalBlock fails closed instead of emitting a misleading empty block.

Role and type must agree

The message codecs preserve the Role field exactly; they do not infer it from the Go type. Set RoleAssistant on AIMessage, RoleTool on ToolResultMessage, and so on. Tests exercise mixed messages and empty or nil block slices.

Proof

Related: UserMessage, Content blocks.

← back to documentation