---
title: Send images and files to a model
description: Build a prompt that mixes text and files with weft.UserParts and weft.FilePart; an adapter that cannot carry a file fails the model call with an error wrapping weft.ErrUnsupported.
url: https://weftgo.dev/docs/guides/multimodal/
type: how-to
sources:
  - weft/core/message.go:48-54 (UserParts), 192-203 (FilePart)
  - weft/core/errors.go:64-69 (ErrUnsupported)
  - 'weft/core/mw/model.go:118-125 (an exhausted fallback chain: the run error is the last model''s)'
  - weft/internal/adapterkit/adapterkit.go:101-106 (exactly one of Data or URL)
  - weft/openai/convert.go:157-200 (images only)
  - weft/anthropic/convert.go:221-256 (images and PDF)
  - weft/google/convert.go:222-256 (inline data or file URI)
  - weft/docs/adr/0013-adapter-contract.md:155-161 (capability matrix), 165, 203-204
  - weft/AGENTS.md:668-676 (rule 2)
  - weft/example_test.go:83 (ExampleUserParts)
  - docs-site/examples/multimodal/main.go, main_test.go TestMultimodal, TestFilePartWire
---

# Send images and files to a model

A file goes into a prompt as a part. [`weft.UserParts`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#UserParts) builds a user message from parts, and a [`weft.FilePart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#FilePart) carries an image, a PDF or audio, inline or by URL. The adapter maps it to the provider's image or document block; one that cannot carry the file fails the model call with an error wrapping [`weft.ErrUnsupported`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ErrUnsupported).

An image beside a question, in `multimodal/main.go`:

```go title="multimodal/main.go"
msg := weft.UserParts(
	weft.TextPart{Text: "What is the total on this receipt?"},
	weft.FilePart{MediaType: "image/png", Data: receiptPNG}, // or URL: "https://…"
)
res, err := agt.Generate(ctx, weft.Messages(msg))
```

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

`weft.Prompt` takes text only, so a message with parts goes in through [`weft.Messages`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Messages).

## How it works

A message is a role plus typed parts ([rule 2](https://github.com/weftgo/weft/blob/v0.20.2/AGENTS.md#the-rules-do-not-break-these-tests-pin-them)). `UserParts` copies the parts you give it into a `user` message; your slice is not retained. The core never reads a file's bytes: it carries the part to the model, and the adapter decides how to send it.

On the wire the part has the type `file`, and inline bytes are base64-encoded, as `[]byte` is in `encoding/json`:

```json
{"role":"user","content":[{"type":"text","text":"hi"},{"type":"file","media_type":"image/png","data":"YWJj"}]}
```

The transcript keeps the part, so a run you continue with `weft.Messages(res.Messages...)` sends the file again on every model call. See [Messages, parts and the transcript](https://weftgo.dev/docs/concepts/messages/).

## Send an image

A `FilePart` has three fields. Set `MediaType` and exactly one of the other two:

| Field | Type | Holds |
|---|---|---|
| `MediaType` | `string` | The file's MIME type: `image/png`, `application/pdf`, … |
| `Data` | `[]byte` | The file inline. |
| `URL` | `string` | A URL the provider fetches, or Gemini's file URI. |

The exactly-one rule is checked by the adapter, not the core: a part with both or neither set fails the model call with `ErrUnsupported`. The package example [`ExampleUserParts`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#example-UserParts) prints a message with a URL part.

What each first-party adapter carries:

| Adapter | Carries | Sent as |
|---|---|---|
| [`openai`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/openai) | images only (`image/*`) | an `image_url` part: the URL, or a `data:` URL for inline bytes |
| [`anthropic`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/anthropic) | `image/png`, `image/jpeg`, `image/gif`, `image/webp`, `application/pdf` | an image or document block, base64 or URL |
| [`google`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/google) | every media type: the adapter does not filter, and Gemini carries images, audio and video natively | inline data, or file data for a URL |

## Handle a file the adapter cannot carry

An adapter refuses a part it cannot send before any request leaves the process. The run fails at that step with a `*RunError` that matches `weft.ErrUnsupported`:

```go title="multimodal/main.go"
msg := weft.UserParts(
	weft.TextPart{Text: "Summarise this contract."},
	weft.FilePart{MediaType: "application/pdf", URL: "https://example.com/contract.pdf"},
)
_, err := agt.Generate(ctx, weft.Messages(msg))
if errors.Is(err, weft.ErrUnsupported) {
	fmt.Fprintln(w, "unsupported:", err)
}
```

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

```text
unsupported: weft: run failed at step 0: model stream: weft: request uses a feature the model does not support: chat completions accept image files, got "application/pdf"
requests sent: 0
```

The OpenAI adapter speaks Chat Completions, which accepts images only, so the PDF fails there; the Anthropic adapter would send it as a document block. Because the failure is a run error, you can match it with `errors.Is` and send the request to another model, for example with `mw.FallbackWhen` ([Retry and fall back when a provider fails](https://weftgo.dev/docs/guides/retries-and-fallback/)). Once a backup has been tried, the run error is the backup's: if it fails too, `errors.Is(err, weft.ErrUnsupported)` is false.

## Complete example

`multimodal/main.go` sends an image to a scripted model and a PDF to the OpenAI adapter, pointed at a local test server that counts requests:

```go title="multimodal/main.go"
// Command multimodal runs the examples of the guide "Send images and
// files to a model": go run ./multimodal. The model is scripted with
// wefttest, and the adapter demo talks to a local test server, so it runs
// offline.
package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"sync/atomic"

	openaisdk "github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
	"github.com/weftgo/weft"
	"github.com/weftgo/weft/openai"
	"github.com/weftgo/weft/wefttest"
)

// receiptPNG stands in for an image read from disk or an upload.
var receiptPNG = []byte("\x89PNG\r\n\x1a\n…")

// sendImage puts an image beside a question and shows what the model got.
func sendImage(ctx context.Context, w io.Writer) error {
	scripted := wefttest.Script(wefttest.Say("The receipt is for $42.10."))
	agt := weft.New(scripted)
	msg := weft.UserParts(
		weft.TextPart{Text: "What is the total on this receipt?"},
		weft.FilePart{MediaType: "image/png", Data: receiptPNG}, // or URL: "https://…"
	)
	res, err := agt.Generate(ctx, weft.Messages(msg))
	if err != nil {
		return err
	}
	for _, p := range scripted.LastRequest().Messages[0].Content {
		switch p := p.(type) {
		case weft.TextPart:
			fmt.Fprintf(w, "model received text: %q\n", p.Text)
		case weft.FilePart:
			fmt.Fprintf(w, "model received file: %s, %d bytes\n", p.MediaType, len(p.Data))
		}
	}
	fmt.Fprintln(w, "answer:", res.Text())
	return nil
}

// unsupported sends a PDF through the OpenAI adapter, whose Chat
// Completions API takes images only. The test server counts requests.
func unsupported(ctx context.Context, w io.Writer) error {
	var hits atomic.Int32
	srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
		hits.Add(1)
		http.Error(rw, "not reached", http.StatusTeapot)
	}))
	defer srv.Close()
	client := openaisdk.NewClient(option.WithBaseURL(srv.URL), option.WithAPIKey("test"))
	model := openai.Model("gpt-5-mini", openai.Client(&client))
	agt := weft.New(model)

	msg := weft.UserParts(
		weft.TextPart{Text: "Summarise this contract."},
		weft.FilePart{MediaType: "application/pdf", URL: "https://example.com/contract.pdf"},
	)
	_, err := agt.Generate(ctx, weft.Messages(msg))
	if errors.Is(err, weft.ErrUnsupported) {
		fmt.Fprintln(w, "unsupported:", err)
	}
	fmt.Fprintln(w, "requests sent:", hits.Load())
	return nil
}

func run(ctx context.Context, w io.Writer) error {
	fmt.Fprintln(w, "== image")
	if err := sendImage(ctx, w); err != nil {
		return err
	}
	fmt.Fprintln(w, "== unsupported")
	return unsupported(ctx, w)
}

func main() {
	if err := run(context.Background(), os.Stdout); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
```

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

[View on GitHub](https://github.com/weftgo/weftgo.dev/tree/main/docs-site/examples/multimodal) · run it from `docs-site/examples`:

```sh
go run ./multimodal
go test ./multimodal
```

```text
== image
model received text: "What is the total on this receipt?"
model received file: image/png, 11 bytes
answer: The receipt is for $42.10.
== unsupported
unsupported: weft: run failed at step 0: model stream: weft: request uses a feature the model does not support: chat completions accept image files, got "application/pdf"
requests sent: 0
```

## Best practices & gotchas

- **Set exactly one of `Data` and `URL`.** The adapter refuses a part with both or neither, wrapping `ErrUnsupported`. (Source: `weft/core/message.go:192-199`, `weft/internal/adapterkit/adapterkit.go:101-106`)
- **Check `errors.Is(err, weft.ErrUnsupported)` to fall back.** The refusal is a run error, raised before the request is sent. Under `mw.FallbackWhen` the check belongs in the predicate: when the backup fails too, the run error is the backup's alone. (Source: `weft/core/errors.go:64-69`, `weft/core/mw/model.go:118-125`)
- **Send PDFs to the Anthropic or Google adapter.** The OpenAI adapter's Chat Completions API takes images only. (Source: `weft/openai/convert.go:157-160`, ADR 0013 Appendix A)
- **Mind the transcript's size.** A continued conversation carries every earlier file part on every model call. (Source: rule 2; `weft/core/message.go:48-54`)

## See also

- [Messages, parts and the transcript](https://weftgo.dev/docs/concepts/messages/)
- [Connect a model provider](https://weftgo.dev/docs/guides/providers/)
- [Retry and fall back when a provider fails](https://weftgo.dev/docs/guides/retries-and-fallback/)

Symbols used: [`weft.UserParts`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#UserParts) · [`weft.FilePart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#FilePart) · [`weft.TextPart`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#TextPart) · [`weft.ErrUnsupported`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ErrUnsupported) · [`weft.Messages`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Messages) · [`weft.Prompt`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Prompt) · [`weft.RunError`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#RunError) · [`weft.New`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#New) · [`openai.Model`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/openai#Model) · [`openai.Client`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/openai#Client) · [`mw.FallbackWhen`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/mw#FallbackWhen) · [`wefttest.Script`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/wefttest#Script)
