Repository navigation
Commit 1e06e97
feat(anthropic)!: replace batch auto-instrumentation with explicit trace apis
> 🤖 **This text was AI-generated.**
### Problem
Previously, `wrap_anthropic()` and `AnthropicIntegration.setup()` auto-instrumented
`messages.batches.create()` and `messages.batches.results()` (including `beta.messages.batches`),
emitting two disconnected, immediately-ended `task` spans with no child `llm` spans and
no per-request inputs, outputs, usage metrics, or resumable correlation:
```
task (anthropic.messages.batches.create) # ended immediately, custom_ids only
task (anthropic.messages.batches.results) # ended immediately, {"type": "jsonl_stream"}
```
This violated the Braintrust Batch APIs instrumentation spec, which forbids auto-instrumenting
asynchronous batch lifecycle methods and requires explicit start/collect APIs that produce a
parent `task` span with one child `llm` span per batch request.
### Solution
1. Removed `Batches` / `AsyncBatches` wrappers from `Messages` and `AsyncMessages` so
`messages.batches` and `beta.messages.batches` pass through untouched under both
`wrap_anthropic()` and `AnthropicIntegration.setup()`.
2. Extended `_internal_start_span_with_initial_merge` in `logger.py` to accept `start_time`,
optional `root_span_id`, and `internal` options while routing directly through
`SpanComponentsV4` without colliding with an active `current_span`.
3. Added explicit, caller-owned Anthropic batch capture APIs exported from both
`braintrust` and `braintrust.integrations.anthropic`:
- `start_anthropic_batch_trace(batch=..., requests=...) -> str | None`
- `complete_anthropic_batch_trace(batch=..., results=..., context=..., requests=...) -> None`
- `complete_anthropic_batch_trace_async(batch=..., results=..., context=..., requests=...) -> None`
```
task (anthropic.batch)
├── llm (anthropic.messages.create) [custom_id: req-1]
└── llm (anthropic.messages.create) [custom_id: req-2]
```
```python
batch = client.messages.batches.create(requests=requests)
context = start_anthropic_batch_trace(batch=batch, requests=requests)
# Later (even in another process) once batch.processing_status == "ended":
results = client.messages.batches.results(batch.id)
complete_anthropic_batch_trace(batch=batch, results=results, context=context)
```
The returned context is versioned, constant-size (stores a SHA-256 digest of request IDs
rather than per-item payloads), strips `propagated_event`, and supports idempotent retries,
streaming JSONL/iterables/async iterables, and collect-only mode when `requests` is supplied
without a prior `start_anthropic_batch_trace` call.
BREAKING CHANGE: `messages.batches.create()` and `messages.batches.results()` are no longer
auto-instrumented by `wrap_anthropic()` or `AnthropicIntegration.setup()`. Use
`start_anthropic_batch_trace()` and `complete_anthropic_batch_trace()` /
`complete_anthropic_batch_trace_async()` instead.
Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>1 parent 7fd4377 commit 1e06e97
12 files changed
Lines changed: 1026 additions & 766 deletions
File tree
- py/src/braintrust
- integrations/anthropic
- cassettes
- 0.48.0
- 1.17.0
- latest
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
94 | 94 | | |
95 | 95 | | |
96 | 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 | + | |
97 | 129 | | |
98 | 130 | | |
99 | 131 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
3 | 8 | | |
4 | 9 | | |
5 | 10 | | |
| |||
18 | 23 | | |
19 | 24 | | |
20 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
21 | 29 | | |
22 | 30 | | |
23 | 31 | | |
0 commit comments