Uh oh!
There was an error while loading. Please reload this page.
fix(anthropic): capture time_to_first_token when using .text_stream - #234
Conversation
Drop into this review session: Code Review: #234Fix is correct. Two test gaps worth addressing before merge. Root cause analysis
The fix is the right approach: add an explicit Issues1. Tests don't assert span output or token counts (minor gap) The new tests only call The existing assertlog["output"]["role"] =="assistant"assertlog["output"]["content"]
assertlog["metrics"]["prompt_tokens"] >0assertlog["metrics"]["completion_tokens"] >02. Sync test is missing the initial
3. Async/sync dispatch heuristic in # tracing.py:336ifhasattr(self.__msg_stream, "__aiter__"):
returnself.__async_text_stream()
returnself.__sync_text_stream()This works today because Anthropic's sync # Async streams expose __aiter__; sync streams do not.What's good
Tests confirmed passing locally: |
TracedMessageStream.text_stream was falling through via Wrapper.__getattr__ to the unwrapped Anthropic stream, so __process_message was never called and time_to_first_token was not recorded on the span. Override text_stream to route iteration through the traced stream's own __next__/__anext__, ensuring __process_message is called for every event. Fixes BT-4702.
5545f8a to
cd6f402Compare
TracedMessageStream.text_stream was falling through via Wrapper.getattr to the unwrapped Anthropic stream, so __process_message was never called and time_to_first_token was not recorded on the span.
Override text_stream to route iteration through the traced stream's own next/anext, ensuring __process_message is called for every event.
Fixes https://linear.app/braintrustdata/issue/BT-4702/python-anthropic-time-to-first-token-not-captured-when-using-text