fix(bedrock): strip cross-region inference profile prefixes for context window#6599
fix(bedrock): strip cross-region inference profile prefixes for context window#6599Solaris-star wants to merge 1 commit into
Conversation
…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>
📝 WalkthroughWalkthroughChangesBedrock context sizing
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/crewai/src/crewai/llms/providers/bedrock/completion.py (1)
2093-2106: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winApply prefix stripping to
supports_multimodaland handle case-insensitivity.The PR objective notes that the linked issue also identifies a prefix problem in
supports_multimodalfor non-us.profiles. You can fully resolve that by reusing_strip_inference_profile_prefixthere and removing the hardcodedus.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_idTo 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
📒 Files selected for processing (2)
lib/crewai/src/crewai/llms/providers/bedrock/completion.pylib/crewai/tests/llms/bedrock/test_bedrock.py
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 usedself.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:
bedrock/and geographic prefixes (us-gov.,apac.,us.,eu., …) before lookupclaude-3-5-sonnetbeatsclaude-3)Fixes #6244
Test Coverage
Local (Python 3.12 +
crewai[bedrock]), direct unit construction with mocked Session:Added
test_bedrock_cross_region_inference_profile_context_window.Checklist