Recorder
Recorder
Bases: Hook
Recorder hook that writes LanguageModel calls to JSONL files.
This hook is designed to collect training data: attach it to a
LanguageModel and every concrete call (the chat messages sent to the
LM and the completion it returned) is appended as one JSON line to a
file under base_dir (default synalinks_home(), i.e.
$SYNALINKS_HOME or ~/.synalinks). Attached to any other module
type, it records nothing.
Each record carries the full conversation in a top-level messages
key in OpenAI chat format (completion included as the final assistant
message), which is the chat dataset schema of OpenAI-compatible
fine-tuning stacks such as NVIDIA NeMo (NeMo Customizer chat datasets,
NeMo Framework SFT loaders return the extra metadata keys as-is). For
a consumer that rejects unknown keys, project the messages field
(e.g. jq -c '{messages}' *.jsonl).
Because a LanguageModel is usually shared by several modules
(Generator, ChainOfThought, agents ...), each record is attributed
to the module the call originates from (the module whose call()
invoked the LM), and the files are organized as one folder per program
and one subfolder per originating module:
The program folder is named after the entry module of the call context
(the outermost module of the call stack, usually the Program). When
the LM is called directly (no enclosing module), the records land in
<base_dir>/<lm_name>/<lm_name>_TIMESTAMP.jsonl. Symbolic calls (made
while building the graph) are not recorded.
Each line is a JSON object with the following keys:
synalinks_version: The version of Synalinks that wrote the record.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;Noneif the call failed).cost: The cost of the call in USD as reported by the provider (0.0for local models,Noneif the call failed).messages: The conversation in OpenAI chat format: the chat messages sent to the LM, with the completion appended as the final assistant message (a structured completion is appended as its JSON string). Nothing is appended if the call failed or the response was streamed. Messages carry the chat-completion keys (role,content,reasoning_content,tool_calls,tool_call_id,name) —reasoning_contentis kept because fine-tuning stacks with reasoning support (e.g. NeMo) train on it. When a provider emits reasoning only as opaquethinking_blocks(e.g. Anthropic), their text is folded intoreasoning_content; the raw blocks stay inoutputsonly.inputs_hash: The SHA-256 hex digest of the input messages (the conversation without the appended completion, with sorted keys), to deduplicate records.outputs: The completion returned by the LM as JSON (Noneif the call failed or the response was streamed).config: The serializedLanguageModel(as given bysynalinks.saving.serialize_synalinks_object).config_hash: The SHA-256 hex digest of theconfigJSON (with sorted keys), to group records by module configuration.exception: The exception message (only present if the call failed).
You can enable recording for every module by using
synalinks.record_traces() at the beginning of your scripts.
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()],
)
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/generator_20260703-101530.jsonl
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
str
|
The root folder where the records are written.
If None, uses the value from
|
None
|
Source code in synalinks/src/hooks/recorder.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 | |