🐛 Bug Report: Anthropic: a sync stream that fails mid-way never ends its span
维护者通常 1 天内回复
@lakshya-dhariwal 已经在做这个了。
开始于 2026年10月20日。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- python
- 领域
- observability, testing
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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
pip install -e packages/opentelemetry-instrumentation-anthropic anthropic==1.13.0 opentelemetry-sdkat8d9c93f.- 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?
- 主要语言
- Python
- 星标
- 7.5k
- 派生
- 1.2k
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 3
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
traceloop/openllmetry 的其他 Issue
-
🐛 Bug Report: OpenAI Responses: with_raw_response.create() raises when the body can't be parsed, though the uninstrumented call returns可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭bug python
难度 2/5 1-3 小时 新手友好度 76/100
traceloop/openllmetry#4717 ·
维护者通常 1 天内回复
-
🐛 Bug Report: Incomplete cache token migration after #4243 breaks Bedrock and Groq instrumentation可能已有人在做 @Quantum-Enigma-dot 于 21 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
traceloop/openllmetry#4665 · 1 条评论 ·
维护者通常 1 天内回复
-
🐛 Bug Report: OpenAI instrumentation drops gen_ai.usage.reasoning_tokens on streamed chat completions可能已有人在做 @jayfeng7 于 23 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
traceloop/openllmetry#4660 · 1 条评论 ·
维护者通常 1 天内回复
-
Bug Report: Failed dataset deletes still remove local rows and columns可能已有人在做 @inchang-ing 于 32 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
traceloop/openllmetry#4630 ·
维护者通常 1 天内回复
-
🐛 Bug Report: LiteLLM instrumentation does not record seed, stop, frequency_penalty, presence_penalty可能已有人在做 @shantam77 于 38 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
traceloop/openllmetry#4598 · 2 条评论 ·
维护者通常 1 天内回复
查看 traceloop/openllmetry 的全部 Issue
相似的 Issue
-
CI flake: test-windows times out spawning Node in tests/test_antigravity_node_hooks.py (develop red)未关闭area/ci P2
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 2 天内回复
-
benchmark
难度 1/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 79/100
-
难度 2/5 1-3 小时 新手友好度 77/100
mozilla-ai/cq#601 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复