Messages, parts and the transcript
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:
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())}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."How it works
Section titled “How it works”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.
Roles and parts
Section titled “Roles and parts”There are three roles:
| Role | Constant | Carries |
|---|---|---|
user | weft.RoleUser | The person’s input: text, and files. |
assistant | weft.RoleAssistant | The model’s output: reasoning, text and tool calls. |
tool | weft.RoleTool | The 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.IDis the provider’s call id, unique within its step, andNameis the tool.Argsis the raw JSON the model produced, decoded into the tool’s input type only when the call runs.Signatureis 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.CallIDmatches the call’sID;Nameis the tool;Contentis the result text (the output, or the failure message);IsErrormarks 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).
Build messages
Section titled “Build messages”The constructors cover the common shapes:
weft.User(text)andweft.Assistant(text)return a message with oneTextPart.weft.UserParts(parts...)returns a user message with several parts, for text beside files.- Any other shape is a literal:
weft.Message{Role: …, Content: []weft.Part{…}}, as the assistant and tool messages above.
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:
b, err := json.Marshal(transcript[1]) // the assistant message with the tool callif err != nil { return err}fmt.Fprintln(w, string(b))
var back []weft.Messageall, _ := 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 typesfmt.Fprintln(w, "decoded:", call.Name, string(call.Args))
var m weft.Messageerr = json.Unmarshal([]byte(`{"role":"user","content":[{"type":"sticker"}]}`), &m)fmt.Fprintln(w, "unknown part:", err){"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.
Repair
Section titled “Repair”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
CallIDnames 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:
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) } }}tool call_1 (error: true): no result recorded: the call was interruptedRepair 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).
Complete example
Section titled “Complete example”concepts/messages.go builds a transcript, round-trips it through JSON and repairs a broken one:
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}Run it from docs-site/examples:
go run ./concepts messagesgo test ./conceptsuser 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 interruptedBest practices & gotchas
Section titled “Best practices & gotchas”- 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.
Repairkeeps 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,ReasoningPartandFilePartis 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.Instructionsbefore 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 withweft.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)
See also
Section titled “See also”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