Skip to main content

Functions

tj.init()

Configure the SDK. Call once before list_conversations() or import_conversations().
tj.init(
    *,
    provider: str = "langsmith",
    api_key: str,
    project_id: str,
    storage_dir: str = "~/.trajectory",
    debug: bool = False,
) -> None
ParameterTypeDefaultDescription
providerstr"langsmith"Trace provider to connect to
api_keystrrequiredProvider API key
project_idstrrequiredProvider project ID
storage_dirstr"~/.trajectory"Directory for local staging data
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 | ConversationSummary] | None = None,
    *,
    bulk: bool = False,
    source: str | None = None,
    stage: bool = True,
    redactor: BasePiiRedactor | None = None,
) -> list[Trajectory]
ParameterTypeDefaultDescription
conversationslistNoneConversation IDs or ConversationSummary objects
bulkboolFalseUse bulk export path instead of API calls
sourcestrNonePath to local export file (required when bulk=True)
stageboolTrueStage in local SQLite for deduplication
redactorBasePiiRedactorNonePII redactor to apply after building

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.

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
errorstr | NoneError if conversation failed

Task

FieldTypeDescription
conversation_idstr | NoneUnique conversation identifier
data_sourcestr | NoneProvider name (e.g. "langsmith")
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
usagedict | NoneToken usage stats
finish_reasonstr | NoneWhy the model stopped ("stop", "tool_calls")
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

TrajectoryMetrics & ExecutionMetrics

FieldTypeDescription
stepsint | NoneTotal steps
tokens_generatedint | NoneTotal completion tokens
num_tool_callsint | NoneTotal tool invocations
num_tool_failuresint | NoneFailed tool calls
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"
]