Skip to content

feat(py/anthropic): explore AmazonBedrock backend for Anthropic - #261

Closed
huangjeff5 wants to merge 2 commits into
feat/py-deprecate-vertexaifrom
exp/py-anthropic-bedrock-backend
Closed

huangjeff5 wants to merge 2 commits into
feat/py-deprecate-vertexaifrom
exp/py-anthropic-bedrock-backend

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Stack (merge in order): #257 (Anthropic(backend=GoogleCloud())) → #258 (OpenAICompatible) → #259 (OpenAICompatible(backend=GoogleCloud())) → #260 (deprecate genkit-vertexai) → #261 (Draft exploration: Anthropic(backend=AmazonBedrock())) → #263 (Draft exploration: OpenAICompatible(backend=AmazonBedrock()))

Exploration (Draft): Stress-testing the backend= pattern introduced in #257 by implementing Anthropic(backend=AmazonBedrock(...)) against AsyncAnthropicBedrock. Not intended to merge yet; meant to surface where the Backend seam holds cleanly and where AWS/Bedrock diverges from Google Cloud.

Adds an AmazonBedrock(region=..., profile=...) backend to genkit-anthropic (behind the genkit-anthropic[amazon-bedrock] extra) so Claude callers on AWS can use the native Anthropic Messages API (AsyncAnthropicBedrock) and full AnthropicConfig (adaptive thinking, effort, task_budget, prompt caching) instead of Bedrock Converse's lowest-common-denominator BedrockConfig.

from genkit import Genkit
from genkit_anthropic import AmazonBedrock, Anthropic, AnthropicConfig

# 1. Serve Claude from Amazon Bedrock Runtime using your AWS credential chain
ai = Genkit(plugins=[Anthropic(backend=AmazonBedrock(region='us-west-2'))])

# 2. Full AnthropicConfig works, using Bedrock model / inference-profile IDs
res = await ai.generate(
    model=Anthropic.claude('global.anthropic.claude-sonnet-4-6'),
    prompt='Suggest a dish for a guest with a shellfish allergy.',
    config=AnthropicConfig(thinking='adaptive', effort='medium'),
)
print(res.text)
# => Herb-Roasted Chicken with Lemon Rice

# 3. Missing AWS region fails explicitly instead of silently routing to us-east-1
# ai = Genkit(plugins=[Anthropic(backend=AmazonBedrock())])  # no AWS_REGION / ~/.aws/config
# => GenkitError: FAILED_PRECONDITION, names AmazonBedrock(region=...) and AWS_REGION

