Skip to content

Commit 1e06e97

Browse files
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

‎py/src/braintrust/__init__.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ def wrap_anthropic(client: _Any) -> _Any:
9494
return _wrap_anthropic(client)
9595

9696

97+
def start_anthropic_batch_trace(*, batch: _Any, requests: _Any) -> str | None:
98+
from .integrations.anthropic import start_anthropic_batch_trace as _start_anthropic_batch_trace
99+
100+
return _start_anthropic_batch_trace(batch=batch, requests=requests)
101+
102+
103+
def complete_anthropic_batch_trace(
104+
*,
105+
batch: _Any,
106+
results: _Any | None = None,
107+
context: str | None = None,
108+
requests: _Any | None = None,
109+
) -> None:
110+
from .integrations.anthropic import complete_anthropic_batch_trace as _complete_anthropic_batch_trace
111+
112+
return _complete_anthropic_batch_trace(batch=batch, results=results, context=context, requests=requests)
113+
114+
115+
async def complete_anthropic_batch_trace_async(
116+
*,
117+
batch: _Any,
118+
results: _Any | None = None,
119+
context: str | None = None,
120+
requests: _Any | None = None,
121+
) -> None:
122+
from .integrations.anthropic import complete_anthropic_batch_trace_async as _complete_anthropic_batch_trace_async
123+
124+
return await _complete_anthropic_batch_trace_async(
125+
batch=batch, results=results, context=context, requests=requests
126+
)
127+
128+
97129
def wrap_instructor(client: _Any) -> _Any:
98130
from .integrations.instructor import wrap_instructor as _wrap_instructor
99131

‎py/src/braintrust/integrations/anthropic/__init__.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import warnings
22

3+
from .batch import (
4+
complete_anthropic_batch_trace,
5+
complete_anthropic_batch_trace_async,
6+
start_anthropic_batch_trace,
7+
)
38
from .integration import AnthropicIntegration
49
from .tracing import _wrap_anthropic
510

@@ -18,6 +23,9 @@ def wrap_anthropic_client(client):
1823

1924
__all__ = [
2025
"AnthropicIntegration",
26+
"complete_anthropic_batch_trace",
27+
"complete_anthropic_batch_trace_async",
28+
"start_anthropic_batch_trace",
2129
"wrap_anthropic",
2230
"wrap_anthropic_client",
2331
]

0 commit comments

Comments
 (0)