Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these cassettes are not valid

    status:
      code: 401
      message: Unauthorized

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions py/src/braintrust/integrations/anthropic/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1740,3 +1740,24 @@ def test_sync_batches_results_logs_error_on_failure(self, memory_logger):
span = spans[0]
assert span["span_attributes"]["name"] == "anthropic.messages.batches.results"
assert "results fetch failed" in span["error"]


@pytest.mark.vcr
def test_anthropic_sync_create_stream_logs_error_and_ends_span(memory_logger):
# A request that the API rejects (here: an invalid API key, a 401) must end the span with
# the error, as it does for the non-streaming call and for the async client.
assert not memory_logger.pop()

client = wrap_anthropic(anthropic.Anthropic(api_key="invalid-key-for-error-cassette"))
with pytest.raises(anthropic.AuthenticationError):
client.messages.create(
model=MODEL,
max_tokens=16,
messages=[{"role": "user", "content": "hi"}],
stream=True,
)

spans = memory_logger.pop()
assert len(spans) == 1
assert spans[0]["span_attributes"]["name"] == "anthropic.messages.stream"
assert spans[0]["error"]
7 changes: 6 additions & 1 deletion py/src/braintrust/integrations/anthropic/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ def create(self, *args, **kwargs):
def __trace_stream(self, stream_func, *args, **kwargs):
span = _start_span("anthropic.messages.stream", kwargs)
request_start_time = time.time()
s = stream_func(*args, **kwargs)
try:
s = stream_func(*args, **kwargs)
except Exception as e:
span.log(error=e)
span.end()
raise
return TracedMessageStreamManager(s, span, request_start_time)


Expand Down
Loading