Skip to main content

Session Management

tj.init()

Configure the SDK. Call once before other functions. For telemetry-only usage, only project_id and trajectory_api_key are required; for trace import, also pass provider and api_key.

Trace Import

tj.list_conversations()

List available conversations from the configured provider.

tj.import_conversations()

Import conversations and return one Trajectory per conversation.

tj.transform()

Transform raw provider data into a Trajectory without fetching. Useful for custom pipelines that already have the source payload.

Building Trajectories

See Bring Your Own Data for the full walkthrough.

tj.build_trajectory_from_messages()

Build a Trajectory from a flat list of Message objects. Cumulative steps, tool-call metrics, and telemetry fields (content hash, idempotency key) are computed automatically.
task_metadata recognizes num_turns, total_tokens, total_cost, and completion_tokens; other keys are ignored.

tj.build_trajectory_from_parsed()

Build a Trajectory from a ParsedConversation. Provider subclasses delegate to this so the ParsedConversation → Trajectory logic has a single implementation.

tj.build_reward_from_scalar()

Build a single-component Reward from a numeric score. scaled_value is normalized to [0, 1] using score_range.

Message Recipes

Adapters from common third-party message formats into Message objects. All recipes fail loud on unexpected input — see Bring Your Own Data for examples.

Helpers


Trace Workflow

See Linking Events to Trajectories for the workflow.

tj.start_trace()

Create a TraceContext for buffering correlated telemetry and trajectory data.
If trace_id is omitted, the SDK generates a UUID4 hex string.

TraceContext

trace.event(event_type, properties=None, *, user_id=None, trajectory_id=None, source="sdk", metadata=None) -> TelemetryEvent Append a TelemetryEvent to trace.events with trace_id and session_id set to trace.trace_id. trace.build_trajectory(messages, *, data_source, conversation_id=None, ...) -> Trajectory Convenience wrapper around build_trajectory_from_messages that injects trace_id and defaults conversation_id to trace_id. Accepts the same kwargs as build_trajectory_from_messages (minus trace_id).

tj.upload_trace()

Upload trajectories, stamp matching events with their returned trajectory_id, then push events — atomically.
Each input trajectory must carry a trace_id. Returns a dict with upload, push, events (as actually sent), and unstamped_events (events pushed without a trajectory_id because no match was found). Raises RuntimeError if any trajectory’s upload failed and there are unstamped events tied to its trace_id — no events are pushed in that case.

Upload & Push

tj.upload()

Upload trajectories via the Trajectory API.
Returns a dict:

tj.push_events()

Push telemetry events to the Trajectory API. Invalid events are dropped with logged warnings.

tj.save()

Save trajectories to JSON files on disk. Each file is named {output_dir}/{conversation_id}.json.

Model ID Helpers

For chaining a Trajectory-served chat completion back to its trajectory id, the backend returns the model id on the response headers.

Primitives

All primitives are frozen dataclasses unless noted.

Trajectory

Task

Step

Message

ToolCall

ToolResponse

ToolDefinition

Reward & RewardComponent

TelemetryEvent

See Telemetry Events for the full guide.

PushResult

Telemetry

TrajectoryMetrics & ExecutionMetrics

ConversationSummary


Type Aliases