Skip to content

feat(py/openai): explore AmazonBedrock backend for OpenAICompatible - #263

Closed
huangjeff5 wants to merge 3 commits into
exp/py-anthropic-bedrock-backendfrom
exp/py-openai-bedrock-backend
Closed

huangjeff5 wants to merge 3 commits into
exp/py-anthropic-bedrock-backendfrom
exp/py-openai-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): Companion to #261, stress-testing the backend= pattern on OpenAICompatible by implementing OpenAICompatible(backend=AmazonBedrock(...)) against Amazon Bedrock's OpenAI-compatible API. Not intended to merge yet; meant to surface how AWS SigV4, Bedrock bearer tokens, and Bedrock Runtime vs. Mantle behave behind OpenAICompatible.

Adds an AmazonBedrock(region=..., profile=...) backend to genkit-openai (behind the genkit-openai[amazon-bedrock] extra) so callers on AWS can call Bedrock's OpenAI-compatible models through OpenAICompatible, authenticating with either the AWS credential chain (SigV4) or a Bedrock bearer token (AWS_BEARER_TOKEN_BEDROCK).

# uv add 'genkit-openai[amazon-bedrock]'
from genkit import Genkit
from genkit_openai import AmazonBedrock, OpenAICompatible

# 1. Amazon Bedrock's OpenAI-compatible endpoint, authenticated via AWS SigV4 or AWS_BEARER_TOKEN_BEDROCK
bedrock = OpenAICompatible(backend=AmazonBedrock(region='us-west-2'))
ai = Genkit(plugins=[bedrock])

# 2. Same ref helper and OpenAIConfig as any OpenAICompatible server
res = await ai.generate(model=bedrock.model('openai.gpt-oss-20b-1:0'), prompt='Suggest a dish.')
print(res.text)
# => Grilled salmon with lemon and dill

# 3. Custom name= lets multiple AWS regions (or Bedrock() from genkit-amazon-bedrock) run side by side
eu = OpenAICompatible(name='bedrock-eu', backend=AmazonBedrock(region='eu-central-1'))
eu.model('openai.gpt-oss-20b-1:0')
# => ModelRef(name='bedrock-eu/openai.gpt-oss-20b-1:0', ...)

# 4. Override base_url for Bedrock Mantle (auto-switches SigV4 service to 'bedrock-mantle') or VPC PrivateLink
mantle = OpenAICompatible(
    name='mantle',
    backend=AmazonBedrock(region='us-east-2'),
    base_url='https://bedrock-mantle.us-east-2.api.aws/v1',
)

What Fit Cleanly vs. Real Friction Found

  • The _Server Protocol from feat(py): serve Model Garden's OpenAI-compatible models via OpenAICompatible(backend=GoogleCloud()) #259 scaled cleanly with zero core changes: Adding AmazonBedrock took one self-contained module (_amazon_bedrock.py), the amazon-bedrock extra (boto3>=1.37.24, botocore>=1.37.24), and one elif isinstance(backend, AmazonBedrock) branch in OpenAICompatible.__init__.
  • SigV4 signing works cleanly via AsyncOpenAI._prepare_request: Unlike anthropic, the openai Python SDK (2.21.0) has no built-in Bedrock/SigV4 client. Subclassing AsyncOpenAI (_SigV4OpenAI) to override the Stainless _prepare_request hook signs every outgoing request with botocore.auth.SigV4Auth without interfering with http_client, timeout, max_retries, or httpx.MockTransport — and fetches get_frozen_credentials() off the event loop via asyncio.to_thread, mapping mid-run expired STS/SSO BotoCoreErrors to FAILED_PRECONDITION.
  • Two Bedrock OpenAI-compatible endpoints (bedrock-runtime vs. bedrock-mantle): Defaults to AWS's recommended https://bedrock-runtime.{region}.amazonaws.com/openai/v1 (SigV4 service bedrock, no GET /models). Overriding base_url= to a bedrock-mantle.{region}.api.aws host automatically switches SigV4 signing to bedrock-mantle.
  • Explicit region required (no silent us-east-1 fallback): Matches feat(py/anthropic): explore AmazonBedrock backend for Anthropic #261 and genkit-amazon-bedrock — resolves region=, AWS_REGION, AWS_DEFAULT_REGION, then ~/.aws/config, failing loudly with FAILED_PRECONDITION when unset.
  • Namespace collision consideration with genkit-amazon-bedrock: Defaults to name='bedrock' (matching genkit-amazon-bedrock's plugin name, just as GoogleCloud defaults to 'modelgarden'). Apps running both Bedrock() (for Converse / Stability images / Titan embedders) and OpenAICompatible(backend=AmazonBedrock(...)) pass a custom name= (e.g. name='bedrock-openai').

Open Questions to Decide Before Moving Past Exploration

  1. Default namespace ('bedrock' vs. something non-colliding): Unlike genkit-vertexai (which feat(py): deprecate genkit-vertexai; remove it in the next release #260 deprecates), genkit-amazon-bedrock stays alive for Converse, Stability images, and Titan/Cohere embedders. Should OpenAICompatible(backend=AmazonBedrock(...)) default to 'bedrock' (matching GoogleCloud -> 'modelgarden') or require/pick a distinct name if people commonly register both side by side?
  2. bedrock-runtime vs. bedrock-mantle default: bedrock-runtime.{region}.amazonaws.com/openai/v1 is AWS's recommended default, while bedrock-mantle.{region}.api.aws/v1 adds GET /models, Responses API features, and Mantle-only models. Should AmazonBedrock stay on bedrock-runtime with base_url= override, or expose an explicit switch?
  3. Bedrock bearer tokens (AWS_BEARER_TOKEN_BEDROCK): Same question as feat(py/anthropic): explore AmazonBedrock backend for Anthropic #261 — right now AWS_BEARER_TOKEN_BEDROCK works automatically (skipping SigV4), while top-level api_key= and client_options={'api_key': ...} are rejected. Should api_key= stay rejected or accept a Bedrock bearer token?
  4. Relying on AsyncOpenAI._prepare_request: _prepare_request is a documented Stainless base-client hook (with a leading underscore). Confirm we're comfortable relying on it (with a contract test) vs. wrapping an httpx.Auth flow.

Verification

  • uv run --no-sync pytest packages/genkit-openai/ tests/version_bounds_guard_test.py (1,123 passed)
  • uv run --no-sync ruff format --check --preview .
  • uv run --no-sync ruff check --preview .
  • uv run --no-sync ty check .
  • uv run --no-sync pyrefly check
  • python3 scripts/check_consistency.py and uv lock --check

@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-openai-bedrock-backend branch December 1, 2026 19:21
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