Skip to content

Messages, parts and the transcript

View as Markdown

A weft.Message is one turn of a conversation: a weft.Role plus an ordered list of typed parts. A transcript is a []weft.Message; it round-trips through encoding/json without loss, and weft.Repair turns any transcript into valid model input.

One support turn, written by hand in concepts/messages.go, in the same shape the loop produces:

concepts/messages.go
transcript := []weft.Message{
weft.User("Where is order 1234?"),
{Role: weft.RoleAssistant, Content: []weft.Part{
weft.TextPart{Text: "Let me check."},
weft.ToolCallPart{ID: "call_1", Name: "lookup_order", Args: json.RawMessage(`{"order_id":"1234"}`)},
}},
{Role: weft.RoleTool, Content: []weft.Part{ // one tool message per step, a result per call
weft.ToolResultPart{CallID: "call_1", Name: "lookup_order", Content: "order 1234: shipped"},
}},
weft.Assistant("Order 1234 has shipped."),
}
for _, m := range transcript {
fmt.Fprintf(w, "%-9s %d part(s), text %q\n", m.Role, len(m.Content), m.Text())
}

concepts/messages.go on GitHub

user 1 part(s), text "Where is order 1234?"
assistant 2 part(s), text "Let me check."
tool 1 part(s), text ""
assistant 1 part(s), text "Order 1234 has shipped."

The loop appends to the transcript as a run goes: each step adds one assistant message (its reasoning, text and tool calls) and, when the step called tools, one tool message holding a result for every call that ran. Two exceptions: a step that produced no text, reasoning or calls adds no assistant message, and a call parked at the approval boundary has no result until the run that decides it, so a step whose every call parked adds no tool message. RunResult.Messages is that transcript, the run’s input included. Pass it back with weft.Messages(res.Messages...) and the next run continues where this one ended.

There are three roles:

RoleConstantCarries
userweft.RoleUserThe person’s input: text, and files.
assistantweft.RoleAssistantThe model’s output: reasoning, text and tool calls.
toolweft.RoleToolThe results of one step’s tool calls, one result per call.

There is no system role. The system instruction is agent configuration (weft.Instructions) and travels on ModelRequest.System, so a transcript never carries it and adapters never merge it in.

The role of a message you pass in is not checked. That covers a message given to weft.Messages and a transcript decoded from JSON: Message.UnmarshalJSON rejects an unknown part type but takes any role string, the loop does not look, and weft.Repair passes such a message through unchanged. Steering is the exception: a steered message that is not user fails with ErrInvalidSteer. Each adapter converts only user, assistant and tool, so a message with any other role, such as a "system" message imported from an OpenAI-format transcript, stays in res.Messages and is never sent to the provider. The run gets no error and no log line.

A weft.Part is a sealed interface: the set of part types is closed, and only weft adds to it. The three every tool-using conversation has:

  • weft.TextPart: Text, a span of user or assistant text.
  • weft.ToolCallPart: a call the model requested. ID is the provider’s call id, unique within its step, and Name is the tool. Args is the raw JSON the model produced, decoded into the tool’s input type only when the call runs. Signature is a provider’s opaque token on the call itself (Gemini’s thought signatures), empty for providers without one.
  • weft.ToolResultPart: the outcome of one call. CallID matches the call’s ID; Name is the tool; Content is the result text (the output, or the failure message); IsError marks a failure. A tool failure is a result the model reads, never a run error.

Two more part types exist: weft.ReasoningPart, a provider reasoning block weft keeps and sends back with its signature but never acts on; and weft.FilePart, an image, PDF or audio file in a user message (Send images and files to a model).

The constructors cover the common shapes:

Message has two fields, Role and Content. m.Text() returns the concatenation of its text parts, so a tool message’s text is empty.

On the wire every part carries a "type" discriminator (text, tool_call, tool_result, reasoning, file). Encoding is plain json.Marshal; decoding goes through Message.UnmarshalJSON, which restores each part as its own Go type and rejects a part type it does not know:

