langsmith-rust 0.1.2

Rust crate for manual tracing to LangSmith, providing similar ergonomics to the Python and TypeScript SDKs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
# Reading Guide: Understanding the LangSmith Rust Codebase

This guide provides a recommended reading order for understanding the codebase, from simplest to most complex concepts.

## Prerequisites

Before reading the code, ensure you understand:
- Rust basics (ownership, borrowing, async/await)
- Serde serialization
- HTTP clients (reqwest)
- Design patterns (Strategy, Factory, Observer)

## Reading Order

### Level 1: Foundation (Start Here)

**Goal**: Understand the basic data structures and configuration.

#### 1. `src/error.rs`
**Why first**: Understanding error types helps with error handling throughout.
**What you'll learn**: 
- Error types used in the crate
- How errors are structured
- Error conversion patterns

**Key concepts**:
- `LangSmithError` enum
- `Result<T>` type alias
- Error propagation

**Time**: 5 minutes

#### 2. `src/config/env.rs`
**Why second**: Configuration is the foundation - everything depends on it.
**What you'll learn**:
- How environment variables are loaded
- Singleton pattern implementation
- Configuration structure

**Key concepts**:
- `Config` struct
- `Lazy<Mutex<Option<Config>>>` singleton
- Environment variable loading
- `.env` file support

**Time**: 10 minutes

#### 3. `src/models/run.rs`
**Why third**: The `Run` struct is the core data model.
**What you'll learn**:
- Structure of a LangSmith run
- Run types (Chain, Llm, Tool, etc.)
- How `dotted_order` is generated
- Parent-child relationships

**Key concepts**:
- `Run` struct
- `RunType` enum
- `RunUpdate` struct
- `generate_dotted_order()` method

**Time**: 15 minutes

#### 4. `src/models/messages.rs`
**Why fourth**: Message types are important for agent systems.
**What you'll learn**:
- LangChain-compatible message types
- Tool calls and tool messages
- Message serialization

**Key concepts**:
- `AIMessage`, `ToolMessage`, `HumanMessage`, `SystemMessage`
- `ToolCall` struct
- `Message` enum

**Time**: 10 minutes

**Checkpoint**: At this point, you should understand:
- ✅ What data structures are used
- ✅ How configuration works
- ✅ What a "run" represents

---

### Level 2: Core Functionality

**Goal**: Understand how tracing actually works.

#### 5. `src/utils/serialization.rs`
**Why fifth**: Serialization is used everywhere - understand it early.
**What you'll learn**:
- How inputs/outputs are serialized
- Why primitives are wrapped in objects
- JSON object validation

**Key concepts**:
- `ensure_object()` function
- `ensure_inputs_object()` / `ensure_outputs_object()`
- Primitive wrapping logic

**Time**: 10 minutes

#### 6. `src/client/http.rs`
**Why sixth**: The HTTP client is the interface to LangSmith API.
**What you'll learn**:
- How HTTP requests are made
- API endpoint structure
- Error handling in HTTP calls
- Authentication headers

**Key concepts**:
- `LangSmithClient` struct
- `post_run()` method
- `patch_run()` method
- Header management

**Time**: 15 minutes

#### 7. `src/tracing/context.rs`
**Why seventh**: Context propagation is key to hierarchical tracing.
**What you'll learn**:
- What trace context contains
- How context flows from parent to child
- Builder pattern usage

**Key concepts**:
- `TraceContext` struct
- Context builder methods
- Context propagation

**Time**: 10 minutes

#### 8. `src/tracing/tracer.rs` (First Pass)
**Why eighth**: Tracer is the main interface - read it carefully.
**What you'll learn**:
- How tracers are created
- Parent-child relationships
- How runs are posted and patched
- Context management

**Key concepts**:
- `Tracer` struct
- `create_child()` method
- `post()` and `patch()` methods
- Context propagation

**Time**: 20 minutes

**Checkpoint**: At this point, you should understand:
- ✅ How data flows to LangSmith
- ✅ How parent-child relationships work
- ✅ How context is propagated

---

### Level 3: Advanced Features

**Goal**: Understand helper functions and design patterns.

#### 9. `src/tracing/decorator.rs`
**Why ninth**: The decorator pattern makes tracing automatic.
**What you'll learn**:
- How `trace_node` works
- Automatic tracing flow
- Error handling in decorators
- Sync vs async versions

**Key concepts**:
- `trace_node()` function
- `trace_node_sync()` function
- Function wrapping pattern
- Error propagation

**Time**: 15 minutes

#### 10. `src/factories/tracer_factory.rs`
**Why tenth**: Factory pattern simplifies tracer creation.
**What you'll learn**:
- Factory pattern implementation
- Different tracer creation methods
- When to use each factory method

**Key concepts**:
- `TracerFactory` struct
- Factory methods (`create_root()`, `create_with_thread()`, etc.)
- Factory pattern benefits

**Time**: 10 minutes

#### 11. `src/strategies/serialization_strategy.rs`
**Why eleventh**: Strategy pattern for serialization.
**What you'll learn**:
- Strategy pattern implementation
- Pluggable serialization
- Default serialization strategy

**Key concepts**:
- `SerializationStrategy` trait
- `DefaultSerializationStrategy`
- Strategy pattern benefits

**Time**: 10 minutes

#### 12. `src/strategies/tracing_strategy.rs`
**Why twelfth**: Strategy pattern for tracing.
**What you'll learn**:
- Different tracing strategies
- Async vs sync strategies
- How strategies are used

**Key concepts**:
- `TracingStrategy` trait
- `AsyncTracingStrategy`
- `SyncTracingStrategy`

**Time**: 15 minutes

**Checkpoint**: At this point, you should understand:
- ✅ How automatic tracing works
- ✅ Design patterns used
- ✅ How to extend functionality

---

### Level 4: Integration & Observability

**Goal**: Understand advanced features and integration patterns.

#### 13. `src/observability/observer.rs`
**Why thirteenth**: Observer pattern for additional observability.
**What you'll learn**:
- Observer pattern implementation
- How observers work
- LangSmith observer

**Key concepts**:
- `Observer` trait
- `LangSmithObserver`
- Observer methods

**Time**: 10 minutes

#### 14. `src/observability/observable.rs`
**Why fourteenth**: Observable trait implementation.
**What you'll learn**:
- Observable pattern
- How to make nodes observable
- Observer management

**Key concepts**:
- `Observable` trait
- `ObservableNode` struct
- Observer notification

**Time**: 10 minutes

#### 15. `src/observability/node_wrapper.rs`
**Why fifteenth**: Wrapper for making nodes observable.
**What you'll learn**:
- How to wrap nodes
- Observable node execution
- Integration with tracing

**Key concepts**:
- `ObservableNodeWrapper`
- Node wrapping pattern
- Observer integration

**Time**: 15 minutes

#### 16. `src/lib.rs`
**Why sixteenth**: Main entry point - see how everything fits together.
**What you'll learn**:
- Module structure
- Public API
- Re-exports
- Initialization

**Key concepts**:
- Module organization
- Public API design
- Initialization flow

**Time**: 10 minutes

**Checkpoint**: At this point, you should understand:
- ✅ How observability works
- ✅ How everything fits together
- ✅ The complete architecture

---

### Level 5: Examples & Integration

**Goal**: See how it all works in practice.

#### 17. `examples/test_llm_tracing.rs`
**Why seventeenth**: See basic usage in action.
**What you'll learn**:
- Basic tracing usage
- Manual tracing pattern
- LLM call tracing

**Time**: 15 minutes

#### 18. `examples/decorator_example.rs`
**Why eighteenth**: See decorator pattern in action.
**What you'll learn**:
- `trace_node` usage
- Multiple node tracing
- Hierarchical tracing

**Time**: 15 minutes

#### 19. `examples/observable_graph.rs`
**Why nineteenth**: See observer pattern in action.
**What you'll learn**:
- Observable nodes
- Multiple observers
- Observer integration

**Time**: 15 minutes

#### 20. `tests/` directory
**Why twentieth**: Tests show expected behavior.
**What you'll learn**:
- How components are tested
- Expected usage patterns
- Edge cases

**Time**: 30 minutes

**Checkpoint**: At this point, you should understand:
- ✅ How to use the crate
- ✅ Best practices
- ✅ Integration patterns

---

## Deep Dive Topics

After completing the reading order, dive deeper into:

### 1. Error Handling Flow
- Trace through error propagation
- Understand when errors are logged vs returned
- See how tracing errors don't break execution

### 2. Context Propagation
- Understand `dotted_order` generation
- See how `trace_id` flows through hierarchy
- Understand parent-child relationships

### 3. Serialization Flow
- Trace how inputs/outputs are serialized
- Understand primitive wrapping
- See JSON object validation

### 4. HTTP Request Flow
- Understand request building
- See header management
- Understand response handling

### 5. Design Patterns
- Strategy pattern: How strategies are used
- Factory pattern: How factories simplify creation
- Observer pattern: How observers are notified
- Singleton pattern: How config is managed

## Common Questions

### Q: Where does tracing actually happen?
**A**: In `src/tracing/tracer.rs` - `post()` and `patch()` methods call `LangSmithClient`.

### Q: How are parent-child relationships maintained?
**A**: Through `trace_id` (inherited) and `parent_run_id` (set to parent's `run_id`).

### Q: How does `trace_node` work?
**A**: It wraps your function, creates a tracer, posts before execution, executes function, then patches with outputs.

### Q: What's the difference between `Tracer` and `TracerFactory`?
**A**: `Tracer` is the main struct. `TracerFactory` provides convenient factory methods for common patterns.

### Q: How do I add a new run type?
**A**: Add a variant to `RunType` enum in `src/models/run.rs`.

## Tips for Reading

1. **Read in order**: Each level builds on previous levels
2. **Run examples**: Execute examples while reading to see behavior
3. **Check tests**: Tests show expected usage patterns
4. **Trace execution**: Follow a simple example through the code
5. **Draw diagrams**: Visualize data flow and relationships

## Time Estimate

- **Level 1**: ~40 minutes
- **Level 2**: ~55 minutes
- **Level 3**: ~50 minutes
- **Level 4**: ~45 minutes
- **Level 5**: ~75 minutes

**Total**: ~4-5 hours for complete understanding

## Next Steps

After reading:
1. Try modifying examples
2. Add a new run type
3. Create a custom observer
4. Integrate with your own graph system
5. Read `ARCHITECTURE.md` for deeper understanding