Recorder
Recorder
Bases: Hook
Recorder hook that writes LanguageModel calls as fine-tuning data.
Attach it to a LanguageModel (or enable it everywhere with
synalinks.record_traces()) and every concrete call (the messages sent
to the LM, the tools it was given, and the completion it returned) is
written under base_dir (default ./.synalinks in the working
directory). Attached to any other module type, it records nothing.
Symbolic calls (made while building the graph) are not recorded.
Because a LanguageModel is usually shared by several modules
(Generator, ChainOfThought, agents ...), each call is attributed to
the module it originates from (the module whose call() invoked the
LM), with one folder per program and per originating module, nested
along the module's name scope:
./.synalinks/
└── program_name/
└── module_name/ # e.g. function_calling_agent/tool_calls_generator
├── train.jsonl # one fine-tuning example per line
├── metadata.jsonl # one line per train.jsonl line
└── failed.jsonl # failed, streamed or not representable calls
The program folder is named after the entry module of the call context
(usually the Program). When the LM is called directly, the files land
in <base_dir>/<lm_name>/. Files are appended to across runs.
train.jsonl holds only complete conversations (ending with the
assistant's completion) and only the keys of the target format (see
format_conversation), so it can be given as is to a fine-tuning
framework: NVIDIA NeMo AutoModel or NeMo RL ("openai", the default),
Tinker's FromConversationFileBuilder ("tinker"), or NVIDIA NeMo
Customizer ("customizer"). A structured completion is the JSON string
of its fields. When a provider emits reasoning only as opaque
thinking_blocks (e.g. Anthropic), their text is used as the reasoning.
Each metadata.jsonl line describes the train.jsonl line with the
same line_hash (the SHA-256 of that line):
synalinks_version: The version of Synalinks that wrote the record.session_id: The id of the recording process, shared by its calls.format: The format of thetrain.jsonlline.call_id: The unique id of the LM call.parent_call_id: The id of the parent module's call (Noneif the LM is the entry module).program: The name of the entry module (usually theProgram).module: The name of the module the LM call originates from.timestamp: The time the call began (seconds since the epoch).duration: The call duration in seconds.usage: The token usage of the call (prompt_tokens,completion_tokens,total_tokens,cached_tokens,cache_creation_tokens,reasoning_tokens). All zeros on a cache hit;Noneif the call failed, was streamed, or was answered by the fallback (whose own record carries the usage).cost: The cost of the call in USD as reported by the provider (0.0for local models and cache hits,Nonewhenusageis).cache_hit:Truewhen the response came from the LM's file cache (only present then).fallback:Truewhen every attempt failed and the LM's fallback answered (only present then).inputs_hash: The SHA-256 hex digest of the input messages (with sorted keys), to deduplicate examples.line_hash: The SHA-256 hex digest of thetrain.jsonlline.outputs: The completion returned by the LM as JSON.schema: The JSON schema the completion was constrained to (Nonefor a free-form call).config: The serializedLanguageModel(as given bysynalinks.saving.serialize_synalinks_object).config_hash: The SHA-256 hex digest of theconfigJSON (with sorted keys), to group examples by model configuration.
A failed.jsonl line has the same keys (without line_hash), plus the
conversation in chat-completion format (messages, and tools when the
call had tools) and one of:
exception: The error, when the call failed (including when every attempt failed and no fallback answered) or was streamed.unsupported: Why the conversation is not representable in the targetformat(e.g. tool calls in the"tinker"format).
Example:
import synalinks
# Enable recording globally
synalinks.record_traces()
# Or attach a Recorder hook directly to a LanguageModel
lm = synalinks.LanguageModel(
model="ollama/mistral",
hooks=[synalinks.hooks.Recorder(format="tinker")],
)
inputs = synalinks.Input(data_model=Query)
outputs = await synalinks.Generator(
data_model=Answer,
language_model=lm,
)(inputs)
program = synalinks.Program(inputs=inputs, outputs=outputs, name="my_program")
await program(Query(query="What is the capital of France?"))
# => ./.synalinks/my_program/generator/train.jsonl (+ metadata.jsonl)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
str
|
The root folder where the records are written.
If None, uses the value from |
None
|
format
|
str
|
The format of the |
None
|
Source code in synalinks/src/hooks/recorder.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
format_conversation(messages, tools=None, format='openai')
Format a chat-completion conversation as one fine-tuning example.
This is how the Recorder writes each line of its train.jsonl files.
The formats were checked against the frameworks' own loaders:
"openai":{"messages": [...], "tools": [...]}with the chat-completion messages as is (reasoning_content,tool_callswith JSON-stringarguments,role: "tool"results withtool_call_id). Loads in NVIDIA NeMo AutoModel (ChatDataset) and NeMo RL (openai_format), which render the tools, reasoning, tool calls and tool results."tinker":{"messages": [...]}withroleandcontentonly, reasoning prefixed to the content as<think>...</think>(Tinker dropsreasoning_content). Loads in Tinker'sFromConversationFileBuilder, which cannot load tool calls from a file: conversations with tool calls are not representable."customizer":{"messages": [...], "tools": [...]}restricted to the keys NVIDIA NeMo Customizer validates (role,content,thinking,tool_calls): reasoning becomes a separatethinkingmessage, tool-callargumentsa JSON object, andtool_call_idis dropped.
"tinker" and "customizer" only take text content: a conversation
with image or audio parts is not representable in them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list
|
Chat-completion message dicts. |
required |
tools
|
list
|
Optional chat-completion tool declarations. |
None
|
format
|
str
|
One of |
'openai'
|
Returns:
| Type | Description |
|---|---|
tuple
|
|