RunTree class provides fine-grained control over trace creation and management. While @traceable is recommended for most use cases, RunTree is useful when you need manual control over trace lifecycle.
Constructor
Name of the run.
Type of run:
"llm", "chain", "tool", "retriever", or "prompt".Input data for the run.
LangSmith project to log to. Defaults to environment variable.
Custom run ID. Auto-generated if not provided.
Parent RunTree for creating nested traces.
Tags for filtering and organization.
Additional metadata.
LangSmith client to use. Uses default client if not provided.
Core methods
post
Send the run to LangSmith (creates or updates).Queues the run for background upload.
end
Mark the run as completed and set outputs.Output data from the run.
Error message if the run failed.
Explicit end time. Defaults to current time.
patch
Update specific fields of the run.create_child
Create a child run for nested tracing.Name of the child run.
Type of the child run.
Inputs for the child run.
The newly created child RunTree.
Context manager usage
Use RunTree as a context manager for automatic timing and error handling:Manual trace example
Complete example of manually building a trace:Properties
Unique identifier for the run.
ID of the root trace (top-level run).
Hierarchical position in the trace tree (e.g.,
"20240101T120000.1.2").ID of the parent run, if any.
When the run started.
When the run ended.
Metadata attached to the run. Can be modified directly.
Tags attached to the run. Can be modified directly.
Input data for the run.
Output data from the run.
Error message if the run failed.
Global configuration with configure()
Set global defaults for all RunTree instances:When to use RunTree
UseRunTree instead of @traceable when:
- You need manual control over timing: Start and end runs at specific points
- Building traces programmatically: Constructing complex trace structures
- Integrating with existing code: Can’t easily decorate functions
- Custom nested structures: Need precise control over parent-child relationships
@traceable for its simplicity and automatic handling.