🐛 Bug Report: Anthropic: after uninstrument(), a client that used with_raw_response / with_streaming_response stays traced through them
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- Half a day
- Newbie friendliness
- 62/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python
- Domain
- observability
Research direction
Start with the reproduction script in the issue: it instruments, uses the plain, with_raw_response and with_streaming_response accessors on one client, then uninstruments and counts spans. The cause is that the SDK's cached accessor keeps a bound reference to the instrumented create, which AnthropicInstrumentor().uninstrument() does not reset. Done when all three lines print 0 spans, including for AsyncAnthropic.
Written by the indexing model from the issue text.
Description
Which component is this bug for?
Anthropic Instrumentation
📜 Description
After AnthropicInstrumentor().uninstrument(), calls made through client.messages.with_raw_response or client.messages.with_streaming_response are still traced, if that client used the accessor while the instrumentation was active. Plain client.messages.create(...) on the same client is correctly no longer traced, and a new client is fine.
The Anthropic SDK caches these accessor objects on the client (cached_property), and the cached wrapper keeps a reference to the instrumented create it was built from. uninstrument() restores the class method, but not the bound method inside the cached accessor, so those calls keep going through the instrumentation. The same happens with AsyncAnthropic.
(Unlike #4552 for OpenAI, where uninstrument() does nothing at all, the Anthropic instrumentation does uninstrument plain calls; only the cached accessors stay instrumented.)
👟 Reproduction steps
pip install anthropic opentelemetry-instrumentation-anthropic opentelemetry-sdk
import anthropic
from importlib.metadata import version
from anthropic._base_client import httpx2 as httpx # anthropic 1.x uses httpx2
from opentelemetry.sdk.trace import SpanProcessor, TracerProvider
from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
class Count(SpanProcessor):
n = 0
def on_end(self, span): Count.n += 1
provider = TracerProvider(); provider.add_span_processor(Count())
def respond(request): # local stand-in for the Anthropic API
return httpx.Response(200, json={"id": "msg_1", "type": "message", "role": "assistant", "model": "claude-x",
"content": [{"type": "text", "text": "Hello"}], "stop_reason": "end_turn",
"usage": {"input_tokens": 3, "output_tokens": 1}})
KW = dict(model="claude-x", max_tokens=10, messages=[{"role": "user", "content": "hi"}])
print(f"opentelemetry-instrumentation-anthropic {version('opentelemetry-instrumentation-anthropic')} | anthropic {version('anthropic')}")
for accessor in ("plain", "with_raw_response", "with_streaming_response"):
AnthropicInstrumentor().instrument(tracer_provider=provider)
client = anthropic.Anthropic(api_key="x", http_client=httpx.Client(transport=httpx.MockTransport(respond)))
def call():
if accessor == "plain":
client.messages.create(**KW)
elif accessor == "with_raw_response":
client.messages.with_raw_response.create(**KW)
else:
with client.messages.with_streaming_response.create(**KW) as r:
r.parse()
call() # used while instrumented
AnthropicInstrumentor().uninstrument()
Count.n = 0
call() # the same client, after uninstrument()
print(f"{accessor:24s} spans after uninstrument(): {Count.n}")
👍 Expected behavior
After uninstrument(), no call is traced, whichever accessor it goes through: 0 spans in all three lines.
👎 Actual Behavior with Screenshots
opentelemetry-instrumentation-anthropic 0.62.4 | anthropic 1.13.0
plain spans after uninstrument(): 0
with_raw_response spans after uninstrument(): 1
with_streaming_response spans after uninstrument(): 1
🤖 Python Version
3.12
📃 Provide any additional context for the Bug.
opentelemetry-instrumentation-anthropic 0.62.4 (the latest release); anthropic 1.13.0 and 1.11.0. The same happens for AsyncAnthropic. Related: #4552 (OpenAI, uninstrument() a no-op).
👀 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?
None
- Dominant language
- Python
- Stars
- 7.5k
- Forks
- 1.2k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 2
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from traceloop/openllmetry
-
🐛 Bug Report: OpenAI Responses: with_raw_response.create() raises when the body can't be parsed, though the uninstrumented call returnsPossibly taken A pull request linked to this issue is open or already merged. Openbug python
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
traceloop/openllmetry#4717 ·
Maintainers usually reply within 1 day
-
🐛 Bug Report: Incomplete cache token migration after #4243 breaks Bedrock and Groq instrumentationPossibly taken @Quantum-Enigma-dot claimed this 21 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
traceloop/openllmetry#4665 · 1 comment ·
Maintainers usually reply within 1 day
-
🐛 Bug Report: OpenAI instrumentation drops gen_ai.usage.reasoning_tokens on streamed chat completionsPossibly taken @jayfeng7 claimed this 23 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
traceloop/openllmetry#4660 · 1 comment ·
Maintainers usually reply within 1 day
-
Bug Report: Failed dataset deletes still remove local rows and columnsPossibly taken @inchang-ing claimed this 32 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
traceloop/openllmetry#4630 ·
Maintainers usually reply within 1 day
-
🐛 Bug Report: LiteLLM instrumentation does not record seed, stop, frequency_penalty, presence_penaltyPossibly taken @shantam77 claimed this 38 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
traceloop/openllmetry#4598 · 2 comments ·
Maintainers usually reply within 1 day
All issues in traceloop/openllmetry
Similar issues
-
opensubtitlescom: provider rejects pt-PT even though its own converter maps itPossibly taken @paulo-0308 claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Diaoul/subliminal#1405 ·
-
Internal ErrorOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
frappe/frappe_docker#2071 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
The-Pocket/PocketFlow#161 ·
-
comp/desktop P2 type/bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NousResearch/hermes-agent#157702 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
zai-org/zcode-plugins#202 ·
Maintainers usually reply within 1 day