Skip to content

fix(bedrock): strip cross-region inference profile prefixes for context window#6599

Open
Solaris-star wants to merge 1 commit into
crewAIInc:mainfrom
Solaris-star:fix/6244-bedrock-cross-region-context
Open

fix(bedrock): strip cross-region inference profile prefixes for context window#6599
Solaris-star wants to merge 1 commit into
crewAIInc:mainfrom
Solaris-star:fix/6244-bedrock-cross-region-context

Conversation

@Solaris-star

Copy link
Copy Markdown

Description

AWS Bedrock cross-region inference profile model ids look like us.anthropic.claude-sonnet-4-.... BedrockCompletion.get_context_window_size() keyed its table on bare ids and used self.model.startswith(...), so prefixed profiles never matched and fell through to the 8192 default. That makes a 200K-token Claude model behave like ~8K and triggers premature summarization.

This PR:

  • strips bedrock/ and geographic prefixes (us-gov., apac., us., eu., …) before lookup
  • matches longest table prefix first (so claude-3-5-sonnet beats claude-3)

Fixes #6244

Test Coverage

Local (Python 3.12 + crewai[bedrock]), direct unit construction with mocked Session:

anthropic.claude-3-5-sonnet-... → 170000
us.anthropic.claude-sonnet-4-... → 170000
eu./apac. profiles → 170000
amazon.titan-text-express → 6800

Added test_bedrock_cross_region_inference_profile_context_window.

Checklist

  • Bug fix
  • Tests added
  • Self-reviewed

…xt window

Geographic profile ids (us./eu./apac./us-gov.) prevented startswith matches
against bare foundation-model keys, so Claude 200K models fell back to 8192.

Also match longest prefix first so claude-3-5-sonnet wins over claude-3.

Fixes crewAIInc#6244

Signed-off-by: Solaris-star <820622658@qq.com>
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Bedrock context sizing

Layer / File(s) Summary
Prefix-aware context-window lookup
lib/crewai/src/crewai/llms/providers/bedrock/completion.py, lib/crewai/tests/llms/bedrock/test_bedrock.py
BedrockCompletion strips geographic inference-profile prefixes before matching context-window entries, prioritizes longer prefixes, and tests regional model identifiers against the expected 200,000-token context size.

Suggested reviewers: lorenzejay

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: fixing Bedrock context-window lookup for cross-region inference profile prefixes.
Description check ✅ Passed The description directly describes the same Bedrock prefix fix and added regression test.
Linked Issues check ✅ Passed The PR satisfies the issue's primary context-window fallback bug by stripping geographic prefixes and adding coverage.
Out of Scope Changes check ✅ Passed The changes stay focused on Bedrock context-window prefix handling and related tests, with no obvious unrelated additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
lib/crewai/src/crewai/llms/providers/bedrock/completion.py (1)

2093-2106: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Apply prefix stripping to supports_multimodal and handle case-insensitivity.

The PR objective notes that the linked issue also identifies a prefix problem in supports_multimodal for non-us. profiles. You can fully resolve that by reusing _strip_inference_profile_prefix there and removing the hardcoded us. variations.

Additionally, standardizing the parsed model ID to lowercase prevents mismatches if a user passes mixed-casing (e.g., US.Anthropic...).

♻️ Proposed refactor to share logic and handle casing
     `@staticmethod`
     def _strip_inference_profile_prefix(model_id: str) -> str:
         """Strip geographic cross-region inference profile prefixes.
 
         AWS Bedrock inference profile ids look like
         ``us.anthropic.claude-sonnet-4-...`` / ``eu.`` / ``apac.`` / ``us-gov.``.
         Context-window tables are keyed on the bare foundation-model id.
         """
-        model_id = model_id.removeprefix("bedrock/")
+        model_id = model_id.lower().removeprefix("bedrock/")
         for geo in ("us-gov.", "apac.", "us.", "eu.", "jp.", "au.", "ca."):
             if model_id.startswith(geo):
                 return model_id[len(geo) :]
         return model_id

To complete the issue fix, you can then apply this helper to supports_multimodal() outside the current diff:

    def supports_multimodal(self) -> bool:
        ...
        model_lower = self._strip_inference_profile_prefix(self.model)
        vision_models = (
            "anthropic.claude-3",
            "anthropic.claude-sonnet-4",
            "anthropic.claude-opus-4",
            "anthropic.claude-haiku-4",
            "amazon.nova-lite",
            "amazon.nova-pro",
            "amazon.nova-premier",
            # removed the us.* duplicates
        )
        return any(model_lower.startswith(m) for m in vision_models)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/crewai/src/crewai/llms/providers/bedrock/completion.py` around lines 2093
- 2106, Update supports_multimodal to normalize self.model through
_strip_inference_profile_prefix and lowercase it before matching. Remove the
hardcoded us. model variants from its vision_models list, retaining only bare
model prefixes so all supported geographic profiles and mixed-cased IDs are
handled consistently.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@lib/crewai/src/crewai/llms/providers/bedrock/completion.py`:
- Around line 2093-2106: Update supports_multimodal to normalize self.model
through _strip_inference_profile_prefix and lowercase it before matching. Remove
the hardcoded us. model variants from its vision_models list, retaining only
bare model prefixes so all supported geographic profiles and mixed-cased IDs are
handled consistently.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9b7a5475-70d5-4b93-a6f5-55900f5e7b2a

📥 Commits

Reviewing files that changed from the base of the PR and between 69c0308 and 4ea6ca5.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/llms/providers/bedrock/completion.py
  • lib/crewai/tests/llms/bedrock/test_bedrock.py

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.

[BUG] Bedrock cross-region inference profiles get the 8192-token fallback context window

1 participant