|
1 | 1 | from __future__ import annotations as _annotations |
2 | 2 |
|
| 3 | +import gc |
3 | 4 | import json |
| 5 | +import logging |
4 | 6 | from collections.abc import AsyncIterator, Iterator |
5 | 7 | from typing import Any, cast |
6 | 8 |
|
@@ -618,6 +620,43 @@ def test_sync_messages_stream(instrumented_client: anthropic.Anthropic, exporter |
618 | 620 | ) |
619 | 621 |
|
620 | 622 |
|
| 623 | +def test_sync_messages_stream_close_early( |
| 624 | + instrumented_client: anthropic.Anthropic, exporter: TestExporter, caplog: pytest.LogCaptureFixture |
| 625 | +) -> None: |
| 626 | + response = instrumented_client.messages.create( |
| 627 | + max_tokens=1000, |
| 628 | + model='claude-3-haiku-20240307', |
| 629 | + system='You are a helpful assistant.', |
| 630 | + messages=[{'role': 'user', 'content': 'What is four plus five?'}], |
| 631 | + stream=True, |
| 632 | + ) |
| 633 | + chunks_seen = 0 |
| 634 | + chunk = None |
| 635 | + |
| 636 | + with caplog.at_level(logging.ERROR, logger='opentelemetry.context'): |
| 637 | + with response as stream: |
| 638 | + for chunk in stream: |
| 639 | + if hasattr(chunk, 'delta') and isinstance(chunk.delta, TextDelta): # type: ignore |
| 640 | + chunks_seen += 1 |
| 641 | + break |
| 642 | + del chunk |
| 643 | + del stream |
| 644 | + del response |
| 645 | + gc.collect() |
| 646 | + |
| 647 | + assert chunks_seen == 1 |
| 648 | + assert not [ |
| 649 | + record |
| 650 | + for record in caplog.records |
| 651 | + if record.name == 'opentelemetry.context' and 'Failed to detach context' in record.getMessage() |
| 652 | + ] |
| 653 | + span_names = [span['name'] for span in exporter.exported_spans_as_dict(parse_json_attributes=True)] |
| 654 | + assert span_names == [ |
| 655 | + 'Message with {request_data[model]!r}', |
| 656 | + 'streaming response from {request_data[model]!r} took {duration:.2f}s', |
| 657 | + ] |
| 658 | + |
| 659 | + |
621 | 660 | def test_messages_stream_text_block_none() -> None: |
622 | 661 | from logfire._internal.integrations.llm_providers.anthropic import ( |
623 | 662 | AnthropicMessageStreamState, |
|
0 commit comments