What Fit Cleanly vs. Real Friction Found

  • The Backend seam mostly held with zero core changes: Adding AmazonBedrock + AmazonBedrockBackend took one self-contained module (_amazon_bedrock.py), the amazon-bedrock extra (anthropic[bedrock]>=0.96.0,<1), and a dispatch arm in _backend_for. Like GoogleCloud, no backend types are exported from genkit core.
  • Bedrock Runtime breaks model-ID portability and static catalog listing: Unlike Google Cloud (which accepts bare Anthropic names like claude-sonnet-4-6), Bedrock Runtime requires Bedrock model IDs or cross-region inference-profile IDs (global.anthropic.claude-sonnet-4-6, us.anthropic.claude-sonnet-4-5-20250929-v1:0, global.anthropic.claude-opus-4-6-v1 with per-model suffixes). We added Backend.include_static_catalog = False so list_actions() doesn't advertise bare claude-* IDs in the Dev UI that 400 on Bedrock; note that get_model_info() also falls back to DEFAULT_SUPPORTS (prompt-based JSON instead of ConstrainedSupport.ALL) for Bedrock-formatted IDs.
  • AsyncAnthropicBedrock silently defaults to us-east-1 and blocks on boto3 auth: The SDK's _infer_region() silently falls back to us-east-1, and _prepare_request() calls boto3.Session().get_credentials() synchronously on the event loop, raising a raw RuntimeError when credentials are missing (which Retry middleware would treat as transient). Preflighting in asyncio.to_thread(load_aws_context, ...) enforces an explicit region (matching genkit-amazon-bedrock) and turns missing profiles/credentials into FAILED_PRECONDITION.
  • AsyncAnthropicBedrock.beta.messages has no .stream() method: In anthropic==0.112.0, AsyncAnthropic and AsyncAnthropicVertex expose beta.messages.stream(), but AsyncAnthropicBedrock.beta.messages exposes create() only. AnthropicModel._generate_streaming() now falls back to messages.stream() with extra_headers={'anthropic-beta': ...}, which AsyncAnthropicBedrock._prepare_options() turns into anthropic_beta in the InvokeModel JSON body.
  • Complements genkit-amazon-bedrock rather than replacing it: Unlike genkit-vertexai (which only wrapped AsyncAnthropicVertex and AsyncOpenAI and can be deprecated in feat(py): deprecate genkit-vertexai; remove it in the next release #260), genkit-amazon-bedrock covers Bedrock Converse (Nova, Llama, Mistral, Cohere, DeepSeek), Stability images, and Titan/Cohere embedders. Anthropic(backend=AmazonBedrock(...)) gives Claude users native Anthropic features on AWS billing.

Open Questions to Decide Before Moving Past Exploration

  1. bedrock-runtime (AsyncAnthropicBedrock) vs. Bedrock Mantle (AsyncAnthropicBedrockMantle): anthropic==0.112.0 already ships AsyncAnthropicBedrockMantle (https://bedrock-mantle.{region}.api.aws/anthropic), which uses standard /v1/messages + SSE, has beta.messages.stream(), and accepts cleaner anthropic.<model> IDs without regional inference-profile suffixes. Should AmazonBedrock target bedrock-runtime (broader regional/model availability today), Mantle, or distinguish the two?
  2. Model IDs & native structured output: Should AmazonBedrock stay strictly pass-through (Anthropic.claude('global.anthropic.claude-sonnet-4-6')), or normalize Bedrock Claude IDs in get_model_info() so native structured outputs (ConstrainedSupport.ALL) work without passing extra?
  3. Bedrock bearer tokens (AWS_BEARER_TOKEN_BEDROCK): Right now top-level Anthropic(backend=AmazonBedrock(), api_key=...) is rejected (mirroring GoogleCloud), while AWS_BEARER_TOKEN_BEDROCK and client_options={'api_key': ...} work without boto3 SigV4. Should api_key= stay rejected (to avoid confusion with ANTHROPIC_API_KEY) or accept a Bedrock bearer token?
  4. Mid-process expired STS/SSO credentials: _prepare() catches missing/broken creds before the first call, but if short-lived STS/SSO credentials expire hours later during _prepare_request(), AsyncAnthropicBedrock will still raise raw RuntimeError/BotoCoreError. If we ship this, Backend likely needs an optional error-mapping hook so _models.py stays boto3-free while still mapping mid-run AWS auth failures cleanly.
  5. What about OpenAICompatible(backend=AmazonBedrock(...))? AWS Bedrock Mantle also exposes an OpenAI-compatible endpoint (https://bedrock-mantle.{region}.api.aws/openai/v1). Worth deciding separately whether OpenAICompatible wants an AmazonBedrock backend for Mantle alongside genkit-amazon-bedrock.

Verification

  • uv run --no-sync pytest packages/genkit-anthropic/ tests/version_bounds_guard_test.py (489 passed, 5 live skipped)
  • uv run --no-sync ruff format --check --preview packages/genkit-anthropic/
  • uv run --no-sync ruff check --preview packages/genkit-anthropic/
  • uv run --no-sync ty check .
  • uv run --no-sync pyrefly check
  • python3 scripts/check_consistency.py and uv lock --check

… HEAD

# Conflicts:
#	packages/genkit-anthropic/pyproject.toml
#	uv.lock
@huangjeff5

Copy link
Copy Markdown
Collaborator Author

Removing Bedrock exploration from the Model Garden stack (#257 → #258 → #259 → #260). Keeping this closed draft as reference.

@huangjeff5 huangjeff5 closed this Dec 1, 2026
@huangjeff5
huangjeff5 deleted the exp/py-anthropic-bedrock-backend branch December 1, 2026 19:21
@huangjeff5
huangjeff5 removed this pull request from stack #262 December 1, 2026 19:50
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