fix(anthropic): don't fail instrumentation when anthropic>=1 removed Completions - #197
Open
balasivagn wants to merge 1 commit into
Open
fix(anthropic): don't fail instrumentation when anthropic>=1 removed Completions#197balasivagn wants to merge 1 commit into
balasivagn wants to merge 1 commit into
Conversation
…Completions AnthropicInstrumentor()._instrument()/_uninstrument() unconditionally imported anthropic.resources.completions to wrap the legacy Completions API. anthropic-sdk-python 1.0 removed that module entirely (see its MIGRATION.md, "Removed: the legacy Text Completions API"), so the import raised ImportError before Messages ever got wrapped - meaning instrument() silently traced nothing at all on anthropic>=1, not just missing completions support. Make the Completions import optional: skip wrapping it when the module isn't there, continue wrapping Messages (which is unaffected) either way. Verified against both anthropic 0.125.0 and 1.2.0 - no regression on the old SDK, working Messages instrumentation on the new one. Adds a regression test that simulates the anthropic>=1 environment via sys.modules patching (fails on the old code with the exact ImportError seen in production, passes with the fix), and skips the two existing tests that specifically exercise the legacy Completions API when it isn't importable, so the suite stays green under either SDK version instead of failing on a feature that no longer exists.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
AnthropicInstrumentor().instrument()currently raisesImportErrorand instruments nothing at all onanthropic>=1.0- not just missing legacy Completions support, but Messages too, since the failing import happens before Messages ever gets wrapped. This makes the Completions import optional so Messages instrumentation (the actively-used API) keeps working regardless of which anthropic major version is installed.Why?
anthropic-sdk-python1.0 (2026-08-20) removed the legacy Text Completions API entirely (see its MIGRATION.md, "Removed: the legacy Text Completions API"), including theanthropic.resources.completionsmodule._instrument()/_uninstrument()here import it unconditionally at the top of the method, so onanthropic>=1the whole method aborts withModuleNotFoundError: No module named 'anthropic.resources.completions'and no wrapping happens for either API.I hit this in production:
traceai-anthropic0.1.10 (latest on PyPI) paired withanthropic>=1silently produced zero LLM-call traces, with only a caughtImportErrorlogged.frameworks/anthropic/pyproject.toml'santhropic = ">=0.41.0"has no upper bound, so anyone installing fresh today gets this combination.How was it tested?
pytest)tests/test_framework_anthropic.pysuite against bothanthropic==0.125.0(last pre-1.0 release) andanthropic==1.2.0(latest) in separate venvs:anthropic<1: 9 passed, 0 skipped (no regression)anthropic>=1: 7 passed, 2 skipped (the two tests that specifically exercise the legacy Completions API - genuinely inapplicable onanthropic>=1, not a fix-related failure)test_instrument_without_legacy_completions_api, which simulates theanthropic>=1environment viasys.modulespatching rather than depending on which version happens to be installed. Confirmed it fails against the unpatched code with the exactModuleNotFoundErrorseen in production, and passes with the fix.Checklist
mainNotes for reviewers
anthropic<1, just inapplicable when the legacy API doesn't exist. Theskipifreads the actualsys.modulesstate so it self-corrects whenever this repo's own pin moves toanthropic>=1.black/isort/ruffflagged several pre-existing style issues intraceai_anthropic/__init__.pyunrelated to this change (unsorted__slots__, aCollectionimport that predatescollections.abc, an over-lengthlogger.warningline) - left those alone per "keep PRs focused and small," only formatted the lines I actually added/touched.frameworks/anthropic/pyproject.toml'santhropic = ">=0.41.0"doesn't need a version bump/cap from this PR - it's genuinely correct now that both majors work.traceai_anthropic/_stream.py'sfrom anthropic.types import CompletionisTYPE_CHECKING-only, so it doesn't hit this at runtime - no change needed there.🤖 Generated with Claude Code