Skip to main content
LangSmith lets you create dataset examples with file attachments—like images, audio files, or documents—and use them in your prompts and evaluators when running evaluations with multimodal content. While you can include multimodal data in your examples by base64 encoding it, this approach is inefficient—the encoded data takes up more space than the original binary files, resulting in slower transfers to and from LangSmith. Using attachments instead provides two key benefits:
  1. Faster upload and download speeds due to more efficient binary file transfers
  2. Enhanced visualization of different file types in the LangSmith UI
This guide covers how to create examples with attachments, build multimodal prompts and evaluators that use those attachments, and run evaluations with multimodal content.

1. Create examples with attachments

To upload examples with attachments using the SDK, use the create_examples / update_examples Python methods or the uploadExamplesMultipart / updateExamplesMultipart TypeScript methods.

Python

Requires langsmith>=0.3.13

TypeScript

Requires version >= 0.2.13You can use the uploadExamplesMultipart method to upload examples with attachments.Note that this is a different method from the standard createExamples method, which currently does not support attachments. Each attachment requires either a Uint8Array or an ArrayBuffer as the data type.
  • Uint8Array: Useful for handling binary data directly.
  • ArrayBuffer: Represents fixed-length binary data, which can be converted to Uint8Array as needed.
Note that you cannot directly pass in a file path in the TypeScript SDK, as accessing local files is not supported in all runtime environments.
Along with being passed in as bytes, attachments can be specified as paths to local files. To do so pass in a path for the attachment data value and specify arg dangerously_allow_filesystem=True:

2. Run evaluations

Define a target function

Now that we have a dataset that includes examples with attachments, we can define a target function to run over these examples. The following example simply uses OpenAI’s GPT-4o model to answer questions about an image and an audio clip.

Python

The target function you are evaluating must have two positional arguments in order to consume the attachments associated with the example, the first must be called inputs and the second must be called attachments.
  • The inputs argument is a dictionary that contains the input data for the example, excluding the attachments.
  • The attachments argument is a dictionary that maps the attachment name to a dictionary containing a presigned url, mime_type, and a reader of the bytes content of the file. You can use either the presigned url or the reader to get the file contents. Each value in the attachments dictionary is a dictionary with the following structure:

TypeScript

In the TypeScript SDK, the config argument is used to pass in the attachments to the target function if includeAttachments is set to true.The config will contain attachments which is an object mapping the attachment name to an object of the form:

Define custom evaluators

The exact same rules apply as above to determine whether the evaluator should receive attachments.The evaluator below uses an LLM to judge if the reasoning and the answer are consistent. To learn more about how to define llm-based evaluators, please see this guide.

3. Update examples with attachments

In the code above, we showed how to add examples with attachments to a dataset. It is also possible to update these same examples using the SDK.As with existing examples, datasets are versioned when you update them with attachments. Therefore, you can navigate to the dataset version history to see the changes made to each example. To learn more, please see this guide.When updating an example with attachments, you can update attachments in a few different ways:
  • Pass in new attachments
  • Rename existing attachments
  • Delete existing attachments
Note that:
  • Any existing attachments that are not explicitly renamed or retained will be deleted.
  • An error will be raised if you pass in a non-existent attachment name to retain or rename.
  • New attachments take precedence over existing attachments in case the same attachment name appears in the attachments and attachment_operations fields.