【Bug已解决】AWS Bedrock: cannot invoke Anthropic Claude Sonnet 3.5 v2 model, raises error Invocation of…
【Bug已解决】AWS Bedrock cannot invoke Anthropic Claude Sonnet 3.5 v2 model, raises error Invocation of model ID with on-demand throughput isnt supported. 解决方案一、现象长什么样你在 AWS Bedrock 上调anthropic.claude-3-5-sonnet-20241022-v2:0收到Invocation of model ID with on-demand throughput isnt supported.用按需吞吐量调用该模型 ID 不被支持或ValidationException: The model ... does not support on-demand throughput in this region你用InvokeModel/Converse直接传了这个模型 ID却被告知按需不支持在部分区域region能调、换区域就报错用bedrock-runtime的modelIdbase model id失败但别人给了一个很长的 ARN 就能成你确认模型已请求访问通过仍报这个。一句话在 Bedrock 上调用 Claude Sonnet 3.5 v2 时某些模型 ID 必须走推理配置inference profile而不是基础模型 ID——直接用基础模型 ID 以按需方式调用在该区域不被支持。二、背景AWS Bedrock 对部分模型尤其新发布的、或跨区复制的引入了Inference Profile推理配置机制。原因在于为了跨多个可用区/区域提供容量与低延迟Bedrock 会把这个模型封装成一个推理配置它有自己的 ID通常是一个 ARN或带us./eu.前缀的 profile ID基础模型 ID 本身在该区域只作为被引用对象存在。于是直接用基础模型 IDanthropic.claude-3-5-sonnet-20241022-v2:0以 on-demand 方式调用 → 报按需不支持改用对应的inference profile ID / ARN调用 → 成功该要求因模型、因区域而异有的区域基础 ID 可用有的区域必须用 profile。这是 Bedrock 的容量路由策略不是权限或 key 的问题。三、根因根因是调用时用的模型 ID 类型与 Bedrock 在该区域的要求不匹配InvokeModel(modelIdanthropic.claude-3-5-sonnet-20241022-v2:0) - 该区域要求此模型走 inference profile - 基础 ID 不支持 on-demand - ValidationException: on-demand throughput isnt supported修复方向在 Bedrock 控制台找到该模型对应的inference profile用它的 ID或 ARN调用或切换到支持基础 ID on-demand 的区域。四、最小可运行复现下面用 Python 模拟基础 ID 被拒、profile ID 通过from dataclasses import dataclass from typing import Dict dataclass class _BedrockRegion: # 该区域此模型只能用 profile 调用 profile_required: Dict[str, str] None def __post_init__(self): self.profile_required self.profile_required or { anthropic.claude-3-5-sonnet-20241022-v2:0: arn:aws:bedrock:us-west-2:123:inference-profile/abcd } def invoke(self, model_id: str) - str: if model_id in self.profile_required and not model_id.startswith(arn:): raise ValueError( Invocation of model ID with on-demand throughput isnt supported. f请改用 inference profile: {self.profile_required[model_id]} ) return ok def main(): region _BedrockRegion() try: region.invoke(anthropic.claude-3-5-sonnet-20241022-v2:0) except ValueError as e: print(ERR:, e) # 改用 profile ARN print(region.invoke(arn:aws:bedrock:us-west-2:123:inference-profile/abcd)) if __name__ __main__: main()运行后基础 ID 被拒、profile ARN 通过与真实表现一致。五、解决方案第一层最小直接修复最小修复是改用该模型在对应区域的 inference profile ID/ARNimport boto3 client boto3.client(bedrock-runtime, region_nameus-west-2) # 用 inference profile ARN 代替基础模型 ID response client.invoke_model( modelIdarn:aws:bedrock:us-west-2:123456789012:inference-profile/anthropic.claude-3-5-sonnet-20241022-v2:0, contentTypeapplication/json, acceptapplication/json, bodyjson.dumps({ anthropic_version: bedrock-2023-05-31, max_tokens: 1024, messages: [{role: user, content: hi}], }), )在 Bedrock 控制台 → 模型访问 → 找到 Claude Sonnet 3.5 v2 → 复制它的Inference profile ARN用它调用即可。用ConverseAPI 同理传 profile ID。六、解决方案第二层结构化改进把模型 ID → 该区域应使用的调用 ID做成映射策略集中管理避免硬编码基础 ID 导致失败from dataclasses import dataclass, field from typing import Dict dataclass(frozenTrue) class ClaudeBedrockInferenceProfilePolicy: Bedrock 调用策略按区域解析应使用的模型 ID基础 ID / profile。 规则 - 记录 (region, base_model_id) - 实际调用 ID - 若该区域要求 profile则自动替换为 profile ARN - 未知组合报明确错误提示去控制台查 profile mapping: Dict[str, Dict[str, str]] field(default_factorylambda: { us-west-2: { anthropic.claude-3-5-sonnet-20241022-v2:0: arn:aws:bedrock:us-west-2:123456789012:inference-profile/ anthropic.claude-3-5-sonnet-20241022-v2:0, } }) def resolve(self, region: str, base_model_id: str) - str: region_map self.mapping.get(region, {}) if base_model_id in region_map: return region_map[base_model_id] # 不在映射里假定基础 ID 可直接用或用户自己保证 if base_model_id.startswith(arn:): return base_model_id # 明确提示 raise ValueError( f区域 {region} 的模型 {base_model_id} 未配置 inference profile 请到 Bedrock 控制台确认是否需使用 profile ARN 调用 ) def demo() - None: policy ClaudeBedrockInferenceProfilePolicy() print(policy.resolve(us-west-2, anthropic.claude-3-5-sonnet-20241022-v2:0)) if __name__ __main__: demo()七、解决方案第三层断言 / CI 守护import pytest from your_module import ClaudeBedrockInferenceProfilePolicy def test_profile_resolved_for_required_region(): policy ClaudeBedrockInferenceProfilePolicy() resolved policy.resolve(us-west-2, anthropic.claude-3-5-sonnet-20241022-v2:0) assert resolved.startswith(arn:aws:bedrock) def test_arn_passed_through(): policy ClaudeBedrockInferenceProfilePolicy() arn arn:aws:bedrock:us-west-2:1:inference-profile/x assert policy.resolve(us-west-2, arn) arn def test_unknown_combo_raises(): policy ClaudeBedrockInferenceProfilePolicy() with pytest.raises(ValueError): policy.resolve(eu-central-1, some.model:0) def test_resolve_returns_str(): policy ClaudeBedrockInferenceProfilePolicy() assert isinstance(policy.resolve(us-west-2, anthropic.claude-3-5-sonnet-20241022-v2:0), str)CI 里加一条对所有支持的 (region, model) 组合断言resolve返回可用调用 ID避免上线才发现 on-demand 不支持。八、排查清单你用的是基础模型 ID 还是 inference profile ID该区域可能要求 profile。是否在 Bedrock 控制台复制了该模型的Inference profile ARN换区域是否就不报错了说明是区域级 profile 要求差异。是否用了Converse/InvokeModel但模型 ID 类型不对模型访问是否已请求通过访问未批也会表现为调用失败但错误信息不同。是否在代码里把模型 ID 解析集中管理避免散落硬编码基础 ID九、小结Bedrock 上调 Claude Sonnet 3.5 v2 报Invocation of model ID with on-demand throughput isnt supported根因是该区域要求用inference profile ID/ARN而非基础模型 ID 以按需方式调用——这是 Bedrock 的容量路由策略。最小修复是到控制台复制该模型的 profile ARN 来调用或切到支持基础 ID 的区域结构化做法是抽成ClaudeBedrockInferenceProfilePolicy集中维护 (region, model) → 调用 ID 的映射最后用 pytest 守护所有组合都解析出可用 ID避免上线踩坑。