---
title: Messages, parts and the transcript
description: A weft.Message is a role plus typed parts; a transcript round-trips through JSON losslessly, and weft.Repair makes any transcript valid model input.
url: https://weftgo.dev/docs/concepts/messages/
type: concept
sources:
  - weft/docs/adr/0001-message-model.md:13-41 (roles, parts, no system role), 128-162 (transcript invariants and Repair)
  - weft/AGENTS.md rule 2
  - weft/core/message.go:8-28 (SchemaVersion, Role), 30-93 (Message, User, UserParts, Assistant, Text, UnmarshalJSON), 95-147 (Part, wire discriminators), 149-204 (TextPart, ToolCallPart, ToolResultPart, ReasoningPart, FilePart)
  - weft/core/repair.go:5-117 (Repair)
  - 'weft/core/message.go:77-83 (UnmarshalJSON keeps any role: only part types are checked)'
  - 'weft/core/loop.go:819-824 (a steered message must be RoleUser: ErrInvalidSteer); weft/thread/steer.go:149-153, 187-190 (the same for s.Steer and a Steer busy policy)'
  - 'weft/core/repair.go:110-112 (Repair''s default case: a message of any other role passes through)'
  - weft/openai/convert.go:34-73, weft/anthropic/convert.go:118-145, weft/google/convert.go:20-50 (each role switch converts user, assistant and tool only; no default case)
  - weft/studio/web/src/lib/error-kind.ts:243-259 (REPAIRED_RESULT, synthesised by Repair, matched on the text)
  - weft/core/loop.go:233-241 (input repaired before the first model call), 706-728 (one assistant message per step), 757-767 (one tool message per step)
  - weft/example_test.go ExampleRepair, ExampleUserParts
  - docs-site/examples/concepts/messages.go, concepts_test.go TestDemos/messages
---

# Messages, parts and the transcript

A [`weft.Message`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Message) is one turn of a conversation: a [`weft.Role`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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:

```go title="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](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/concepts/messages.go)

```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

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

There are three roles:

| Role | Constant | Carries |
|---|---|---|
| `user` | [`weft.RoleUser`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#RoleUser) | The person's input: text, and files. |
| `assistant` | [`weft.RoleAssistant`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#RoleAssistant) | The model's output: reasoning, text and tool calls. |
| `tool` | [`weft.RoleTool`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#TextPart): `Text`, a span of user or assistant text.
- [`weft.ToolCallPart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ReasoningPart), a provider reasoning block weft keeps and sends back with its signature but never acts on; and [`weft.FilePart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#FilePart), an image, PDF or audio file in a user message ([Send images and files to a model](https://weftgo.dev/docs/guides/multimodal/)).

## Build messages

The constructors cover the common shapes:

- [`weft.User(text)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#User) and [`weft.Assistant(text)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Assistant) return a message with one `TextPart`.
- [`weft.UserParts(parts...)`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#UserParts) 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()`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Message.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`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Message.UnmarshalJSON), which restores each part as its own Go type and rejects a part type it does not know:

```go title="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](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/concepts/messages.go)

```text
{"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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#SchemaVersion), currently 1; within a version, fields change only by addition.

## 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 `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:

```go title="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](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/concepts/messages.go)

```text
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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#example-Repair) 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](https://weftgo.dev/docs/reference/badges/#other-words)).

## Complete example

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

```go title="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
}
```

[concepts/messages.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/concepts/messages.go)

Run it from `docs-site/examples`:

```sh
go run ./concepts messages
go test ./concepts
```

```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."
{"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
```

## 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.** `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`)

## See also

- [Continue a conversation](https://weftgo.dev/docs/guides/conversations/)
- [Send images and files to a model](https://weftgo.dev/docs/guides/multimodal/)
- [What does a run return?](https://weftgo.dev/docs/concepts/runs/)
- [What is a session?](https://weftgo.dev/docs/sessions/overview/)

Symbols used: [`weft.Message`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Message) · [`weft.Role`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Role) · [`weft.Part`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Part) · [`weft.TextPart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#TextPart) · [`weft.ToolCallPart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ToolCallPart) · [`weft.ToolResultPart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ToolResultPart) · [`weft.ReasoningPart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ReasoningPart) · [`weft.FilePart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#FilePart) · [`weft.User`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#User) · [`weft.UserParts`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#UserParts) · [`weft.Assistant`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Assistant) · [`weft.Repair`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Repair) · [`weft.SchemaVersion`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#SchemaVersion) · [`weft.Messages`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Messages)
