tj.build_trajectory_from_messages() takes a flat list of Message objects and produces a
fully-formed Trajectory ready to upload — same shape as the provider-ingested ones.
The SDK ships recipes (messages_from_*) for the most common formats. For anything else,
construct Message objects yourself using the same primitives.
Quickstart
trajectory_api_key.
Message Recipes
The SDK ships adapters for these source formats:| Recipe | Source format |
|---|---|
messages_from_openai_chat | OpenAI ChatCompletion (modern tool_calls or legacy function_call) |
messages_from_anthropic_messages | Anthropic Messages API content blocks |
messages_from_vercel_ai_sdk | Vercel AI SDK UIMessage / CoreMessage |
messages_from_prompt_response | Flat prompt/response strings |
messages_from_role_content_pairs | [(role, content), ...] tuples |
All recipes fail loud on unexpected input. Silent fallbacks turn parse bugs into silent
data-quality bugs, so unrecognized shapes raise
ValueError / TypeError with descriptive
messages. Fix the input, or write a custom recipe (see Custom Formats).messages_from_openai_chat
human → user, ai → assistant), multi-modal content arrays,
modern tool_calls, legacy function_call, usage, finish_reason, and reasoning
pass-through.
messages_from_anthropic_messages
tool_result blocks are fanned out into one role="tool" Message each so step
builders and tool-failure metrics see every result individually.
messages_from_vercel_ai_sdk
UIMessage (parts) and CoreMessage (content) shapes, text /
tool-invocation / tool-call / tool-result / reasoning parts.
messages_from_prompt_response
For the simplest case — a single prompt and response, optionally with a system message:messages_from_role_content_pairs
For ad-hoc construction from tuples:Building the Trajectory
| Parameter | Type | Required | Description |
|---|---|---|---|
messages | list[Message] | Yes | Flat list of messages across all turns. The SDK builds cumulative steps. |
conversation_id | str | Yes | Unique conversation identifier. |
data_source | str | Yes | Where the trajectory came from (e.g. "my_export", "prod_agent"). |
reward | Reward | None | No | Aggregate reward — use tj.build_reward_from_scalar(value) for a single number. |
task_metadata | dict | None | No | Recognized keys: num_turns, total_tokens, total_cost, completion_tokens. Extras are ignored. |
error | str | None | No | Error message if the conversation failed. Auto-sets termination_reason to "ERROR" if not provided. |
start_time / end_time | str | None | No | ISO timestamps; used to compute execution_metrics.total_time. |
termination_reason | TerminationReason | None | No | Defaults to "ENV_DONE" or "ERROR" based on error. |
extra_telemetry | dict | None | No | Extra fields merged into Trajectory.telemetry.data. |
trace_id | str | None | No | Caller-owned correlation key for linking to telemetry events. |
model_id | str | None | No | Trajectory model identifier (see model id helpers). |
num_tool_calls / num_tool_failures / tool_error_rate), and content-hash / idempotency-key
telemetry fields.
Custom Formats
If none of the shipped recipes fit your data, write your own. The public helpers cover the fiddly parts:| Helper | Purpose |
|---|---|
tj.normalize_role(role_str) | Canonicalize role to system / user / assistant / tool. Recognizes aliases like human, ai, function. |
tj.flatten_text_content(content) | Collapse multi-modal content (string, list of blocks, dict) into plain text. |
tj.parse_tool_arguments(raw) | Normalize tool-call arguments (dict, JSON string, or None) into a dict. |
Pair With Telemetry
build_trajectory_from_messages accepts a trace_id so trajectories built this way participate
in the same correlation story as provider-imported ones. Pair it with tj.start_trace() and
tj.upload_trace() for the cleanest workflow:
Related
Core Concepts
Trajectories, Steps, Turns, Messages.
Linking Events to Trajectories
Correlate uploaded trajectories with telemetry via
trace_id.API Reference
Full signatures for builders, recipes, and helpers.
PII Redaction
Strip sensitive data before trajectories leave your environment.