Skip to content

Python: Prefer boto3 session and injected client region over BEDROCK_REGION - #9967

Open
Mehedi Hasan (Mahdi-Hasan) wants to merge 2 commits into
microsoft:mainfrom
Mahdi-Hasan:fix/9962-bedrock-session-region
Open

Mehedi Hasan (Mahdi-Hasan) wants to merge 2 commits into
microsoft:mainfrom
Mahdi-Hasan:fix/9962-bedrock-session-region

Conversation

@Mahdi-Hasan

Copy link
Copy Markdown

Motivation & Context

BedrockChatClient and BedrockEmbeddingClient read BEDROCK_REGION before the region of a caller-supplied boto3_session. A session set to eu-west-1 was sent to us-east-1 when the env var said so, with no error. An injected runtime client also reported the ambient region in client.region and service_url().

Description & Review Guide

  • What are the major changes?
    • Region now resolves in this order: injected client's meta.region_name, explicit region=, boto3_session.region_name, BEDROCK_REGION, then us-east-1.
    • Explicit region= still overrides the session, as the issue asks.
    • Embedding client: renamed a walrus variable that shadowed the region argument.
    • Docstrings updated to the new order.
  • What is the impact of these changes?
    • Callers who pass a session and also set BEDROCK_REGION to a different value now get the session's region. This is the fix the issue asks for. Callers who want the old result can pass region=.
  • What do you want reviewers to focus on?
    • The existing env-region-wins test cases encoded the old order. I replaced them with session-region-beats-env and added an env-region case for a session with no region.

Related Issue

Fixes #9962

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: implementation, tests, and this description. The issue's repro was re-run against a real boto3.Session before and after the change.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

🤖 Generated with Claude Code

…REGION

BedrockChatClient and BedrockEmbeddingClient resolved BEDROCK_REGION before
the region of a caller-supplied boto3 session, so a session configured for
another region was silently redirected. An injected runtime client also
reported the ambient region instead of its own.

Region now resolves as: injected client's region, explicit `region=`,
boto3 session region, BEDROCK_REGION, then us-east-1.

Fixes microsoft#9962

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 2, 2026 11:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Nov 2, 2026
@Mahdi-Hasan

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain, and the region-precedence changes, documentation updates, and relevant client tests are addressed across the affected Bedrock clients.

No open issues 🎉

Review effort: Lite

This branch was previously deployed

1 inactive deployment
github-app-auth — 217a4910 Deployed Nov 2, 2026 by Mahdi-Hasan via add_label #26829
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Bedrock environment region overrides a provided boto3 session

2 participants