🐛 Bug Report: Anthropic: a sync stream that fails mid-way never ends its span

Open
#4,643 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@lakshya-dhariwal is already working on this.

Since Oct 20, 2026.

  • #4644 by @lakshya-dhariwal — open
  • #4705 by @simin-yuan — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python

Research direction

Read streaming.py at AnthropicStream.__next__ and compare its exception handling with AnthropicAsyncStream.__anext__. Add a sync regression test using a stream that emits an error mid-iteration; verify the raised error ends the span with status ERROR and that the span is exported only once. A linked pull request is already open.

Written by the indexing model from the issue text.

Description

Which component is this bug for?

Anthropic Instrumentation

📜 Description

When a sync Anthropic stream raises during iteration, its LLM span never ends. The failed call never reaches the backend. Two ways to hit it: the API sends an event: error such as overloaded_error mid-stream, or the connection drops.

AnthropicStream.__next__ (streaming.py:250-264 at 8d9c93f) bumps the exception counter and re-raises. It ends the span only on StopIteration. AnthropicAsyncStream.__anext__ (streaming.py:390-399) ends the span with ERROR on the same exception. #4101 (d6c991c) edited this sync except block (raise e to raise) and left the span open.

👟 Reproduction steps
  1. pip install -e packages/opentelemetry-instrumentation-anthropic anthropic==1.13.0 opentelemetry-sdk at 8d9c93f.
  2. Run this script. A mock transport serves one text delta and then the error event, with no network:
import asyncio
import json

import anthropic
import httpx2 as httpx  # anthropic 1.13.0 ships on httpx2
from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter

exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
AnthropicInstrumentor().instrument(tracer_provider=provider)


def sse(event, data):
    return f"event: {event}\ndata: {json.dumps(data)}\n\n"


# one text delta, then the API's mid-stream error event
BODY = "".join([
    sse("message_start", {"type": "message_start", "message": {
        "id": "msg_1", "type": "message", "role": "assistant", "model": "claude-x", "content": [],
        "stop_reason": None, "stop_sequence": None, "usage": {"input_tokens": 11, "output_tokens": 1}}}),
    sse("content_block_start", {"type": "content_block_start", "index": 0,
                                "content_block": {"type": "text", "text": ""}}),
    sse("content_block_delta", {"type": "content_block_delta", "index": 0,
                                "delta": {"type": "text_delta", "text": "Hel"}}),
    sse("error", {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}),
]).encode()
KW = dict(model="claude-x", max_tokens=16, messages=[{"role": "user", "content": "hi"}], stream=True)


def report(label, raised):
    spans = exporter.get_finished_spans()
    print(f"{label} raised={raised} finished_spans={len(spans)} "
          f"status={[s.status.status_code.name for s in spans]}")
    exporter.clear()


def sync_run():
    transport = httpx.MockTransport(lambda r: httpx.Response(200, content=BODY))
    client = anthropic.Anthropic(api_key="x", http_client=httpx.Client(transport=transport))
    try:
        for _ in client.messages.create(**KW):
            pass
    except anthropic.APIError as e:
        report("sync ", type(e).__name__)


async def async_run():
    async def handler(request):
        return httpx.Response(200, content=BODY)
    transport = httpx.MockTransport(handler)
    client = anthropic.AsyncAnthropic(api_key="x", http_client=httpx.AsyncClient(transport=transport))
    try:
        async for _ in await client.messages.create(**KW):
            pass
    except anthropic.APIError as e:
        report("async", type(e).__name__)


sync_run()
asyncio.run(async_run())
👍 Expected behavior

The sync span ends with status ERROR, as the async one does.

👎 Actual Behavior with Screenshots
$ python repro.py   # main 8d9c93f, opentelemetry-instrumentation-anthropic 0.62.4, anthropic 1.13.0
sync  raised=APIStatusError finished_spans=0 status=[]
async raised=APIStatusError finished_spans=1 status=['ERROR']

$ python repro.py   # same, with the __anext__ except body copied into __next__
sync  raised=APIStatusError finished_spans=1 status=['ERROR']
async raised=APIStatusError finished_spans=1 status=['ERROR']

$ python repro_gc.py   # repro.py plus gc.collect() after sync_run(), on #4539 head 7f99923
sync  raised=APIStatusError finished_spans=0 status=[]
sync-after-gc raised=- finished_spans=1 status=['OK']
async raised=APIStatusError finished_spans=1 status=['ERROR']

$ python repro_gc.py   # #4539 head 7f99923 plus the same __next__ change
sync  raised=APIStatusError finished_spans=1 status=['ERROR']
sync-after-gc raised=- finished_spans=0 status=[]
async raised=APIStatusError finished_spans=1 status=['ERROR']
# not run: the package's pytest suite, and a real dropped connection
🤖 Python Version

3.12.6

📃 Provide any additional context for the Bug.

With #4539 from @rupak-eng applied, __del__ exports the failed sync stream at GC with status OK (last run above). The failure then reads as a success. The fix sets _instrumentation_completed in the except branch, so __del__ skips that span. #4590 covers the missing duration point on this branch, a metric gap and not span status.

👀 Have you spent some time to check if this bug has been raised before?
  • I checked and didn't find similar issue
Are you willing to submit PR?

Yes I am willing to submit a PR!

@doronkopit5, I'll open it as its own PR with a sync test that serves the error event. It stays independent of #4539, so neither waits on the other. Is that fine with you?

Dominant language
Python
Stars
7.5k
Forks
1.2k
Avg merge
2d 6h
Merged PRs (30d)
2

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from traceloop/openllmetry

All issues in traceloop/openllmetry

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.