Skip to content

fix(anthropic): don't fail instrumentation when anthropic>=1 removed Completions - #197

Open
balasivagn wants to merge 1 commit into
future-agi:mainfrom
balasivagn:fix/anthropic-v1-completions-import
Open

fix(anthropic): don't fail instrumentation when anthropic>=1 removed Completions#197
balasivagn wants to merge 1 commit into
future-agi:mainfrom
balasivagn:fix/anthropic-v1-completions-import

Conversation

@balasivagn

Copy link
Copy Markdown

What does this PR do?

AnthropicInstrumentor().instrument() currently raises ImportError and instruments nothing at all on anthropic>=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-python 1.0 (2026-08-20) removed the legacy Text Completions API entirely (see its MIGRATION.md, "Removed: the legacy Text Completions API"), including the anthropic.resources.completions module. _instrument()/_uninstrument() here import it unconditionally at the top of the method, so on anthropic>=1 the whole method aborts with ModuleNotFoundError: No module named 'anthropic.resources.completions' and no wrapping happens for either API.

I hit this in production: traceai-anthropic 0.1.10 (latest on PyPI) paired with anthropic>=1 silently produced zero LLM-call traces, with only a caught ImportError logged. frameworks/anthropic/pyproject.toml's anthropic = ">=0.41.0" has no upper bound, so anyone installing fresh today gets this combination.

How was it tested?

  • Unit tests added / updated (pytest)
  • Ran the full tests/test_framework_anthropic.py suite against both anthropic==0.125.0 (last pre-1.0 release) and anthropic==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 on anthropic>=1, not a fix-related failure)
  • Added test_instrument_without_legacy_completions_api, which simulates the anthropic>=1 environment via sys.modules patching rather than depending on which version happens to be installed. Confirmed it fails against the unpatched code with the exact ModuleNotFoundError seen in production, and passes with the fix.
  • N/A integration tests / gateway behavior - this only touches the Anthropic Python framework instrumentor.

Checklist

  • Branch is off main
  • Commit messages follow Conventional Commits
  • No TODOs or commented-out code left in
  • No real API keys or secrets in the diff
  • N/A prose/VOCABULARY.md - no user-facing docs changed

Notes for reviewers

  • Left the two skipped tests' assertions untouched - they're accurate for anthropic<1, just inapplicable when the legacy API doesn't exist. The skipif reads the actual sys.modules state so it self-corrects whenever this repo's own pin moves to anthropic>=1.
  • Kept the diff scoped to the ImportError bug: black/isort/ruff flagged several pre-existing style issues in traceai_anthropic/__init__.py unrelated to this change (unsorted __slots__, a Collection import that predates collections.abc, an over-length logger.warning line) - left those alone per "keep PRs focused and small," only formatted the lines I actually added/touched.
  • frameworks/anthropic/pyproject.toml's anthropic = ">=0.41.0" doesn't need a version bump/cap from this PR - it's genuinely correct now that both majors work.
  • Also verified traceai_anthropic/_stream.py's from anthropic.types import Completion is TYPE_CHECKING-only, so it doesn't hit this at runtime - no change needed there.

🤖 Generated with Claude Code

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant