Skip to content

fix(py/anthropic): allow anthropic 1.x and 2.x - #165

Closed
huangjeff5 wants to merge 2 commits into
mainfrom
fix/py-anthropic-cap
Closed

huangjeff5 wants to merge 2 commits into
mainfrom
fix/py-anthropic-cap

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

genkit-anthropic capped anthropic<1, but anthropic 1.0 shipped 2026-08-20 and 2.0 on 2026-10-21 (latest 2.10.0). Anyone already on anthropic 1.x or 2.x couldn't install genkit-anthropic or Model Garden Claude. This widens the range to anthropic>=0.98.0,<3; the plugin works on 0.98, 1.x and 2.x.

# --- BEFORE ---
# uv add genkit-anthropic 'anthropic>=1'
# => resolver error: genkit-anthropic requires anthropic>=0.96.0,<1

# --- AFTER: the same call works on anthropic 0.98, 1.17 and 2.10 ---
response = await ai.generate(
    model=Anthropic.claude('claude-sonnet-4-5'),
    prompt='Suggest a dish for a guest with a peanut allergy.',
    config={'temperature': 0.2},   # sent in the request body where the SDK no longer takes it as a keyword
)

Main Changes

  • Sampling params go where the installed SDK accepts them: anthropic 1.0 removed temperature, top_p and top_k from the messages.create() / stream() signatures. The API still accepts them in the body, so passing them as Python keywords is a TypeError. _build_params now sends any request key the SDK's create/stream don't name through extra_body, before config.extra merges, so extra still wins on conflict. On 0.98 they stay keyword arguments.
  • Transport errors match the SDK's HTTP library, detected at import: anthropic 1.0 moved from httpx to httpx2. The mid-stream catch in _models.py listed httpx exceptions only, so on 1.x a dropped stream would escape as an unmapped error. New _compat.py finds the HTTP library from the base class of the SDK's DefaultAsyncHttpxClient and exposes the matching exception tuples, without version checks.
  • Credential pinning checks both auth headers: anthropic 2.0 sends API keys as Authorization: Bearer instead of x-api-key. pinned_credential_header already checked both, so no plugin change was needed; the test helper now accepts either.
  • Tests build mocks against whichever HTTP library the SDK uses: From 1.0, the SDK rejects an httpx.AsyncClient passed as http_client with a TypeError. The genkit-anthropic and Model Garden fixtures now import httpx from genkit_anthropic._compat. This is tests only; genkit-vertexai's source doesn't touch an HTTP client.

Verification

  • pytest packages/genkit-anthropic: 391 passed, 5 skipped (live, no key) at anthropic 0.98.0 (floor), 1.17.0 (last 1.x) and 2.10.0 (lock).
  • Model Garden tests: 53 passed at each of the three versions.
  • Full suite at the lock: 7903 passed, 44 skipped. pyright, pyrefly, ty, ruff and check_consistency.py clean.

Notes

Widen the anthropic spec to >=0.98.0,<3 and lock 2.10.0.

anthropic 1.0 moved its transport to httpx2 and dropped temperature/top_p/top_k
from messages.create(). The plugin now reads both off the installed SDK in
_compat.py: mid-stream transport errors are caught from whichever httpx module
the SDK's client subclasses, and sampling fields the SDK doesn't name go in
extra_body (config.extra still wins over them).

Tests build mock transports from the same module, accept the key as either
x-api-key (<2) or Authorization: Bearer (2.x), and use context_management as
the beta-only example since speed went GA in 1.x.
… module

anthropic>=1 runs on httpx2 and rejects an httpx.AsyncClient, so the fake
Vertex endpoint takes its transport from genkit_anthropic._compat.
@huangjeff5 huangjeff5 closed this Nov 24, 2026
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