concepts/messages.go
b, err := json.Marshal(transcript[1]) // the assistant message with the tool call
if err != nil {
return err
}
fmt.Fprintln(w, string(b))
var back []weft.Message
all, _ := json.Marshal(transcript)
if err := json.Unmarshal(all, &back); err != nil {
return err
}
call := back[1].Content[1].(weft.ToolCallPart) // parts come back as their own types
fmt.Fprintln(w, "decoded:", call.Name, string(call.Args))
var m weft.Message
err = json.Unmarshal([]byte(`{"role":"user","content":[{"type":"sticker"}]}`), &m)
fmt.Fprintln(w, "unknown part:", err)

concepts/messages.go on GitHub

{"role":"assistant","content":[{"type":"text","text":"Let me check."},{"type":"tool_call","id":"call_1","name":"lookup_order","args":{"order_id":"1234"}}]}
decoded: lookup_order {"order_id":"1234"}
unknown part: weft: message content[0]: unknown part type "sticker"

An unknown type is an error, never a dropped part: silently losing content would corrupt the transcript. The wire format is versioned by weft.SchemaVersion, currently 1; within a version, fields change only by addition.

A valid transcript pairs every tool call with exactly one result. weft.Repair(msgs) restores that from anything: a transcript cut off mid-step, one assembled by hand, one loaded from storage. Every run’s input goes through it before the first model call, so you only call it yourself to persist or inspect a transcript.

What it changes:

  • A tool call with no result gets a visible error result: no result recorded: the call was interrupted.
  • A result whose CallID names no call of the assistant message before it is dropped, as is a second result for the same call.
  • Only the first tool message after an assistant message is kept; a later one is non-canonical and dropped whole.
  • A tool message that does not follow an assistant message with calls is dropped, and so is any part in a tool message that is not a ToolResultPart.
  • Everything outside tool messages is untouched.

Here the only result answers a call nobody made, so it is dropped, and the real call gets a synthesized result:

concepts/messages.go
interrupted := []weft.Message{
weft.User("Where is order 1234?"),
{Role: weft.RoleAssistant, Content: []weft.Part{
weft.ToolCallPart{ID: "call_1", Name: "lookup_order", Args: json.RawMessage(`{"order_id":"1234"}`)},
}},
{Role: weft.RoleTool, Content: []weft.Part{ // answers a call no message made
weft.ToolResultPart{CallID: "call_9", Name: "lookup_order", Content: "order 9: shipped"},
}},
}
for _, m := range weft.Repair(interrupted) {
for _, p := range m.Content {
if r, ok := p.(weft.ToolResultPart); ok {
fmt.Fprintf(w, "%s %s (error: %v): %s\n", m.Role, r.CallID, r.IsError, r.Content)
}
}
}

concepts/messages.go on GitHub

tool call_1 (error: true): no result recorded: the call was interrupted

Repair is pure, never changing its input, and idempotent: Repair(Repair(m)) equals Repair(m). A dropped part leaves no marker; a synthesized one is always visible to the model. The package example ExampleRepair shows the same repair on a transcript that stops after the call.

Studio marks a synthesized result where it can see one: on the run page’s Request pane, a message line holding that text is prefixed [synthesised by Repair]. The record carries no other sign, so Studio matches the text (Badges and holes).

concepts/messages.go builds a transcript, round-trips it through JSON and repairs a broken one:

concepts/messages.go
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/weftgo/weft"
)
// buildTranscript writes one turn of a support conversation by hand: the
// shape the loop itself produces.
func buildTranscript(w io.Writer) []weft.Message {
transcript := []weft.Message{
weft.User("Where is order 1234?"),
{Role: weft.RoleAssistant, Content: []weft.Part{
weft.TextPart{Text: "Let me check."},
weft.ToolCallPart{ID: "call_1", Name: "lookup_order", Args: json.RawMessage(`{"order_id":"1234"}`)},
}},
{Role: weft.RoleTool, Content: []weft.Part{ // one tool message per step, a result per call
weft.ToolResultPart{CallID: "call_1", Name: "lookup_order", Content: "order 1234: shipped"},
}},
weft.Assistant("Order 1234 has shipped."),
}
for _, m := range transcript {
fmt.Fprintf(w, "%-9s %d part(s), text %q\n", m.Role, len(m.Content), m.Text())
}
return transcript
}
// roundTrip encodes the transcript as JSON and decodes it back.
func roundTrip(w io.Writer, transcript []weft.Message) error {
b, err := json.Marshal(transcript[1]) // the assistant message with the tool call
if err != nil {
return err
}
fmt.Fprintln(w, string(b))
var back []weft.Message
all, _ := json.Marshal(transcript)
if err := json.Unmarshal(all, &back); err != nil {
return err
}
call := back[1].Content[1].(weft.ToolCallPart) // parts come back as their own types
fmt.Fprintln(w, "decoded:", call.Name, string(call.Args))
var m weft.Message
err = json.Unmarshal([]byte(`{"role":"user","content":[{"type":"sticker"}]}`), &m)
fmt.Fprintln(w, "unknown part:", err)
return nil
}
// repairTranscript repairs a transcript cut off mid-step.
func repairTranscript(w io.Writer) {
interrupted := []weft.Message{
weft.User("Where is order 1234?"),
{Role: weft.RoleAssistant, Content: []weft.Part{
weft.ToolCallPart{ID: "call_1", Name: "lookup_order", Args: json.RawMessage(`{"order_id":"1234"}`)},
}},
{Role: weft.RoleTool, Content: []weft.Part{ // answers a call no message made
weft.ToolResultPart{CallID: "call_9", Name: "lookup_order", Content: "order 9: shipped"},
}},
}
for _, m := range weft.Repair(interrupted) {
for _, p := range m.Content {
if r, ok := p.(weft.ToolResultPart); ok {
fmt.Fprintf(w, "%s %s (error: %v): %s\n", m.Role, r.CallID, r.IsError, r.Content)
}
}
}
}
// demoMessages builds a transcript, round-trips it through JSON and
// repairs a broken one.
func demoMessages(ctx context.Context, w io.Writer) error {
transcript := buildTranscript(w)
if err := roundTrip(w, transcript); err != nil {
return err
}
repairTranscript(w)
return nil
}

View concepts/messages.go on GitHub

Run it from docs-site/examples:

Terminal window
go run ./concepts messages
go test ./concepts
user 1 part(s), text "Where is order 1234?"
assistant 2 part(s), text "Let me check."
tool 1 part(s), text ""
assistant 1 part(s), text "Order 1234 has shipped."
{"role":"assistant","content":[{"type":"text","text":"Let me check."},{"type":"tool_call","id":"call_1","name":"lookup_order","args":{"order_id":"1234"}}]}
decoded: lookup_order {"order_id":"1234"}
unknown part: weft: message content[0]: unknown part type "sticker"
tool call_1 (error: true): no result recorded: the call was interrupted
  • Store transcripts as JSON of []weft.Message. The encoding is lossless, signatures included, and versioned; rebuilding messages from events loses signatures and block boundaries. (Source: weft/core/message.go:30-41, weft/core/run.go:551-574)
  • Put every result of one step on one tool message. Repair keeps only the first tool message after an assistant message. (Source: weft/core/repair.go:21-36)
  • Branch on part types with a type switch, and keep a default case. The set is closed, so a switch over TextPart, ToolCallPart, ToolResultPart, ReasoningPart and FilePart is complete for v0.20.2; weft plans to add part types (approval parts), and only weft can. (Source: weft/core/message.go:95-100)
  • Keep the system prompt out of the transcript. There is no system role; use weft.Instructions. (Source: ADR 0001, AGENTS.md rule 2)
  • Move a foreign system message into weft.Instructions before the run. A transcript imported from another framework may hold {"role":"system",…}. It decodes, but all three adapters drop it from the request without an error, so the model never sees that text. Take its text out of the slice and pass it with weft.Instructions. (Source: weft/core/message.go:77-83, weft/openai/convert.go:34-73, weft/anthropic/convert.go:118-145, weft/google/convert.go:20-50)

Symbols used: weft.Message · weft.Role · weft.Part · weft.TextPart · weft.ToolCallPart · weft.ToolResultPart · weft.ReasoningPart · weft.FilePart · weft.User · weft.UserParts · weft.Assistant · weft.Repair · weft.SchemaVersion · weft.Messages