attachment

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package attachment provides utilities for creating and managing attachments in Braintrust traces.

Attachments allow you to log arbitrary binary data, like images, audio, video, PDFs, and large JSON objects, as part of your traces. This enables multimodal evaluations, handling substantial data structures, and advanced use cases such as summarizing visual content or analyzing document metadata. Attachments are securely stored in an object store and associated with your organization.

Most users won't need to use this package directly, as the instrumentation middleware (trace/contrib/openai, trace/contrib/anthropic) automatically handles attachment conversion. This package is primarily useful for:

  • Manual logging without instrumentation
  • Custom scenarios not covered by auto-instrumentation
  • Writing instrumentation for new providers

For more information, see: https://www.braintrust.dev/docs/instrument/attachments

Example

Example demonstrates how to create an attachment from a file and embed it in a message for multimodal AI requests. The attachment is included alongside text content in the message structure used by OpenAI and Anthropic APIs.

// Create an attachment from a local file
attach, err := FromFile(ImageJPEG, "./photo.jpg")
if err != nil {
	panic(err)
}

// Get the attachment in message format
attachMsg, err := attach.Base64Message()
if err != nil {
	panic(err)
}

// Embed the attachment in a message structure (OpenAI/Anthropic format)
message := map[string]interface{}{
	"role": "user",
	"content": []interface{}{
		map[string]interface{}{
			"type": "text",
			"text": "What's in this image?",
		},
		attachMsg, // Attachment message in correct format
	},
}

// Convert to JSON to log to a span
jsonData, _ := json.Marshal(message)
fmt.Printf("Message with attachment: %s\n", jsonData)

// In practice, you would log this to a span using:
// span.SetAttributes(attribute.String("braintrust.input_json", string(jsonData)))
Example (MultipleAttachments)

Example_multipleAttachments shows how to include multiple attachments in a single message, which is useful for comparing images or providing multiple pieces of visual context.

// Create attachments from different sources
imageAttach, _ := FromFile(ImagePNG, "./chart.png")
pdfAttach, _ := FromFile(PDF, "./report.pdf")

// Get message format for each attachment
imageMsg, _ := imageAttach.Base64Message()
pdfMsg, _ := pdfAttach.Base64Message()

// Build a message with text and multiple attachments
message := map[string]interface{}{
	"role": "user",
	"content": []interface{}{
		map[string]interface{}{
			"type": "text",
			"text": "Compare the data in this chart with the report.",
		},
		imageMsg,
		pdfMsg,
	},
}

_ = message // Log to span in your application

Index

Examples

Constants

View Source
const (
	ImageJPEG = "image/jpeg"
	ImagePNG  = "image/png"
	ImageGIF  = "image/gif"
	ImageWEBP = "image/webp"
	TextPlain = "text/plain"
	PDF       = "application/pdf"
)

Common MIME types for attachments

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	// contains filtered or unexported fields
}

Attachment represents a file or binary data attachment. Attachments are single-use - once consumed, subsequent calls will error.

func FromBytes

func FromBytes(contentType string, data []byte) *Attachment

FromBytes creates an attachment from raw bytes.

func FromFile

func FromFile(contentType string, path string) (*Attachment, error)

FromFile reads a file and creates an attachment. The file is read into memory and closed before returning.

func FromReader

func FromReader(contentType string, r io.Reader) *Attachment

FromReader creates an attachment from an io.Reader. The reader is consumed lazily when Base64URL() or Base64Message() is called.

func FromURL

func FromURL(url string) (*Attachment, error)

FromURL fetches a URL and creates an attachment. Content type is derived from the Content-Type header. Returns an error if the request fails or status is not 200 OK.

func (*Attachment) Base64Message

func (a *Attachment) Base64Message() (map[string]string, error)

Base64Message returns the attachment in message format for AI providers. Returns: {"type": "base64_attachment", "content": "data:..."}

func (*Attachment) Base64URL

func (a *Attachment) Base64URL() (string, error)

Base64URL returns the attachment as a data URL string. Format: "data:image/jpeg;base64,..."

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL