Skip to content

Commit 86f2514

Browse files
[codex] Add Anthropic early-close regression coverage (#2080)
Co-authored-by: Alex Hall <alex.mojaki@gmail.com>
1 parent 8c50d07 commit 86f2514

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

tests/otel_integrations/test_anthropic.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations as _annotations
22

3+
import gc
34
import json
5+
import logging
46
from collections.abc import AsyncIterator, Iterator
57
from typing import Any, cast
68

@@ -618,6 +620,43 @@ def test_sync_messages_stream(instrumented_client: anthropic.Anthropic, exporter
618620
)
619621

620622

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+
621660
def test_messages_stream_text_block_none() -> None:
622661
from logfire._internal.integrations.llm_providers.anthropic import (
623662
AnthropicMessageStreamState,

0 commit comments

Comments
 (0)