Skip to main content
Mibyan saves conversation trajectories in ShareGPT-compatible JSONL format for use as training data, debugging artifacts, and reinforcement learning datasets. Source files: agent/trajectory.py, agent/session_persistence.py (search for _save_trajectory), batch_runner.py

File Naming Convention

Trajectories are written to files in the current working directory: The batch runner (batch_runner.py) writes to a custom output file per batch (e.g., batch_001_output.jsonl) with additional metadata fields. You can override the filename via the filename parameter in save_trajectory().

JSONL Entry Format

Each line in the file is a self-contained JSON object. There are two variants:

CLI/Interactive Format (from _save_trajectory)

Batch Runner Format (from batch_runner.py)

The tool_stats and tool_error_counts dictionaries are normalized to include ALL possible tools (from model_tools.TOOL_TO_TOOLSET_MAP) with zero defaults, ensuring consistent schema across entries for HuggingFace dataset loading.

Conversations Array (ShareGPT Format)

The conversations array uses ShareGPT role conventions:

Complete Example

Normalization Rules

Reasoning Content Markup

The trajectory converter normalizes ALL reasoning into <think> tags, regardless of how the model originally produced it:
  1. Native thinking tokens (msg["reasoning"] field from providers like Anthropic, OpenAI o-series): Wrapped as <think>\n{reasoning}\n</think>\n and prepended before the content.
  2. REASONING_SCRATCHPAD XML (when native thinking is disabled and the model reasons via system-prompt-instructed XML): <REASONING_SCRATCHPAD> tags are converted to <think> via convert_scratchpad_to_think().
  3. Empty think blocks: Every gpt turn is guaranteed to have a <think> block. If no reasoning was produced, an empty block is inserted: <think>\n</think>\n — this ensures consistent format for training data.

Tool Call Normalization

Tool calls from the API format (with tool_call_id, function name, arguments as JSON string) are converted to XML-wrapped JSON:
  • Arguments are parsed from JSON strings back to objects (not double-encoded)
  • If JSON parsing fails (shouldn’t happen — validated during conversation), an empty {} is used with a warning logged
  • Multiple tool calls in one assistant turn produce multiple <tool_call> blocks in a single gpt message

Tool Response Normalization

All tool results following an assistant message are grouped into a single tool turn with XML-wrapped JSON responses:
  • If tool content looks like JSON (starts with { or [), it’s parsed so the content field contains a JSON object/array rather than a string
  • Multiple tool results are joined with newlines in one message
  • The tool name is matched by position against the parent assistant’s tool_calls array

System Message

The system message is generated at save time (not taken from the conversation). It follows the Mibyan function-calling prompt template with:
  • Preamble explaining the function-calling protocol
  • <tools> XML block containing the JSON tool definitions
  • Schema reference for FunctionCall objects
  • <tool_call> example
Tool definitions include name, description, parameters, and required (set to null to match the canonical format).

Loading Trajectories

Trajectories are standard JSONL — load with any JSON-lines reader:

Loading for HuggingFace Datasets

The normalized tool_stats schema ensures all entries have the same columns, preventing Arrow schema mismatch errors during dataset loading.

Controlling Trajectory Saving

Trajectory saving is a run_agent.py / library-level switch — the mibyan CLI does not expose a config key or flag for it:
Or programmatically: AIAgent(..., save_trajectories=True) / initialize_agent(..., save_trajectories=True). When enabled, the _save_trajectory() method is called at the end of each conversation turn. The batch runner always saves trajectories (that’s its primary purpose). Samples with zero reasoning across all turns are automatically discarded by the batch runner to avoid polluting training data with non-reasoning examples.