Send images and files to a model
A file goes into a prompt as a part. weft.UserParts builds a user message from parts, and a weft.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.
An image beside a question, in 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))weft.Prompt takes text only, so a message with parts goes in through weft.Messages.
How it works
Section titled “How it works”A message is a role plus typed parts (rule 2). 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:
{"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.
Send an image
Section titled “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 prints a message with a URL part.
What each first-party adapter carries:
| Adapter | Carries | Sent as |
|---|---|---|
openai | images only (image/*) | an image_url part: the URL, or a data: URL for inline bytes |
anthropic | image/png, image/jpeg, image/gif, image/webp, application/pdf | an image or document block, base64 or URL |
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
Section titled “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:
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)}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: 0The 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). 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
Section titled “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:
// 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) }}View on GitHub · run it from docs-site/examples:
go run ./multimodalgo test ./multimodal== imagemodel received text: "What is the total on this receipt?"model received file: image/png, 11 bytesanswer: The receipt is for $42.10.== unsupportedunsupported: 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: 0Best practices & gotchas
Section titled “Best practices & gotchas”- Set exactly one of
DataandURL. The adapter refuses a part with both or neither, wrappingErrUnsupported. (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. Undermw.FallbackWhenthe 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
Section titled “See also”- Messages, parts and the transcript
- Connect a model provider
- Retry and fall back when a provider fails
Symbols used: weft.UserParts · weft.FilePart · weft.TextPart · weft.ErrUnsupported · weft.Messages · weft.Prompt · weft.RunError · weft.New · openai.Model · openai.Client · mw.FallbackWhen · wefttest.Script