Skip to main content
The langsmith.schemas module contains Pydantic models and type definitions for LangSmith data structures.

Core types

Run

Represents a single execution trace (span).
from langsmith.schemas import Run

# Accessing run properties
def my_evaluator(run: Run, example):
    print(f"Run ID: {run.id}")
    print(f"Name: {run.name}")
    print(f"Type: {run.run_type}")
    print(f"Inputs: {run.inputs}")
    print(f"Outputs: {run.outputs}")
    print(f"Error: {run.error}")
    print(f"Start: {run.start_time}")
    print(f"End: {run.end_time}")
id
UUID
Unique identifier for the run.
name
str
Name of the run/operation.
run_type
str
Type: "llm", "chain", "tool", "retriever", or "prompt".
inputs
dict[str, Any]
Input data for the run.
outputs
dict[str, Any] | None
Output data from the run.
error
str | None
Error message if the run failed.
start_time
datetime
When the run started.
end_time
datetime | None
When the run ended.
parent_run_id
UUID | None
ID of the parent run for nested traces.
trace_id
UUID
ID of the root trace.
dotted_order
str
Hierarchical position in trace tree.
tags
list[str]
Tags attached to the run.
metadata
dict[str, Any]
Metadata attached to the run.
session_id
UUID | None
Project/session ID.

Example

Represents a dataset example (record).
from langsmith.schemas import Example

def my_evaluator(run, example: Example):
    print(f"Example ID: {example.id}")
    print(f"Dataset: {example.dataset_id}")
    print(f"Inputs: {example.inputs}")
    print(f"Outputs: {example.outputs}")  # Ground truth
    print(f"Metadata: {example.metadata}")
id
UUID
Unique identifier for the example.
dataset_id
UUID
ID of the dataset this example belongs to.
inputs
dict[str, Any] | None
Input data for the example.
outputs
dict[str, Any] | None
Expected output data (ground truth).
metadata
dict[str, Any] | None
Additional metadata.
created_at
datetime
When the example was created.
modified_at
datetime | None
When the example was last modified.
source_run_id
UUID | None
ID of the run this example was created from.
url
str | None
URL to view the example in the LangSmith UI.

Dataset

Represents a dataset collection.
from langsmith.schemas import Dataset

client = Client()
dataset: Dataset = client.read_dataset(dataset_name="my-dataset")

print(f"Name: {dataset.name}")
print(f"ID: {dataset.id}")
print(f"Examples: {dataset.example_count}")
print(f"URL: {dataset.url}")
id
UUID
Unique identifier for the dataset.
name
str
Name of the dataset.
description
str | None
Description of the dataset.
data_type
DataType | None
Type of data: "kv", "llm", or "chat".
example_count
int | None
Number of examples in the dataset.
created_at
datetime
When the dataset was created.
modified_at
datetime | None
When the dataset was last modified.
url
str | None
URL to view the dataset in the LangSmith UI.

Create/Update types

ExampleCreate

Used for creating new examples.
from langsmith.schemas import ExampleCreate

example = ExampleCreate(
    inputs={"question": "What is LangSmith?"},
    outputs={"answer": "A platform for LLM development"},
    metadata={"source": "docs"},
    split=["train"]
)

client.create_example(
    dataset_id=dataset.id,
    **example.dict()
)
inputs
dict[str, Any] | None
Input data.
outputs
dict[str, Any] | None
Expected outputs.
metadata
dict[str, Any] | None
Metadata.
split
str | list[str] | None
Dataset splits (e.g., "train", "test").
attachments
dict[str, Attachment] | None
File attachments.
id
UUID | None
Custom ID for the example.

ExampleUpdate

Used for updating existing examples.
from langsmith.schemas import ExampleUpdate

update = ExampleUpdate(
    id=example.id,
    inputs={"question": "Updated question"},
    metadata={"revised": True}
)

client.update_example(**update.dict())

Attachment handling

Attachment

For uploading files with examples or runs.
from langsmith.schemas import Attachment
from pathlib import Path

# From bytes
attachment = Attachment(
    mime_type="text/plain",
    data=b"file contents"
)

# From file path
attachment = Attachment(
    mime_type="image/png",
    data=Path("image.png")
)

# Use in examples
example = client.create_example(
    dataset_id=dataset.id,
    inputs={"query": "Analyze this image"},
    attachments={
        "input_image": attachment
    }
)
mime_type
str
required
MIME type of the attachment (e.g., "text/plain", "image/png").
data
bytes | Path
required
File contents as bytes or path to file.

Enums

DataType

Dataset types.
from langsmith.schemas import DataType

DataType.kv      # Key-value pairs
DataType.llm     # LLM completion format
DataType.chat    # Chat message format

Score types

SCORE_TYPE

Valid score types for feedback.
from langsmith.schemas import SCORE_TYPE

# Union of: StrictBool | StrictInt | StrictFloat | None
score: SCORE_TYPE = 0.95
score: SCORE_TYPE = 1
score: SCORE_TYPE = True

VALUE_TYPE

Valid value types for non-numeric feedback.
from langsmith.schemas import VALUE_TYPE

# Union of: dict | str | StrictBool | StrictInt | StrictFloat | None
value: VALUE_TYPE = "good"
value: VALUE_TYPE = {"category": "excellent", "details": "..."}

Usage metadata

UsageMetadata

Token usage information for LLM calls.
from langsmith.schemas import UsageMetadata, InputTokenDetails

usage = UsageMetadata(
    input_tokens=100,
    output_tokens=50,
    total_tokens=150,
    input_token_details=InputTokenDetails(
        cache_read=20
    )
)
input_tokens
int
Number of input tokens.
output_tokens
int
Number of output tokens.
total_tokens
int
Total tokens (input + output).
input_token_details
InputTokenDetails | None
Detailed breakdown of input tokens (cache hits, etc.).
output_token_details
OutputTokenDetails | None
Detailed breakdown of output tokens (reasoning tokens, etc.).

Working with schemas

Type annotations

from langsmith.schemas import Run, Example
from langsmith.evaluation import EvaluationResult

def my_evaluator(run: Run, example: Example | None) -> EvaluationResult:
    """Type-safe evaluator function."""
    prediction = run.outputs.get("answer")
    reference = example.outputs.get("answer") if example else None
    
    return EvaluationResult(
        key="accuracy",
        score=1.0 if prediction == reference else 0.0
    )

Serialization

All schema types are Pydantic models and support standard serialization:
from langsmith import Client

client = Client()
dataset = client.read_dataset(dataset_name="my-dataset")

# To dict
data = dataset.dict()

# To JSON
import json
json_str = dataset.json()
parsed = json.loads(json_str)

# From dict
from langsmith.schemas import Dataset
dataset = Dataset(**data)

Build docs developers (and LLMs) love