Skip to main content

Functions

tj.init()

Configure the SDK. Call once before other functions.
tj.init(
    *,
    provider: str = "langsmith",
    api_key: str | None = None,
    project_id: str,
    workspace_id: str | None = None,
    destination_id: str | None = None,
    trajectory_api_key: str | None = None,
    transforms: list[BaseTransform] | None = None,
    debug: bool = False,
) -> None
ParameterTypeDefaultDescription
providerstr"langsmith"Trace provider to connect to
api_keystr | NoneNoneProvider API key. If omitted, reads from LANGSMITH_API_KEY or LANGCHAIN_API_KEY environment variable.
project_idstrrequiredProvider project/session ID
workspace_idstr | NoneNoneLangSmith workspace/tenant ID. Required for bulk export.
destination_idstr | NoneNoneBulk export destination ID. Required for bulk export.
trajectory_api_keystr | NoneNoneAPI key for uploading trajectories. Can also be set via TRAJECTORY_API_KEY environment variable.
transformslist[BaseTransform] | NoneNoneList of transforms to apply after building (e.g. PII redaction)
debugboolFalseEnable debug logging

tj.list_conversations()

List available conversations from the configured provider.
tj.list_conversations(*, limit: int = 50) -> list[ConversationSummary]
ParameterTypeDefaultDescription
limitint50Max conversations to return
Returns a list of ConversationSummary:
FieldTypeDescription
conversation_idstrUnique conversation identifier
num_turnsintNumber of turns
first_seenstr | NoneISO timestamp of earliest trace
last_seenstr | NoneISO timestamp of most recent trace
root_run_nameslist[str]Names of top-level runs

tj.import_conversations()

Import conversations and return one Trajectory per conversation.
tj.import_conversations(
    conversations: list[str] | list[ConversationSummary] | None = None,
    *,
    bulk: bool = False,
    source: str | None = None,
    limit: int | None = None,
    since: timedelta | datetime | None = None,
) -> list[Trajectory]
ParameterTypeDefaultDescription
conversationslistNoneConversation IDs or ConversationSummary objects
bulkboolFalseUse bulk export path instead of individual API calls
sourcestr | NoneNonePath to a local parquet file. When bulk=True and source is omitted, the SDK triggers a live export from LangSmith (requires workspace_id and destination_id in init()).
limitint | NoneNoneMax conversations to process (bulk mode only)
sincetimedelta | datetime | NoneNoneTime window for bulk export. Pass a timedelta (e.g. timedelta(hours=1)) to export recent data, or a datetime for an absolute start time. Only used when bulk=True.

tj.save()

Save Trajectories to JSON files on disk.
tj.save(
    trajectories: Trajectory | list[Trajectory],
    output_dir: str,
) -> None
Each Trajectory is saved as {output_dir}/{conversation_id}.json.

tj.transform()

Transform raw conversation data into a Trajectory without fetching from a provider. Useful for custom pipelines where you already have the raw data.
tj.transform(
    raw_data: dict,
    *,
    provider: str = "langsmith",
    api_key: str = "",
    project_id: str = "",
) -> Trajectory
ParameterTypeDefaultDescription
raw_datadictrequiredRaw conversation data in the provider’s format
providerstr"langsmith"Provider to use for parsing
api_keystr""Provider API key (not needed for parsing)
project_idstr""Provider project ID (not needed for parsing)

tj.upload()

Upload trajectories to the Trajectory platform via the Trajectory API. Requires trajectory_api_key to be set in tj.init() or via the TRAJECTORY_API_KEY environment variable.
tj.upload(
    trajectories: Trajectory | list[Trajectory],
    dataset: str,
) -> None
ParameterTypeDescription
trajectoriesTrajectory | list[Trajectory]Trajectories to upload
datasetstrDataset name to group the uploaded trajectories
Empty trajectories (0 steps) are automatically skipped.

Data Models

All models are frozen dataclasses (immutable after construction).

Trajectory

FieldTypeDescription
taskTaskConversation metadata
stepslist[Step]Ordered decision points with cumulative messages
rewardReward | NoneAggregate reward signal
metricsTrajectoryMetrics | NoneComputed content metrics
execution_metricsExecutionMetrics | NoneTiming data
reference_trajectorydict | NoneOptional reference trajectory for comparison
telemetryTelemetry | NoneSource telemetry metadata
idxint | NoneIndex within a batch
errorstr | NoneError if conversation failed

Task

FieldTypeDescription
idstr | NoneTask identifier
data_sourcestr | NoneProvider name (e.g. "langsmith")
conversation_idstr | NoneUnique conversation identifier
num_turnsint | NoneNumber of user-agent turns
num_stepsint | NoneNumber of steps
total_tokensint | NoneTotal tokens consumed
total_costfloat | NoneEstimated cost in USD

Step

FieldTypeDescription
messageslist[Message]Cumulative messages up to this point
rewardReward | NonePer-step reward signal
infodict | NoneProvider-specific metadata
trainable_statusTrainableStatusTraining suitability flag

Message

FieldTypeDescription
roleRole"system", "user", "assistant", or "tool"
contentstr | NoneText content (None for tool-call-only messages)
tool_callslist[ToolCall] | NoneTool invocations by the assistant
tool_responseToolResponse | NoneTool execution result
tool_definitionslist[ToolDefinition] | NoneTool definitions available to the model at this point
usagedict | NoneToken usage stats
finish_reasonstr | NoneWhy the model stopped ("stop", "tool_calls")
metadatadict | NoneProvider-specific message metadata
reasoningstr | NoneChain-of-thought content
trainable_statusTrainableStatusTraining suitability flag

ToolCall

FieldTypeDescription
namestrTool name
argumentsdictArguments passed to the tool
idstr | NoneIdentifier matching the ToolResponse

ToolResponse

FieldTypeDescription
idstrMatches the ToolCall id
namestrTool name
argumentsdictArguments that were passed
responseAny | NoneReturn value
errorstr | NoneError if the call failed
metadatadict | NoneProvider-specific metadata

ToolDefinition

FieldTypeDescription
namestrTool name
descriptionstrWhat the tool does
parametersdictJSON Schema for tool arguments

Telemetry

FieldTypeDescription
sourcestrTelemetry source identifier
datadictRaw telemetry data

TrajectoryMetrics & ExecutionMetrics

FieldTypeDescription
stepsint | NoneTotal steps
tokens_generatedint | NoneTotal completion tokens
aggregated_rewardfloat | NoneAggregate reward value
num_tool_callsint | NoneTotal tool invocations
num_tool_failuresint | NoneFailed tool calls
num_tool_response_noneint | NoneTool calls with no response
tool_error_ratefloat | NoneFailure ratio
env_timefloat | NoneTime in tool execution (seconds)
llm_timefloat | NoneTime waiting for LLM (seconds)
total_timefloat | NoneWall-clock time (seconds)
termination_reasonTerminationReason | NoneWhy the conversation ended

Type Aliases

Role = Literal["system", "user", "assistant", "tool"]

TerminationReason = Literal[
    "TIMEOUT", "ENV_DONE", "MAX_STEPS", "TRUNCATION", "STALE", "ERROR", "NONE"
]