bedrock_adapter.py 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. """AWS Bedrock Converse API adapter (boto3, optional dependency).
  2. Works without API keys via the AWS credential chain, cross-region inference profiles, guardrails and
  3. control-plane model discovery. OpenAI-format messages/tools are converted to Converse on the way in
  4. and responses normalized back to OpenAI-shaped objects.
  5. """
  6. from pm import install_hint
  7. import base64
  8. import importlib
  9. import json
  10. import logging
  11. import os
  12. import re
  13. import time
  14. import traceback
  15. from contextlib import suppress
  16. from types import SimpleNamespace
  17. from typing import Any, Dict, List, Optional, Tuple
  18. from urllib.parse import urlparse
  19. import httpx
  20. from agent.errors import EmptyStreamError
  21. logger = logging.getLogger(__name__)
  22. # Keyed by (region, read_timeout): boto3 freezes its Config into the client, so two models with
  23. # different ``timeout_seconds`` overrides in one region need two clients (None = botocore default).
  24. _bedrock_runtime_client_cache: dict[tuple[str, Optional[float]], Any] = {}
  25. _bedrock_control_client_cache: dict[tuple[str, Optional[float]], Any] = {}
  26. # Routed multiplex profiles: one client per (profile home, service, region, read_timeout). boto3 freezes
  27. # the credential chain into the client too, so a region-only slot would sign profile B's calls with A's keys.
  28. _bedrock_clients_by_home: dict[tuple[str, str, str, Optional[float]], Any] = {}
  29. # botocore's own connect default, set explicitly so a long read budget never stretches the TCP/TLS
  30. # connect wait: a 600s ``request_timeout_seconds`` is for slow generations, not unreachable endpoints.
  31. _BOTO_CONNECT_TIMEOUT_SECONDS = 60
  32. # botocore session kwarg <- profile .env variable (the explicit sources of the default chain).
  33. _AWS_SCOPED_CREDENTIAL_VARS: tuple[tuple[str, str], ...] = (
  34. ("aws_access_key_id", "AWS_ACCESS_KEY_ID"), ("aws_secret_access_key", "AWS_SECRET_ACCESS_KEY"),
  35. ("aws_session_token", "AWS_SESSION_TOKEN"), ("profile_name", "AWS_PROFILE"),
  36. )
  37. def scoped_aws_session_kwargs() -> dict[str, str]:
  38. """``boto3.session.Session`` kwargs from the routed profile's secret scope, ``{}`` when unscoped.
  39. Under a HERMES_HOME override the process env holds the LAUNCH profile's ``AWS_*`` (or nothing), so
  40. every Bedrock client for a served profile must be built from that profile's own ``.env`` values.
  41. Under multiplexing a profile that sets none of its own must NOT get ``{}`` — ``boto3.Session()``
  42. would then resolve the ambient default chain (process env, ~/.aws, instance metadata), i.e. the
  43. launch context's identity, exactly the borrow the Entra adapter refuses. ``AWS_PROFILE`` counts as
  44. an explicit per-profile choice (it names an entry in the shared AWS config, like the Entra
  45. ``AZURE_CLIENT_ID``-only managed-identity opt-in)."""
  46. from hermes_constants import get_hermes_home_override
  47. if get_hermes_home_override() is None:
  48. return {}
  49. from agent.secret_scope import current_secret_scope, is_multiplex_active
  50. scope = current_secret_scope() or {}
  51. kwargs = {kw: scope[var].strip() for kw, var in _AWS_SCOPED_CREDENTIAL_VARS
  52. if (scope.get(var) or "").strip()}
  53. # Under multiplexing a partial set is not enough: boto3 fills whatever is missing from the
  54. # ambient chain (process env, ~/.aws, instance metadata), i.e. the launch context's identity —
  55. # the same borrow the Entra adapter refuses. Require a COMPLETE credential: the key pair, or
  56. # AWS_PROFILE naming an entry in the shared AWS config (an explicit per-profile choice, like
  57. # the Entra AZURE_CLIENT_ID-only managed-identity opt-in).
  58. complete = ("aws_access_key_id" in kwargs and "aws_secret_access_key" in kwargs) \
  59. or "profile_name" in kwargs
  60. if not complete and is_multiplex_active():
  61. raise RuntimeError(
  62. "Bedrock auth is refused for this profile: it sets no complete AWS credential of its "
  63. "own, and under multiplexed profiles the ambient default chain (process env, ~/.aws, "
  64. "instance metadata) would sign this profile's calls with the LAUNCH context's identity. "
  65. "Set AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY in this profile's own config, or "
  66. "AWS_PROFILE to name a shared AWS config profile."
  67. )
  68. return kwargs
  69. # Geo/global cross-Region inference-profile prefixes Bedrock prepends to a model ID.
  70. _BEDROCK_PROFILE_PREFIXES: tuple[str, ...] = ("global", "us", "eu", "apac", "ap", "au", "jp", "ca", "sa", "me", "af")
  71. # The GPT-5.6 Terra/Luna cards also document India geo profiles (``in.openai.gpt-5.6-terra``).
  72. _BEDROCK_OPENAI_PROFILE_PREFIXES: tuple[str, ...] = _BEDROCK_PROFILE_PREFIXES + ("in",)
  73. # Bedrock-hosted OpenAI GPT models (bare in-Region IDs). The bare ID is served by the Bedrock Mantle
  74. # OpenAI-compatible endpoint; its geo/global profile form is served by bedrock-runtime, where these
  75. # cards list Converse as supported. Narrow allowlist so GPT-OSS models stay on the native path.
  76. BEDROCK_OPENAI_RESPONSES_MODEL_IDS: tuple[str, ...] = (
  77. "openai.gpt-6-astra", "openai.gpt-6.1-sol", "openai.gpt-6.1-terra", "openai.gpt-6-sol", "openai.gpt-6-luna",
  78. "openai.gpt-5.5", "openai.gpt-5.6-sol", "openai.gpt-5.6-terra", "openai.gpt-5.6-luna", "openai.gpt-5.4",
  79. )
  80. _BEDROCK_OPENAI_MODEL_IDS_LOWER = frozenset(m.lower() for m in BEDROCK_OPENAI_RESPONSES_MODEL_IDS)
  81. _BEDROCK_OPENAI_HOST_RE = re.compile(r"^bedrock-mantle\.([a-z0-9-]+)\.api\.aws$", re.IGNORECASE)
  82. # Bedrock-hosted xAI Grok (any regional inference-profile prefix) rejects temperature/topP in Converse
  83. # with a hard 400 ("This model doesn't support the temperature field"); reasoning-first, same
  84. # restriction as Claude Opus 4.6+ but _forbids_sampling_params is Claude-only, so it needs its own gate.
  85. _BEDROCK_XAI_GROK_NO_SAMPLING_RE = re.compile(r"^(?:[a-z]+\.)?xai\.grok", re.IGNORECASE)
  86. _MIN_BOTO3_VERSION = (1, 34, 59)
  87. def _require_boto3():
  88. """Import boto3; converse_stream() needs >= 1.34.59 (a system boto3 can shadow the venv pin)."""
  89. install_error = None
  90. try:
  91. # boto3 left [all] (PRs #24220, #24515); PM installs the [bedrock] extra on first use. This
  92. # runs at the first client build, never at import: an import-time sync would rebuild the
  93. # dependency environment of whatever process happens to import this module.
  94. try:
  95. from pm import ensure_import
  96. ensure_import("bedrock")
  97. except Exception as exc: # the import below decides; exc explains a miss
  98. logger.warning("boto3 lazy install did not complete: %s", exc)
  99. install_error = exc
  100. import boto3
  101. except ImportError:
  102. # A completed install that needs a restart must not be reported as "install it".
  103. reason = f": {install_error}" if install_error else f". Run: {install_hint('bedrock')}"
  104. raise ImportError(f"The 'boto3' package is required for the AWS Bedrock provider{reason}") from install_error
  105. try:
  106. version = tuple(int(x) for x in boto3.__version__.split(".")[:3])
  107. except (AttributeError, ValueError):
  108. return boto3 # can't parse — don't block on version check
  109. if version < _MIN_BOTO3_VERSION:
  110. raise RuntimeError(
  111. f"boto3 {boto3.__version__} does not support converse_stream "
  112. f"(minimum 1.34.59 required). Run: hermes pm repair"
  113. )
  114. return boto3
  115. def _new_client(factory: Any, service: str, region: str, read_timeout: Optional[float]):
  116. """``factory.client(...)`` with botocore's own timeouts (60s read / 60s connect) unless a
  117. request timeout is configured. The model-call client makes one attempt per call: Hermes's
  118. retry loop owns retries and backoff (throttles classify as rate_limit there), and botocore's
  119. default of 5 attempts re-sends a read timeout, so a hung call took ~5x the timeout to fail.
  120. ``total_max_attempts`` counts the first request; ``max_attempts`` would count retries only."""
  121. from botocore.config import Config
  122. config = Config(retries={"total_max_attempts": 1}) if service == "bedrock-runtime" else Config()
  123. if read_timeout is not None:
  124. config = config.merge(Config(read_timeout=read_timeout, connect_timeout=_BOTO_CONNECT_TIMEOUT_SECONDS))
  125. return factory.client(service, region_name=region, config=config)
  126. def _cached_client(cache: dict[tuple[str, Optional[float]], Any], service: str, region: str, *,
  127. read_timeout: Optional[float] = None):
  128. """Get or create a boto3 client per (region, read timeout). Unscoped: the default credential
  129. chain. Routed profile: one client per (home, service, region, read timeout), built from that
  130. profile's scoped ``AWS_*`` (falling back to the default chain only for what the profile does not set)."""
  131. from hermes_constants import get_hermes_home_override, hermes_home_key
  132. if get_hermes_home_override() is None:
  133. key = (region, read_timeout)
  134. if key not in cache:
  135. cache[key] = _new_client(_require_boto3(), service, region, read_timeout)
  136. return cache[key]
  137. home_key = (hermes_home_key(), service, region, read_timeout)
  138. client = _bedrock_clients_by_home.get(home_key)
  139. if client is None:
  140. # Scope check first: a cred-less multiplex profile must hit the ambient-chain
  141. # refusal, not a boto3 ImportError on hosts that lack the package.
  142. kwargs = scoped_aws_session_kwargs()
  143. client = _new_client(_require_boto3().Session(**kwargs), service, region, read_timeout)
  144. _bedrock_clients_by_home[home_key] = client
  145. return client
  146. def _get_bedrock_runtime_client(region: str, model: Optional[str] = None):
  147. """The ``bedrock-runtime`` client for *region* whose read timeout is *model*'s configured
  148. request timeout (``providers.bedrock.models.<model>.timeout_seconds`` over
  149. ``providers.bedrock.request_timeout_seconds``). Resolved per call, so a model switch or a
  150. fallback to another Bedrock model gets that model's budget, not the one the session started with."""
  151. from hermes_cli.timeouts import get_provider_request_timeout
  152. return _cached_client(_bedrock_runtime_client_cache, "bedrock-runtime", region,
  153. read_timeout=get_provider_request_timeout("bedrock", model))
  154. def _get_bedrock_control_client(region: str):
  155. return _cached_client(_bedrock_control_client_cache, "bedrock", region)
  156. def reset_client_cache():
  157. """Clear cached boto3 clients. Used in tests and profile switches."""
  158. _bedrock_runtime_client_cache.clear()
  159. _bedrock_control_client_cache.clear()
  160. _bedrock_clients_by_home.clear()
  161. _inference_profile_model_cache.clear()
  162. _DEFAULT_WINDOW_WARNED.clear()
  163. def invalidate_runtime_client(region: str) -> bool:
  164. """Evict one region's cached ``bedrock-runtime`` clients, every read timeout (stale HTTP pool);
  165. True if any was evicted."""
  166. from hermes_constants import get_hermes_home_override, hermes_home_key
  167. if get_hermes_home_override() is not None:
  168. cache: dict = _bedrock_clients_by_home
  169. prefix = (hermes_home_key(), "bedrock-runtime", region)
  170. else:
  171. cache, prefix = _bedrock_runtime_client_cache, (region,)
  172. stale = [key for key in list(cache) if key[:len(prefix)] == prefix]
  173. for key in stale:
  174. cache.pop(key, None)
  175. return bool(stale)
  176. # --- Bedrock Mantle / OpenAI Responses support ---
  177. def parse_bedrock_openai_model_id(model_id: str) -> Optional[tuple[str, str]]:
  178. """``(bare_id, profile_prefix)`` for a Bedrock-hosted OpenAI model, else None. ``profile_prefix``
  179. is ``""`` for the bare in-Region ID and e.g. ``"us"`` / ``"global"`` for a cross-Region profile."""
  180. normalized = str(model_id or "").strip().lower()
  181. prefix, sep, rest = normalized.partition(".")
  182. if sep and prefix in _BEDROCK_OPENAI_PROFILE_PREFIXES and rest.startswith("openai."):
  183. normalized = rest
  184. else:
  185. prefix = ""
  186. return (normalized, prefix) if normalized in _BEDROCK_OPENAI_MODEL_IDS_LOWER else None
  187. def is_openai_bedrock_model(model_id: str) -> bool:
  188. """True for Bedrock-hosted OpenAI GPT models in any endpoint form (GPT-OSS excluded)."""
  189. return parse_bedrock_openai_model_id(model_id) is not None
  190. def bedrock_openai_uses_mantle(model_id: str) -> bool:
  191. """True when the model is served by Mantle's OpenAI Responses endpoint: the bare in-Region ID.
  192. A geo/global profile ID is a bedrock-runtime ID (Mantle rejects it) and rides Converse."""
  193. parsed = parse_bedrock_openai_model_id(model_id)
  194. return parsed is not None and not parsed[1]
  195. def merge_bedrock_openai_model_ids(model_ids: list[str]) -> list[str]:
  196. """Append Mantle-only OpenAI models, which control-plane discovery never lists."""
  197. merged = list(model_ids or [])
  198. seen = {str(m).lower() for m in merged}
  199. return merged + [m for m in BEDROCK_OPENAI_RESPONSES_MODEL_IDS if m.lower() not in seen]
  200. def bedrock_openai_base_url(region: str) -> str:
  201. """Return Bedrock Mantle's OpenAI-compatible base URL for *region*."""
  202. resolved = (region or "").strip() or resolve_bedrock_runtime_region()
  203. return f"https://bedrock-mantle.{resolved}.api.aws/openai/v1"
  204. def _mantle_url_parts(base_url: str) -> tuple[Optional[str], str]:
  205. """(region or None if not a Mantle host, normalized path) for a base URL."""
  206. parsed = urlparse(str(base_url or ""))
  207. match = _BEDROCK_OPENAI_HOST_RE.match(parsed.hostname or "")
  208. return (match.group(1) if match else None), (parsed.path or "").rstrip("/").lower()
  209. def bedrock_openai_region_from_base_url(base_url: str) -> Optional[str]:
  210. """Extract the AWS region from a Bedrock Mantle OpenAI base URL."""
  211. return _mantle_url_parts(base_url)[0]
  212. def is_bedrock_openai_base_url(base_url: str) -> bool:
  213. """True for Bedrock Mantle endpoints (bare host or /openai[/v1] path)."""
  214. region, path = _mantle_url_parts(base_url)
  215. return region is not None and path in {"", "/openai", "/openai/v1"}
  216. def resolve_bedrock_bearer_token(env: Optional[dict[str, str]] = None) -> str:
  217. """Return AWS_BEARER_TOKEN_BEDROCK when Bedrock API-key auth is configured.
  218. Under a HERMES_HOME override the read goes through the profile secret scope so a
  219. served profile never inherits the launch profile's bearer from the process env."""
  220. if env is not None:
  221. return (env.get("AWS_BEARER_TOKEN_BEDROCK", "") or "").strip()
  222. from hermes_constants import get_hermes_home_override
  223. if get_hermes_home_override() is not None:
  224. from agent.secret_scope import get_secret
  225. return (get_secret("AWS_BEARER_TOKEN_BEDROCK", "") or "").strip()
  226. return (os.environ.get("AWS_BEARER_TOKEN_BEDROCK", "") or "").strip()
  227. class BedrockOpenAISigV4Auth(httpx.Auth):
  228. """httpx auth hook that SigV4-signs Bedrock Mantle OpenAI requests."""
  229. requires_request_body = True
  230. def __init__(self, region: str, service: str = "bedrock"):
  231. self.region = (region or "").strip() or resolve_bedrock_runtime_region()
  232. self.service = service
  233. def auth_flow(self, request): # pragma: no cover - exercised by live call
  234. from botocore.auth import SigV4Auth
  235. from botocore.awsrequest import AWSRequest
  236. kwargs = scoped_aws_session_kwargs()
  237. credentials = _require_boto3().Session(**kwargs).get_credentials()
  238. if credentials is None:
  239. raise RuntimeError(
  240. "No AWS credentials available for Bedrock OpenAI Responses. "
  241. "Configure AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY, AWS_PROFILE, SSO, or an instance/task role."
  242. )
  243. # SigV4 must own Authorization: drop the SDK's placeholder bearer header.
  244. headers = {str(k): str(v) for k, v in request.headers.items()
  245. if str(k).lower() not in {"authorization", "x-amz-date", "x-amz-security-token"}}
  246. aws_request = AWSRequest(method=request.method, url=str(request.url), data=request.content or b"", headers=headers)
  247. SigV4Auth(credentials.get_frozen_credentials(), self.service, self.region).add_auth(aws_request)
  248. request.headers.update(dict(aws_request.headers.items()))
  249. yield request
  250. def build_bedrock_openai_http_client(region: str, *, timeout: Optional[float] = None):
  251. """Build an httpx client that SigV4-signs Bedrock OpenAI requests."""
  252. kwargs: dict[str, Any] = {"auth": BedrockOpenAISigV4Auth(region)}
  253. if isinstance(timeout, (int, float)) and not isinstance(timeout, bool) and timeout > 0:
  254. kwargs["timeout"] = timeout
  255. return httpx.Client(**kwargs)
  256. def configure_bedrock_openai_client_kwargs(client_kwargs: dict[str, Any], *, timeout: Optional[float] = None) -> dict[str, Any]:
  257. """Install SigV4 auth on OpenAI SDK kwargs for Bedrock Mantle; a real API key keeps the SDK's
  258. bearer auth, the ``aws-sdk``/``no-key-required`` placeholders mean IAM chain auth."""
  259. base_url = str(client_kwargs.get("base_url") or "")
  260. api_key = client_kwargs.get("api_key")
  261. if not is_bedrock_openai_base_url(base_url) or (
  262. isinstance(api_key, str) and api_key.strip() and api_key not in {"aws-sdk", "no-key-required"}
  263. ):
  264. return client_kwargs
  265. region = bedrock_openai_region_from_base_url(base_url) or resolve_bedrock_runtime_region()
  266. client_kwargs["api_key"] = "aws-sdk"
  267. client_kwargs["http_client"] = build_bedrock_openai_http_client(region, timeout=timeout)
  268. return client_kwargs
  269. # --- Stale-connection detection ---
  270. # A pooled connection killed under boto3 (NAT timeout, VPN flap, RST) surfaces as a botocore/urllib3
  271. # transport error or a bare AssertionError from urllib3's pool checks; retrying the same client
  272. # reproduces it, so the fix is to evict the client.
  273. _STALE_LIB_MODULE_PREFIXES = ("urllib3.", "botocore.", "boto3.")
  274. def _stale_error_types() -> tuple:
  275. """botocore + urllib3 transport-failure exception classes (best-effort import)."""
  276. types: list = []
  277. for module, names in (
  278. ("botocore.exceptions", ("ConnectionError", "HTTPClientError")),
  279. ("urllib3.exceptions", ("ProtocolError", "NewConnectionError", "ConnectionError")),
  280. ):
  281. # AttributeError too: ``from mod import Name`` raised ImportError for a missing name.
  282. with suppress(ImportError, AttributeError): # pragma: no cover — both present with boto3
  283. types += [getattr(importlib.import_module(module), name) for name in names]
  284. return tuple(types)
  285. def is_stale_connection_error(exc: BaseException) -> bool:
  286. """True for botocore/urllib3 transport errors or AssertionErrors raised inside those libs."""
  287. if isinstance(exc, _stale_error_types()):
  288. return True
  289. return isinstance(exc, AssertionError) and any(
  290. (frame.f_globals.get("__name__", "") or "").startswith(_STALE_LIB_MODULE_PREFIXES)
  291. for frame, _lineno in traceback.walk_tb(exc.__traceback__)
  292. )
  293. def is_streaming_access_denied_error(exc: BaseException) -> bool:
  294. """True when IAM denied ``bedrock:InvokeModelWithResponseStream`` (permanent: callers fall back
  295. to converse()). Message-based: the AnthropicBedrock SDK wraps the response but keeps the action name."""
  296. msg = str(exc).lower()
  297. if "invokemodelwithresponsestream" not in msg:
  298. return False
  299. with suppress(ImportError): # pragma: no cover — botocore always present with boto3
  300. from botocore.exceptions import ClientError
  301. if isinstance(exc, ClientError):
  302. code = (getattr(exc, "response", None) or {}).get("Error", {}).get("Code", "")
  303. return code in ("AccessDeniedException", "UnauthorizedException")
  304. return "not authorized" in msg or "accessdenied" in msg
  305. # --- AWS credential detection ---
  306. # Priority order; the first group whose vars are ALL set names the auth source.
  307. _AWS_AUTH_ENV_CHAIN: tuple[tuple[str, ...], ...] = (
  308. ("AWS_BEARER_TOKEN_BEDROCK",), # Bedrock bearer token
  309. ("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"), # explicit IAM key pair
  310. ("AWS_PROFILE",), # named profile (SSO, assume-role)
  311. ("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI",), # ECS / CodeBuild
  312. ("AWS_WEB_IDENTITY_TOKEN_FILE",), # EKS IRSA
  313. )
  314. def _boto3_chain_has_credentials(*, instance_metadata: bool = True) -> bool:
  315. """True if boto3's default chain resolves credentials (IMDS, task role, ...). *instance_metadata*
  316. False skips the EC2 metadata lookup (169.254.169.254), which off EC2 only costs its timeout."""
  317. with suppress(Exception):
  318. import botocore.session
  319. session = botocore.session.get_session()
  320. if not instance_metadata:
  321. session.set_config_variable("ec2_metadata_disabled", "true") # this session only, not os.environ
  322. credentials = session.get_credentials()
  323. resolved = credentials.get_frozen_credentials() if credentials is not None else None
  324. return bool(resolved and resolved.access_key)
  325. return False
  326. def resolve_aws_auth_env_var(
  327. env: Optional[dict[str, str]] = None, *, instance_metadata: bool = True,
  328. ) -> Optional[str]:
  329. """Name of the active AWS auth source: env vars first (no I/O), then ``"iam-role"`` via boto3's chain, else None."""
  330. env = env if env is not None else os.environ
  331. for group in _AWS_AUTH_ENV_CHAIN:
  332. if all(env.get(var, "").strip() for var in group):
  333. return group[0]
  334. return "iam-role" if _boto3_chain_has_credentials(instance_metadata=instance_metadata) else None
  335. def has_aws_credentials(env: Optional[dict[str, str]] = None, *, instance_metadata: bool = True) -> bool:
  336. """True if any AWS credential source (env vars or boto3 chain) is detected.
  337. This two-tier approach mirrors the pattern from OpenClaw PR #62673: cloud environments (EC2, ECS,
  338. Lambda) provide credentials via instance metadata, not environment variables. The env-var check is a
  339. fast path for local development; the boto3 fallback covers all cloud deployments.
  340. """
  341. return resolve_aws_auth_env_var(env, instance_metadata=instance_metadata) is not None
  342. def resolve_bedrock_region(env: Optional[dict[str, str]] = None) -> str:
  343. """AWS_REGION → AWS_DEFAULT_REGION → botocore configured region (~/.aws/config profiles) → us-east-1."""
  344. env = env if env is not None else os.environ
  345. explicit = env.get("AWS_REGION", "").strip() or env.get("AWS_DEFAULT_REGION", "").strip()
  346. if explicit:
  347. return explicit
  348. with suppress(Exception):
  349. import botocore.session
  350. return botocore.session.get_session().get_config_variable("region") or "us-east-1"
  351. return "us-east-1"
  352. def resolve_bedrock_runtime_region(config: Optional[dict[str, Any]] = None) -> str:
  353. """``bedrock.region`` from config.yaml, else :func:`resolve_bedrock_region`. Every non-runtime Bedrock
  354. endpoint must use this so auxiliary calls never leave the primary runtime's region. *config* skips disk."""
  355. if config is None:
  356. with suppress(Exception):
  357. from hermes_cli.config import load_config_readonly
  358. config = load_config_readonly()
  359. cfg_region = str(((config or {}).get("bedrock") or {}).get("region") or "").strip()
  360. return cfg_region or resolve_bedrock_region()
  361. def bedrock_region_from_runtime_url(base_url: str) -> str:
  362. """AWS region from a ``bedrock-runtime.<region>.amazonaws.com`` URL (default us-east-1)."""
  363. m = re.search(r"bedrock-runtime\.([a-z0-9-]+)\.", base_url or "")
  364. return m.group(1) if m else "us-east-1"
  365. def bedrock_guardrail_config(config: Optional[dict[str, Any]] = None) -> Optional[dict[str, Any]]:
  366. """Converse ``guardrailConfig`` from ``bedrock.guardrail`` in config.yaml (None when unset)."""
  367. if config is None:
  368. config = {}
  369. with suppress(Exception):
  370. from hermes_cli.config import load_config_readonly
  371. config = load_config_readonly()
  372. gr = ((config or {}).get("bedrock") or {}).get("guardrail") or {}
  373. if not (gr.get("guardrail_identifier") and gr.get("guardrail_version")):
  374. return None
  375. out = {"guardrailIdentifier": gr["guardrail_identifier"], "guardrailVersion": gr["guardrail_version"]}
  376. for src, dst in (("stream_processing_mode", "streamProcessingMode"), ("trace", "trace")):
  377. if gr.get(src):
  378. out[dst] = gr[src]
  379. return out
  380. def bedrock_guardrail_headers(config: Optional[dict[str, Any]] = None) -> dict[str, str]:
  381. """InvokeModel/Messages-wire form of the configured guardrail. The AnthropicBedrock SDK speaks
  382. InvokeModel, which has no ``guardrailConfig`` body field; Bedrock reads the guardrail from these
  383. headers instead (same enforcement, keeps prompt caching / thinking / 1M context)."""
  384. gr = bedrock_guardrail_config(config)
  385. if not gr:
  386. return {}
  387. headers = {
  388. "X-Amzn-Bedrock-GuardrailIdentifier": str(gr["guardrailIdentifier"]),
  389. "X-Amzn-Bedrock-GuardrailVersion": str(gr["guardrailVersion"]),
  390. }
  391. if str(gr.get("trace", "")).lower() in {"enabled", "enabled_full", "true"}:
  392. headers["X-Amzn-Bedrock-Trace"] = "ENABLED"
  393. return headers
  394. GUARDRAIL_ACTION_FIELD = "amazon-bedrock-guardrailAction"
  395. def anthropic_response_guardrail_intervened(response: Any) -> bool:
  396. """True when Bedrock substituted the InvokeModel reply with guardrail messaging. Unlike Converse
  397. (``stopReason=guardrail_intervened``), InvokeModel keeps ``stop_reason=end_turn`` and signals the
  398. block only via an unmodelled body field the Anthropic SDK keeps in ``model_extra``."""
  399. extra = getattr(response, "model_extra", None) or {}
  400. return str(extra.get(GUARDRAIL_ACTION_FIELD, "")).upper() == "INTERVENED"
  401. def bind_bedrock_runtime(agent, base_url: str, api_mode: str) -> None:
  402. """Point *agent* at a non-Mantle Bedrock wire: ``bedrock_converse`` (boto3 direct, no SDK client) or
  403. ``anthropic_messages`` (AnthropicBedrock SDK, SigV4 via the boto3 chain). ``aws-sdk`` is a sentinel,
  404. never a credential, so the generic Anthropic/OpenAI client builders must not see it. Startup and every
  405. later rebuild (/model switch, fallback restore, fallback-to-Bedrock) share this so region and guardrail
  406. state never lag the active endpoint."""
  407. agent._bedrock_region = bedrock_region_from_runtime_url(base_url)
  408. agent._bedrock_guardrail_config = bedrock_guardrail_config()
  409. agent.client = None
  410. agent._client_kwargs = {}
  411. agent.api_key = agent._anthropic_api_key = "aws-sdk"
  412. agent._anthropic_base_url = base_url
  413. agent._is_anthropic_oauth = False
  414. if api_mode == "anthropic_messages":
  415. from agent.anthropic_adapter import build_anthropic_bedrock_client
  416. from hermes_cli.timeouts import get_provider_request_timeout
  417. agent._anthropic_client = build_anthropic_bedrock_client(
  418. agent._bedrock_region, timeout=get_provider_request_timeout("bedrock", getattr(agent, "model", None)))
  419. else:
  420. agent._anthropic_client = None
  421. def bedrock_model_ids_or_none() -> Optional[list[str]]:
  422. """Live-discover Bedrock model IDs; None on failure/empty so callers use the static list."""
  423. with suppress(Exception):
  424. discovered = discover_bedrock_models(resolve_bedrock_runtime_region())
  425. if discovered:
  426. return merge_bedrock_openai_model_ids([m["id"] for m in discovered])
  427. return None
  428. # --- Tool-calling / prompt-cache capability detection ---
  429. # Models known to reject toolConfig with a ValidationException; unknown models assumed OK.
  430. _NON_TOOL_CALLING_PATTERNS = [
  431. "deepseek.r1", "deepseek-r1", # DeepSeek R1 (both ID formats) — reasoning only
  432. "stability.", # image generation
  433. "cohere.embed", "amazon.titan-embed", # embeddings
  434. ]
  435. # cachePoint allowlist — inverted policy vs tools: unknown models get NO cache markers (they reject
  436. # cachePoint). Claude only reaches build_converse_kwargs under bearer auth.
  437. _CACHE_POINT_PATTERNS = ["anthropic.claude", "amazon.nova"]
  438. def _model_supports_tool_use(model_id: str) -> bool:
  439. """False for denylisted models; unknown models default to True."""
  440. return not any(pattern in model_id.lower() for pattern in _NON_TOOL_CALLING_PATTERNS)
  441. def _model_supports_prompt_cache(model_id: str) -> bool:
  442. # An application-inference-profile ARN names no model: match on the wrapped model (cached lookup).
  443. if _APPLICATION_PROFILE_ARN_RE.search(model_id):
  444. model_id = _resolve_inference_profile_model_id(model_id)
  445. return any(pattern in model_id.lower() for pattern in _CACHE_POINT_PATTERNS)
  446. # --- Server-verdict cachePoint suppression ---
  447. # Bedrock's cachePoint rule is per-family AND per-field (Nova accepts it in system/messages but hard-fails
  448. # on toolConfig.tools) and any static table drifts, so when Bedrock names a placement as unpermitted we
  449. # record the verdict, drop the marker there for the rest of the process, and retry once without it.
  450. CACHE_POINT_PLACEMENTS = ("tools", "system", "messages")
  451. _CACHE_POINT_REJECTIONS: dict[str, set] = {} # model_id (lowercased) → placements Bedrock has rejected this process
  452. # e.g. "#/toolConfig/tools/18: extraneous key [cachePoint] is not permitted"
  453. _CACHE_POINT_PATH_PATTERN = re.compile(r"#/(?P<path>[A-Za-z0-9_./\[\]-]*)", re.IGNORECASE)
  454. _CACHE_POINT = {"cachePoint": {"type": "default"}}
  455. def cache_point_rejection_placement(exc: BaseException) -> Optional[str]:
  456. """Converse section whose cachePoint Bedrock refused, or None. Message-based: the JSON pointer in the
  457. ValidationException is the only thing naming the section (raw or SDK-wrapped). Unlocalisable → "tools"."""
  458. msg = str(exc)
  459. lowered = msg.lower()
  460. if "cachepoint" not in lowered or ("not permitted" not in lowered and "extraneous" not in lowered):
  461. return None
  462. match = _CACHE_POINT_PATH_PATTERN.search(msg)
  463. path = (match.group("path") if match else "").lower()
  464. if "toolconfig" in path or "tools" in path:
  465. return "tools"
  466. return next((placement for placement in ("system", "messages") if placement in path), "tools")
  467. def note_cache_point_rejection(model_id: str, placement: str) -> None:
  468. """Record that ``model_id`` refuses cachePoint blocks in ``placement``."""
  469. if placement in CACHE_POINT_PLACEMENTS:
  470. _CACHE_POINT_REJECTIONS.setdefault(model_id.lower(), set()).add(placement)
  471. def cache_point_allowed(model_id: str, placement: str) -> bool:
  472. """False once Bedrock has refused this placement for this model."""
  473. return placement not in _CACHE_POINT_REJECTIONS.get(model_id.lower(), ())
  474. def reset_cache_point_rejections() -> None:
  475. """Clear recorded cachePoint rejections. Used in tests."""
  476. _CACHE_POINT_REJECTIONS.clear()
  477. def _without_cache_points(blocks: Any) -> Optional[list]:
  478. """``blocks`` minus cachePoint entries, or None if not a list / nothing removed."""
  479. if not isinstance(blocks, list):
  480. return None
  481. cleaned = [b for b in blocks if not (isinstance(b, dict) and set(b.keys()) == {"cachePoint"})]
  482. return None if len(cleaned) == len(blocks) else cleaned
  483. def strip_cache_points(kwargs: dict[str, Any], placement: str) -> dict[str, Any]:
  484. """Copy of Converse kwargs with ``placement``'s cachePoint removed; the SAME object
  485. back when nothing was stripped (callers use identity to decide a retry cannot help)."""
  486. if placement == "messages":
  487. messages = kwargs.get("messages")
  488. cleaned_contents = [
  489. _without_cache_points(msg.get("content") if isinstance(msg, dict) else None) for msg in messages
  490. ] if isinstance(messages, list) else []
  491. if all(content is None for content in cleaned_contents):
  492. return kwargs
  493. return {**kwargs, "messages": [
  494. msg if content is None else {**msg, "content": content} for msg, content in zip(messages, cleaned_contents)
  495. ]}
  496. if placement == "system":
  497. cleaned = _without_cache_points(kwargs.get("system"))
  498. return kwargs if cleaned is None else {**kwargs, "system": cleaned}
  499. if placement == "tools":
  500. tool_config = kwargs.get("toolConfig")
  501. cleaned = _without_cache_points((tool_config or {}).get("tools"))
  502. return kwargs if cleaned is None else {**kwargs, "toolConfig": {**tool_config, "tools": cleaned}}
  503. return kwargs
  504. def recover_from_cache_point_rejection(exc: BaseException, kwargs: dict[str, Any]) -> Optional[dict[str, Any]]:
  505. """Record Bedrock's cachePoint verdict and return retry kwargs, or None when the error
  506. was not a cachePoint rejection / the marker was already absent (caller re-raises)."""
  507. placement = cache_point_rejection_placement(exc)
  508. if placement is None:
  509. return None
  510. retry_kwargs = strip_cache_points(kwargs, placement)
  511. if retry_kwargs is kwargs:
  512. return None
  513. model_id = str(kwargs.get("modelId", ""))
  514. note_cache_point_rejection(model_id, placement)
  515. logger.warning(
  516. "bedrock: %s rejected a cachePoint block in %s — dropping that cache marker for this model and "
  517. "retrying. Prompt caching stays active for the remaining sections.", model_id or "model", placement,
  518. )
  519. return retry_kwargs
  520. # --- Encrypted-content redacted-reasoning suppression ---
  521. # Redacted thinking blobs are sealed to the issuing model/flow; replaying them after a model switch or
  522. # across regions fails with an encrypted-content ValidationException. Drop the redacted blocks and
  523. # resend once (mirrors the cachePoint self-heal above: same-object return means no retry can help).
  524. _REDACTED_REASONING_REJECTION_PATTERN = re.compile(
  525. r"ValidationException.*(?:redacted|encrypt)", re.IGNORECASE | re.DOTALL,
  526. )
  527. def _without_redacted_reasoning(blocks):
  528. """``blocks`` minus reasoningContent entries carrying redactedContent, or None if not a list /
  529. nothing removed. Pure reasoningText blocks are kept."""
  530. if not isinstance(blocks, list):
  531. return None
  532. cleaned = [b for b in blocks if not (
  533. isinstance(b, dict) and isinstance(b.get("reasoningContent"), dict)
  534. and "redactedContent" in b["reasoningContent"]
  535. )]
  536. return None if len(cleaned) == len(blocks) else cleaned
  537. def strip_redacted_reasoning(kwargs):
  538. """Copy of Converse kwargs with redacted reasoning blocks removed; the SAME object
  539. back when nothing was stripped (callers use identity to decide a retry cannot help).
  540. Turns left with empty content are dropped (they carried no replayable signal)."""
  541. messages = kwargs.get("messages")
  542. if not isinstance(messages, list):
  543. return kwargs
  544. cleaned_contents = [
  545. _without_redacted_reasoning(msg.get("content") if isinstance(msg, dict) else None)
  546. for msg in messages
  547. ]
  548. if all(content is None for content in cleaned_contents):
  549. return kwargs
  550. return {**kwargs, "messages": [
  551. msg if content is None else {**msg, "content": content}
  552. for msg, content in zip(messages, cleaned_contents)
  553. if content is None or len(content) > 0
  554. ]}
  555. def recover_from_redacted_reasoning_rejection(exc, kwargs):
  556. """Return retry kwargs with redacted reasoning blocks stripped, or None when the error
  557. was not an encrypted-content rejection / nothing redacted remained (caller re-raises)."""
  558. if not _REDACTED_REASONING_REJECTION_PATTERN.search(str(exc)):
  559. return None
  560. retry_kwargs = strip_redacted_reasoning(kwargs)
  561. if retry_kwargs is kwargs:
  562. return None
  563. logger.warning(
  564. "bedrock: %s rejected replayed redacted reasoning (encrypted content is sealed to the issuing "
  565. "model/flow) - stripping redacted blocks and resending once.",
  566. str(kwargs.get("modelId", "")) or "model",
  567. )
  568. return retry_kwargs
  569. # One optional regional/global inference-profile prefix, then the Claude model family.
  570. _ANTHROPIC_BEDROCK_MODEL_RE = re.compile(
  571. rf"^(?:(?:{'|'.join(_BEDROCK_PROFILE_PREFIXES)})\.)?anthropic\.claude", re.IGNORECASE,
  572. )
  573. def is_anthropic_bedrock_model(model_id: str) -> bool:
  574. """True for Claude on Bedrock (``anthropic.claude-*``, any regional prefix): AnthropicBedrock SDK path."""
  575. return _ANTHROPIC_BEDROCK_MODEL_RE.match(model_id) is not None
  576. # --- Message format conversion: OpenAI → Bedrock Converse ---
  577. def convert_tools_to_converse(tools: list[dict]) -> list[dict]:
  578. """OpenAI ``{"function": {...}}`` tool defs → Converse ``{"toolSpec": {...}}``."""
  579. return [{"toolSpec": {
  580. "name": fn.get("name", ""), "description": fn.get("description", ""),
  581. "inputSchema": {"json": fn.get("parameters", {"type": "object", "properties": {}})},
  582. }} for fn in (t.get("function", {}) for t in tools or [])]
  583. # Converse rejects empty OR whitespace-only text blocks, so the placeholder must be non-whitespace.
  584. # A lone space is whitespace and is rejected too — the placeholder MUST itself be non-whitespace. Ref: issue
  585. # #9486.
  586. _EMPTY_TEXT_PLACEHOLDER = "(empty)"
  587. _PLACEHOLDER_BLOCK = {"text": _EMPTY_TEXT_PLACEHOLDER}
  588. def _safe_text(text) -> str:
  589. """``text`` if it has non-whitespace content, else the placeholder (None/non-str ok)."""
  590. text = "" if text is None else str(text)
  591. return text if text.strip() else _EMPTY_TEXT_PLACEHOLDER
  592. def _image_block_from_data_url(url: str) -> dict:
  593. """``data:<mime>;base64,...`` → Converse image block with RAW bytes (boto3 base64-encodes on the
  594. wire; passing the string through double-encodes and Bedrock rejects it)."""
  595. header, _, data = url.partition(",")
  596. media_type = (header[5:].split(";")[0] if header.startswith("data:") else "") or "image/jpeg"
  597. try:
  598. # Ref: #33317.
  599. raw_bytes = base64.b64decode(data)
  600. except Exception:
  601. raw_bytes = data.encode("utf-8")
  602. return {"image": {"format": media_type.split("/")[-1] if "/" in media_type else "jpeg", "source": {"bytes": raw_bytes}}}
  603. def _convert_content_to_converse(content) -> list[dict]:
  604. """OpenAI content → Converse blocks; blank text → placeholder, remote image URLs → text reference."""
  605. if not isinstance(content, list):
  606. return [{"text": _safe_text(content)}]
  607. blocks = []
  608. for part in content:
  609. if isinstance(part, str):
  610. blocks.append({"text": _safe_text(part)})
  611. elif isinstance(part, dict) and part.get("type", "") == "text":
  612. blocks.append({"text": _safe_text(part.get("text", ""))})
  613. elif isinstance(part, dict) and part.get("type", "") == "image_url":
  614. image_url = part.get("image_url", {})
  615. url = image_url.get("url", "") if isinstance(image_url, dict) else ""
  616. blocks.append(_image_block_from_data_url(url) if url.startswith("data:") else {"text": f"[Image: {url}]"})
  617. return blocks or [dict(_PLACEHOLDER_BLOCK)]
  618. def _system_blocks(content) -> list[dict]:
  619. """System content → text blocks; blank parts are dropped, not placeholder-filled."""
  620. parts = [content] if isinstance(content, str) else content if isinstance(content, list) else []
  621. texts = [part.get("text", "") if isinstance(part, dict) and part.get("type") == "text" else part for part in parts]
  622. return [{"text": text} for text in texts if isinstance(text, str) and text.strip()]
  623. def _tool_use_block(tool_use_id, name, input_dict) -> dict:
  624. return {"toolUse": {"toolUseId": tool_use_id, "name": name, "input": input_dict}}
  625. def _decode_redacted(encoded) -> Optional[bytes]:
  626. """Strict base64 → bytes; None for empty/non-str/undecodable input."""
  627. try:
  628. return base64.b64decode(encoded, validate=True) if isinstance(encoded, str) and encoded else None
  629. except (ValueError, TypeError):
  630. return None
  631. def _replay_ordered_blocks(ordered_blocks: list) -> list[dict]:
  632. """Rebuild the exact Bedrock block sequence captured at normalization time; redacted reasoning is
  633. stored base64 (JSON-safe sidecar) and undecodable entries are skipped."""
  634. content_blocks: list[dict] = []
  635. for block in ordered_blocks:
  636. if not isinstance(block, dict):
  637. continue
  638. if "text" in block and isinstance(block["text"], str):
  639. content_blocks.append({"text": block["text"]})
  640. elif "reasoningContent" in block:
  641. reasoning = block["reasoningContent"]
  642. if not isinstance(reasoning, dict):
  643. continue
  644. # ReasoningContentBlock is a tagged union: reasoningText and redactedContent must go
  645. # out as separate blocks (#115865). Undecodable redacted entries are skipped alone.
  646. if isinstance(reasoning.get("text"), str):
  647. reasoning_text: dict[str, str] = {"text": reasoning["text"]}
  648. if isinstance(reasoning.get("signature"), str) and reasoning["signature"]:
  649. reasoning_text["signature"] = reasoning["signature"] # models that sign thinking reject unsigned replay
  650. content_blocks.append({"reasoningContent": {"reasoningText": reasoning_text}})
  651. encoded = reasoning.get("redactedContentBase64")
  652. if isinstance(encoded, str) and encoded:
  653. redacted = _decode_redacted(encoded)
  654. if redacted is not None:
  655. content_blocks.append({"reasoningContent": {"redactedContent": redacted}})
  656. elif "toolUse" in block and isinstance(block["toolUse"], dict):
  657. tu = block["toolUse"]
  658. content_blocks.append(_tool_use_block(tu.get("toolUseId", ""), tu.get("name", ""), tu.get("input", {})))
  659. return content_blocks
  660. def _parse_tool_args(args) -> Any:
  661. """JSON-decode a tool-call argument string; {} on failure; non-str passes through."""
  662. try:
  663. return json.loads(args) if isinstance(args, str) else args
  664. except (json.JSONDecodeError, TypeError):
  665. return {}
  666. def _redacted_reasoning_block(encoded) -> Optional[dict]:
  667. redacted = _decode_redacted(encoded)
  668. return {"reasoningContent": {"redactedContent": redacted}} if redacted is not None else None
  669. def _reasoning_text_block(text, signature) -> dict:
  670. reasoning_text = {"text": text if isinstance(text, str) else ""}
  671. if isinstance(signature, str) and signature:
  672. reasoning_text["signature"] = signature
  673. return {"reasoningContent": {"reasoningText": reasoning_text}}
  674. # Anthropic Messages block -> Converse block, for turns produced on the AnthropicBedrock path and sent
  675. # over Converse after the sticky stream-denied fallback (same Claude signatures, Converse union shape).
  676. _ANTHROPIC_TO_CONVERSE = {
  677. "thinking": lambda b, args: _reasoning_text_block(b.get("thinking"), b.get("signature")),
  678. "redacted_thinking": lambda b, args: _redacted_reasoning_block(b.get("data")),
  679. "text": lambda b, args: {"text": b["text"]} if isinstance(b.get("text"), str) and b["text"].strip() else None,
  680. # Input is re-sourced from the redacted tool_calls, never the raw captured block.
  681. "tool_use": lambda b, args: _tool_use_block(b.get("id", ""), b.get("name", ""), args.get(b.get("id"), b.get("input", {}))),
  682. }
  683. def _anthropic_ordered_blocks(msg: dict) -> list[dict]:
  684. args = {
  685. tc.get("id"): _parse_tool_args((tc.get("function") or {}).get("arguments", "{}"))
  686. for tc in (msg.get("tool_calls") or []) if isinstance(tc, dict)
  687. }
  688. converted = (
  689. _ANTHROPIC_TO_CONVERSE[b["type"]](b, args)
  690. for b in (msg.get("anthropic_content_blocks") or [])
  691. if isinstance(b, dict) and b.get("type") in _ANTHROPIC_TO_CONVERSE
  692. )
  693. return [block for block in converted if block is not None]
  694. def _reasoning_details_blocks(msg: dict, claude: bool, own: bool = False) -> list[dict]:
  695. """Thinking from the persisted ``reasoning_details`` (Anthropic shape, as Converse captures it), in stored
  696. order: Claude gets signed thinking + redacted blobs, other models readable text only (falling back to
  697. ``reasoning_content``). This is the carrier a turn reloaded from state.db replays from, since the
  698. ordered ``bedrock_content_blocks`` sidecar is live-only. Converse wants reasoning ahead of toolUse."""
  699. blocks = []
  700. for d in msg.get("reasoning_details") or []:
  701. if not isinstance(d, dict):
  702. continue
  703. if d.get("type") == "thinking" and isinstance(d.get("thinking"), str) and d["thinking"].strip():
  704. if (claude or own) and d.get("signature"):
  705. blocks.append(_reasoning_text_block(d["thinking"], d["signature"]))
  706. elif not claude:
  707. blocks.append(_reasoning_text_block(d["thinking"], None))
  708. elif d.get("type") == "redacted_thinking" and (
  709. block := _redacted_reasoning_block(d.get("data") or d.get("redactedContentBase64"))):
  710. blocks.append(block)
  711. text = msg.get("reasoning_content")
  712. if not blocks and not claude and isinstance(text, str) and text.strip():
  713. blocks.append(_reasoning_text_block(text, None))
  714. return blocks
  715. def _assistant_blocks(msg: dict, content, claude: bool = True, own: bool = False) -> list[dict]:
  716. """Assistant message → Converse blocks, from the first carrier that yields any: the ordered
  717. ``bedrock_content_blocks`` sidecar, (Claude only) the ordered ``anthropic_content_blocks`` sidecar,
  718. else thinking from ``reasoning_details`` followed by text, then tool calls. Anthropic-shaped carriers
  719. hold Claude signatures, so other models never read them."""
  720. ordered_blocks = msg.get("bedrock_content_blocks")
  721. if isinstance(ordered_blocks, list) and (content_blocks := _replay_ordered_blocks(ordered_blocks)):
  722. return content_blocks
  723. if claude and (content_blocks := _anthropic_ordered_blocks(msg)):
  724. return content_blocks
  725. content_blocks = _reasoning_details_blocks(msg, claude, own)
  726. if isinstance(content, str) and content.strip():
  727. content_blocks.append({"text": content})
  728. elif isinstance(content, list):
  729. content_blocks.extend(_convert_content_to_converse(content))
  730. for tc in (msg.get("tool_calls", []) or []):
  731. fn = tc.get("function", {})
  732. content_blocks.append(_tool_use_block(tc.get("id", ""), fn.get("name", ""), _parse_tool_args(fn.get("arguments", "{}"))))
  733. return content_blocks
  734. # Per-model reasoning replay on Converse: (turns before the in-flight tool loop, turns inside it).
  735. # signed — Claude: everything verbatim, signatures and redactedContent included (AWS: "You must include
  736. # the signature and all previous messages"; Bedrock ignores older turns server-side for free).
  737. # strip — DeepSeek-R1 (AWS's multi-turn sample removes reasoningContent) and Kimi K3 (Converse raises
  738. # InternalServerException "when reasoning content from earlier turns is included"): prior turns
  739. # lose reasoning, the in-flight loop replays the model's own blocks as captured.
  740. # issuer — OpenAI's reasoning GPTs (gpt-6 family) reject readable reasoningText in assistant history ("This
  741. # model doesn't support the reasoningContent.reasoningText.text field") and stall on another GPT's
  742. # sealed blobs: only turns the answering model stamped keep reasoning, verbatim so the replayed
  743. # prefix stays stable; every other turn replays none, in the in-flight loop too.
  744. # unsigned — every other model: prior turns carry readable reasoningText only, since their signatures and
  745. # redacted blobs may be another model's (a /model switch from Claude) and non-Claude models
  746. # reject them ("This model doesn't support the reasoningContent.reasoningText.signature field");
  747. # the in-flight loop was produced by the model now answering and replays as captured.
  748. # A turn stamped by another model (CONVERSE_ISSUER_TYPE) never gets the in-flight verbatim replay: a fallback or
  749. # switch mid-loop hands the loop to a model that cannot open the previous one's sealed blobs (_reasoning_filter).
  750. _CONVERSE_REPLAY_STRIP_PATTERNS = ("deepseek.r1", "deepseek-r1", "moonshotai.kimi-k3")
  751. _CONVERSE_REPLAY_ISSUER_PATTERNS = ("openai.gpt-6",) # not gpt-oss: it takes readable reasoningText
  752. # reasoning_details record naming the model that minted a turn's Converse reasoning: its signatures are that
  753. # model's own, so it gets them back unmodified on every later turn and after a reload (AWS: "include the text and
  754. # its signature unmodified"); any other model gets the per-policy prior-turn treatment, and Claude gets no
  755. # reasoning at all from a turn another family minted.
  756. CONVERSE_ISSUER_TYPE = "bedrock.native_assistant"
  757. def _issuer(msg: dict) -> Optional[str]:
  758. return next((d["model"] for d in msg.get("reasoning_details") or []
  759. if isinstance(d, dict) and d.get("type") == CONVERSE_ISSUER_TYPE and isinstance(d.get("model"), str)), None)
  760. def _converse_replay_policy(model: Optional[str]) -> str:
  761. if model is None:
  762. return "signed"
  763. if _APPLICATION_PROFILE_ARN_RE.search(model):
  764. model = _resolve_inference_profile_model_id(model) # cached; the profile ARN names no model
  765. lowered = model.lower()
  766. if "claude" in lowered:
  767. return "signed"
  768. if any(p in lowered for p in _CONVERSE_REPLAY_ISSUER_PATTERNS):
  769. return "issuer"
  770. return "strip" if any(p in lowered for p in _CONVERSE_REPLAY_STRIP_PATTERNS) else "unsigned"
  771. def _minted_outside_claude(issuer: Optional[str]) -> bool:
  772. if issuer is None:
  773. return False
  774. if _APPLICATION_PROFILE_ARN_RE.search(issuer) and _resolve_inference_profile_model_id(issuer) == issuer:
  775. # An application profile names no model, and without bedrock:GetInferenceProfile it stays
  776. # unresolved: the issuer is unknown, not foreign, so its reasoning keeps the answering policy.
  777. return False
  778. return _converse_replay_policy(issuer) != "signed"
  779. def reasoning_minted_outside_claude(msg: dict) -> bool:
  780. """True when a non-Claude Converse model stamped this turn's reasoning. Claude gets none of it on either
  781. Bedrock wire (Converse here, AnthropicBedrock in anthropic_message_convert): it cannot open another
  782. family's sealed blobs. Unstamped turns (Claude's own AnthropicBedrock turns, older sessions) are False."""
  783. return _minted_outside_claude(_issuer(msg))
  784. def _unsigned_reasoning(block: dict) -> Optional[dict]:
  785. reasoning = block.get("reasoningContent")
  786. if not isinstance(reasoning, dict):
  787. return block
  788. text = (reasoning.get("reasoningText") or {}).get("text")
  789. return {"reasoningContent": {"reasoningText": {"text": text}}} if isinstance(text, str) and text.strip() else None
  790. def _verbatim(block: dict) -> dict:
  791. return block
  792. def _without_reasoning(block: dict) -> Optional[dict]:
  793. return None if "reasoningContent" in block else block
  794. _REASONING_FILTERS = { # policy -> (before the in-flight tool loop, inside it)
  795. "signed": (_verbatim, _verbatim),
  796. "strip": (_without_reasoning, _verbatim),
  797. "unsigned": (_unsigned_reasoning, _verbatim),
  798. "issuer": (_without_reasoning, _without_reasoning),
  799. }
  800. def _filter_reasoning(blocks: list[dict], keep) -> list[dict]:
  801. return [kept for kept in map(keep, blocks) if kept is not None]
  802. def _reasoning_filter(policy: str, issuer: Optional[str], own: bool, *, in_loop: bool):
  803. """Which reasoning one assistant turn may replay to the model now answering. A turn stamped by ANOTHER
  804. model (fallback or ``/model`` switch, including mid tool loop) loses the in-flight loop's verbatim replay:
  805. sealed ``redactedContent`` opens only for its issuer, and Claude rejects another family's blobs outright
  806. (``Invalid `data` in `redacted_thinking` block``), so a non-Claude turn replays no reasoning to Claude.
  807. Unstamped turns (older sessions) keep the policy, and the rejection-driven resend covers them; under
  808. ``issuer`` only the answering model's own stamped turns keep any reasoning."""
  809. prior, loop = _REASONING_FILTERS[policy]
  810. if own:
  811. return _verbatim if in_loop or policy in ("unsigned", "issuer") else prior
  812. if issuer is not None:
  813. return _without_reasoning if policy == "signed" and _minted_outside_claude(issuer) else prior
  814. return loop if in_loop else prior
  815. def convert_messages_to_converse(
  816. messages: list[dict], model: Optional[str] = None,
  817. ) -> tuple[Optional[list[dict]], list[dict]]:
  818. """OpenAI messages → ``(system_blocks_or_None, converse_messages)``; tool results become ``toolResult``
  819. user blocks. Converse needs strict user/assistant alternation with a user turn first and last:
  820. same-role neighbours merge, placeholder user turns pad the ends. ``model`` selects the reasoning
  821. replay policy for turns before the in-flight tool loop (None replays every carrier verbatim)."""
  822. policy = _converse_replay_policy(model)
  823. # Tool results are role "tool" here, so the last role "user" message opens the in-flight tool loop.
  824. loop_start = next((i for i in range(len(messages) - 1, -1, -1) if messages[i].get("role") == "user"), -1)
  825. system_blocks: list[dict] = []
  826. converse_msgs: list[dict] = []
  827. def append_turn(role: str, blocks: list[dict]) -> None:
  828. if converse_msgs and converse_msgs[-1]["role"] == role:
  829. converse_msgs[-1]["content"].extend(blocks)
  830. else:
  831. converse_msgs.append({"role": role, "content": blocks})
  832. for idx, msg in enumerate(messages):
  833. role = msg.get("role", "")
  834. content = msg.get("content")
  835. if role == "system":
  836. system_blocks.extend(_system_blocks(content))
  837. elif role == "tool":
  838. result_content = content if isinstance(content, str) else json.dumps(content)
  839. append_turn("user", [{"toolResult": {
  840. "toolUseId": msg.get("tool_call_id", ""), "content": [{"text": _safe_text(result_content)}]}}])
  841. elif role == "assistant":
  842. issuer = _issuer(msg) if model is not None else None
  843. own = issuer is not None and issuer == model
  844. keep = _reasoning_filter(policy, issuer, own, in_loop=idx >= loop_start)
  845. blocks = _filter_reasoning(_assistant_blocks(msg, content, claude=policy == "signed", own=own), keep)
  846. append_turn("assistant", blocks or [dict(_PLACEHOLDER_BLOCK)])
  847. elif role == "user":
  848. append_turn("user", _convert_content_to_converse(content))
  849. if converse_msgs and converse_msgs[0]["role"] != "user":
  850. converse_msgs.insert(0, {"role": "user", "content": [dict(_PLACEHOLDER_BLOCK)]})
  851. if converse_msgs and converse_msgs[-1]["role"] != "user":
  852. converse_msgs.append({"role": "user", "content": [dict(_PLACEHOLDER_BLOCK)]})
  853. return (system_blocks or None, converse_msgs)
  854. # --- Response format conversion: Bedrock Converse → OpenAI ---
  855. # Bedrock stopReason → OpenAI finish_reason (unknown → "stop").
  856. _STOP_REASON_TO_FINISH_REASON = {
  857. "end_turn": "stop", "stop_sequence": "stop", "tool_use": "tool_calls", "max_tokens": "length",
  858. "content_filtered": "content_filter", "guardrail_intervened": "content_filter",
  859. }
  860. def _encode_redacted(redacted) -> Optional[str]:
  861. """Redacted reasoning payload → base64 str (bytes encoded, str passed through, else None)."""
  862. if isinstance(redacted, (bytes, bytearray)):
  863. return base64.b64encode(bytes(redacted)).decode("ascii")
  864. return redacted if isinstance(redacted, str) else None
  865. def _tool_call_ns(tool_use_id: str, name: str, input_dict) -> SimpleNamespace:
  866. return SimpleNamespace(
  867. id=tool_use_id, type="function", function=SimpleNamespace(name=name, arguments=json.dumps(input_dict)),
  868. )
  869. def _persisted_reasoning_details(ordered_blocks: list[dict[str, Any]]) -> list[dict[str, Any]]:
  870. details: list[dict[str, Any]] = []
  871. for block in ordered_blocks:
  872. reasoning = block.get("reasoningContent") if isinstance(block, dict) else None
  873. if not isinstance(reasoning, dict):
  874. continue
  875. if isinstance(reasoning.get("text"), str) and reasoning["text"]:
  876. details.append({"type": "thinking", "thinking": reasoning["text"],
  877. **({"signature": reasoning["signature"]} if reasoning.get("signature") else {})})
  878. if reasoning.get("redactedContentBase64"):
  879. details.append({"type": "redacted_thinking", "data": reasoning["redactedContentBase64"]})
  880. return details
  881. class _ResponseParts:
  882. """Accumulator shared by the sync and streaming normalizers."""
  883. def __init__(self) -> None:
  884. self.text_parts: list[str] = []
  885. self.reasoning_details: list[dict[str, Any]] = []
  886. self.tool_calls: list[SimpleNamespace] = []
  887. def absorb_reasoning(self, reasoning: Any, block: dict[str, Any], on_text=None) -> None:
  888. """Fold a Converse ``reasoningContent`` payload into the accumulators and ``block``. The sync response
  889. nests ``reasoningText: {text, signature}``; stream deltas carry ``text`` / ``signature`` flat."""
  890. if not isinstance(reasoning, dict):
  891. return
  892. if isinstance(reasoning.get("reasoningText"), dict):
  893. reasoning = {**reasoning, **reasoning["reasoningText"]}
  894. thinking_text = reasoning.get("text", "")
  895. if thinking_text:
  896. if on_text:
  897. on_text(thinking_text)
  898. block["text"] = block.get("text", "") + str(thinking_text)
  899. signature = reasoning.get("signature")
  900. if isinstance(signature, str) and signature:
  901. block["signature"] = block.get("signature", "") + signature
  902. encoded = _encode_redacted(reasoning.get("redactedContent"))
  903. if encoded:
  904. self.reasoning_details.append({"type": "redacted_thinking", "data": encoded})
  905. block["redactedContentBase64"] = encoded
  906. def build(self, ordered_blocks: list[dict[str, Any]], usage_data: dict[str, int], stop_reason: str, model: str) -> SimpleNamespace:
  907. """Assemble the OpenAI-shaped response. Converse's inputTokens EXCLUDES cache read/write tokens
  908. (OpenAI's prompt_tokens includes them), so they are added back. ``reasoning_details`` carries every
  909. reasoning block in Anthropic shape (signed thinking included) because it is the persisted column."""
  910. msg = SimpleNamespace(
  911. role="assistant", content="\n".join(self.text_parts) if self.text_parts else None,
  912. tool_calls=self.tool_calls or None,
  913. reasoning_details=_persisted_reasoning_details(ordered_blocks) or self.reasoning_details or None,
  914. reasoning_content="\n\n".join(texts) if (texts := [b["reasoningContent"]["text"] for b in ordered_blocks
  915. if isinstance(b.get("reasoningContent"), dict) and b["reasoningContent"].get("text")]) else None,
  916. bedrock_content_blocks=ordered_blocks or None,
  917. )
  918. cache_read_tokens, cache_write_tokens, output_tokens = (
  919. usage_data.get(k, 0) for k in ("cacheReadInputTokens", "cacheWriteInputTokens", "outputTokens")
  920. )
  921. prompt_tokens = usage_data.get("inputTokens", 0) + cache_read_tokens + cache_write_tokens
  922. usage = SimpleNamespace(
  923. prompt_tokens=prompt_tokens, completion_tokens=output_tokens, total_tokens=prompt_tokens + output_tokens,
  924. cache_read_input_tokens=cache_read_tokens, cache_creation_input_tokens=cache_write_tokens,
  925. )
  926. finish_reason = _STOP_REASON_TO_FINISH_REASON.get(stop_reason, "stop")
  927. if self.tool_calls and finish_reason == "stop":
  928. finish_reason = "tool_calls"
  929. return SimpleNamespace(
  930. choices=[SimpleNamespace(index=0, message=msg, finish_reason=finish_reason)], usage=usage, model=model,
  931. )
  932. def normalize_converse_response(response: dict) -> SimpleNamespace:
  933. """Bedrock Converse response → OpenAI ``ChatCompletion``-shaped SimpleNamespace (``.choices[0].message.
  934. {content,tool_calls,reasoning_content,reasoning_details,bedrock_content_blocks}``, ``finish_reason``, ``.usage``)."""
  935. parts = _ResponseParts()
  936. ordered_blocks = []
  937. for block in response.get("output", {}).get("message", {}).get("content", []):
  938. if "text" in block:
  939. parts.text_parts.append(block["text"])
  940. ordered_blocks.append({"text": block["text"]})
  941. elif "reasoningContent" in block:
  942. ordered_reasoning: dict[str, Any] = {}
  943. parts.absorb_reasoning(block["reasoningContent"], ordered_reasoning)
  944. if ordered_reasoning:
  945. ordered_blocks.append({"reasoningContent": ordered_reasoning})
  946. elif "toolUse" in block:
  947. tu = block["toolUse"]
  948. ordered_blocks.append(_tool_use_block(tu.get("toolUseId", ""), tu.get("name", ""), tu.get("input", {})))
  949. parts.tool_calls.append(_tool_call_ns(tu.get("toolUseId", ""), tu.get("name", ""), tu.get("input", {})))
  950. return parts.build(
  951. ordered_blocks, response.get("usage", {}), response.get("stopReason", "end_turn"), response.get("modelId", ""),
  952. )
  953. # --- Streaming response conversion ---
  954. def normalize_converse_stream_events(event_stream) -> SimpleNamespace:
  955. """Consume a ConverseStream event stream (no callbacks) → same shape as ``normalize_converse_response()``."""
  956. return stream_converse_with_callbacks(event_stream)
  957. def stream_converse_with_callbacks(
  958. event_stream, on_text_delta=None, on_tool_start=None, on_reasoning_delta=None,
  959. on_interrupt_check=None, on_event=None,
  960. ) -> SimpleNamespace:
  961. """boto3 ``converse_stream()`` response + callbacks → the ``normalize_converse_response()`` shape.
  962. ``on_text_delta`` only fires while no toolUse block has been seen (as on the Anthropic/chat_completions
  963. paths); ``on_interrupt_check`` True stops streaming; ``on_event`` fires for EVERY event before branching
  964. and its exceptions are swallowed so a watchdog hook can never abort the stream.
  965. Blocks are keyed by the ``contentBlockIndex`` Bedrock stamps on every contentBlockStart/Delta/Stop:
  966. text blocks get NO contentBlockStart, so a counter keyed on starts shredded them (#108200)."""
  967. parts = _ResponseParts()
  968. stream_blocks: dict[int, dict[str, Any]] = {}
  969. current_block_index: Optional[int] = None
  970. current_tool: Optional[dict] = None
  971. current_text_buffer: list[str] = []
  972. has_tool_use = False
  973. stop_reason = None
  974. interrupted = False
  975. usage_data: dict[str, int] = {}
  976. def block_index(payload: dict[str, Any], *, new_block: bool = False) -> int:
  977. """Index of the block a contentBlock* event addresses. Without ``contentBlockIndex`` (test doubles,
  978. proxies) a start opens a fresh slot and a delta/stop continues the current one."""
  979. idx = payload.get("contentBlockIndex")
  980. if isinstance(idx, int):
  981. return idx
  982. return len(stream_blocks) if new_block or current_block_index is None else current_block_index
  983. def flush_text() -> None:
  984. if current_text_buffer:
  985. parts.text_parts.append("".join(current_text_buffer))
  986. current_text_buffer.clear()
  987. for event in event_stream.get("stream", []):
  988. if on_event is not None:
  989. with suppress(Exception):
  990. on_event()
  991. if on_interrupt_check and on_interrupt_check():
  992. interrupted = True
  993. break
  994. if "contentBlockStart" in event:
  995. start_event = event["contentBlockStart"]
  996. idx = current_block_index = block_index(start_event, new_block=True)
  997. start = start_event.get("start", {})
  998. if "toolUse" in start:
  999. has_tool_use = True
  1000. flush_text()
  1001. current_tool = {"toolUseId": start["toolUse"].get("toolUseId", ""), "name": start["toolUse"].get("name", ""), "input_json": ""}
  1002. stream_blocks[idx] = _tool_use_block(current_tool["toolUseId"], current_tool["name"], {})
  1003. if on_tool_start:
  1004. on_tool_start(current_tool["name"])
  1005. elif "contentBlockDelta" in event:
  1006. delta_event = event["contentBlockDelta"]
  1007. idx = current_block_index = block_index(delta_event)
  1008. delta = delta_event.get("delta", {})
  1009. if "text" in delta:
  1010. text = delta["text"]
  1011. block = stream_blocks.setdefault(idx, {"text": ""})
  1012. block["text"] = block.get("text", "") + text
  1013. current_text_buffer.append(text)
  1014. if on_text_delta and not has_tool_use:
  1015. on_text_delta(text)
  1016. elif "toolUse" in delta and current_tool is not None:
  1017. current_tool["input_json"] += delta["toolUse"].get("input", "")
  1018. elif "reasoningContent" in delta:
  1019. reasoning = delta["reasoningContent"]
  1020. if isinstance(reasoning, dict) and (reasoning.get("text", "") or reasoning.get("signature") or _encode_redacted(reasoning.get("redactedContent"))):
  1021. block = stream_blocks.setdefault(idx, {"reasoningContent": {}}).setdefault("reasoningContent", {})
  1022. parts.absorb_reasoning(reasoning, block, on_reasoning_delta)
  1023. elif "contentBlockStop" in event:
  1024. idx = block_index(event["contentBlockStop"])
  1025. current_block_index = None # a following index-less delta opens a fresh slot, not this one
  1026. if current_tool is not None:
  1027. input_dict = _parse_tool_args(current_tool["input_json"]) # "" → {} via the JSON-error path
  1028. parts.tool_calls.append(_tool_call_ns(current_tool["toolUseId"], current_tool["name"], input_dict))
  1029. if "toolUse" in stream_blocks.get(idx, {}):
  1030. stream_blocks[idx]["toolUse"]["input"] = input_dict
  1031. current_tool = None
  1032. else:
  1033. flush_text()
  1034. elif "messageStop" in event:
  1035. stop_reason = event["messageStop"].get("stopReason", "end_turn")
  1036. elif "metadata" in event:
  1037. meta_usage = event["metadata"].get("usage", {})
  1038. usage_data = {key: meta_usage.get(key, 0) for key in ("inputTokens", "outputTokens", "cacheReadInputTokens", "cacheWriteInputTokens")}
  1039. if stop_reason is None and not interrupted:
  1040. raise EmptyStreamError("Bedrock Converse stream ended before messageStop; response is incomplete")
  1041. flush_text()
  1042. return parts.build([stream_blocks[i] for i in sorted(stream_blocks)], usage_data, stop_reason or "end_turn", "")
  1043. # --- High-level API: call Bedrock Converse ---
  1044. def _claude_converse_reasoning(model: str, reasoning_config: dict, max_tokens: Optional[int]) -> tuple[dict, dict]:
  1045. """Claude thinking over Converse: the same ``thinking``/``output_config`` the AnthropicBedrock path sends,
  1046. carried in ``additionalModelRequestFields`` (AWS: "pass the thinking and effort parameters inside
  1047. additionalModelRequestFields"); manual budgets also need temperature 1 and room above the budget."""
  1048. from agent.anthropic_adapter import _thinking_kwargs
  1049. thinking = _thinking_kwargs(reasoning_config, model, max_tokens or 0)
  1050. fields = {k: thinking[k] for k in ("thinking", "output_config") if k in thinking}
  1051. inference: dict[str, Any] = {}
  1052. if (fields.get("thinking") or {}).get("type") == "enabled":
  1053. if "claude-3" not in model.lower(): # 3.7 has no interleaved thinking
  1054. fields["anthropic_beta"] = ["interleaved-thinking-2025-05-14"]
  1055. inference["temperature"] = thinking.get("temperature", 1)
  1056. if max_tokens is not None:
  1057. inference["maxTokens"] = thinking.get("max_tokens", max_tokens)
  1058. return fields, inference
  1059. _NOVA_EFFORT = {"minimal": "low", "low": "low", "medium": "medium"} # anything stronger -> "high"
  1060. def _nova_converse_reasoning(model: str, reasoning_config: dict, max_tokens: Optional[int]) -> tuple[dict, dict]:
  1061. """Nova 2 ``reasoningConfig`` (off by default). At ``high``, temperature/topP/maxTokens must be unset."""
  1062. if reasoning_config.get("enabled") is False:
  1063. return {}, {}
  1064. effort = _NOVA_EFFORT.get(str(reasoning_config.get("effort", "medium")).lower(), "high")
  1065. return {"reasoningConfig": {"type": "enabled", "maxReasoningEffort": effort}}, (
  1066. {"temperature": None, "topP": None, "maxTokens": None} if effort == "high" else {}
  1067. )
  1068. _CONVERSE_REASONING_BUILDERS = (
  1069. (is_anthropic_bedrock_model, _claude_converse_reasoning),
  1070. (lambda model: "amazon.nova-2-lite" in model.lower(), _nova_converse_reasoning),
  1071. )
  1072. def _apply_converse_reasoning(kwargs: dict, inference_config: dict, model: str, reasoning_config: dict, max_tokens) -> None:
  1073. builder = next((b for matches, b in _CONVERSE_REASONING_BUILDERS if matches(model or "")), None)
  1074. if builder is None:
  1075. return
  1076. fields, inference = builder(model, reasoning_config, max_tokens)
  1077. if fields:
  1078. kwargs["additionalModelRequestFields"] = fields
  1079. for key, value in inference.items():
  1080. if value is None:
  1081. inference_config.pop(key, None)
  1082. else:
  1083. inference_config[key] = value
  1084. def build_converse_kwargs(
  1085. model: str, messages: list[dict], tools: Optional[list[dict]] = None, max_tokens: Optional[int] = 4096,
  1086. temperature: Optional[float] = None, top_p: Optional[float] = None,
  1087. stop_sequences: Optional[list[str]] = None, guardrail_config: Optional[dict] = None,
  1088. reasoning_config: Optional[dict] = None, prompt_cache: bool = True,
  1089. ) -> dict[str, Any]:
  1090. """Build kwargs for ``bedrock-runtime.converse()`` / ``converse_stream()``. ``max_tokens=None`` omits
  1091. ``maxTokens`` (model maximum; default stays 4096). cachePoint markers go on system, tools and the
  1092. second-newest message (survives as the tail grows — mirrors Anthropic system_and_3), each only if the
  1093. model supports caching and Bedrock has not rejected that placement. ``reasoning_config`` becomes the
  1094. model's ``additionalModelRequestFields`` reasoning switch (Claude ``thinking``, Nova 2 ``reasoningConfig``)."""
  1095. system_prompt, converse_messages = convert_messages_to_converse(messages, model=model)
  1096. cache_at = {p for p in CACHE_POINT_PLACEMENTS if cache_point_allowed(model, p)} if prompt_cache and _model_supports_prompt_cache(model) else set()
  1097. inference_config: dict[str, Any] = {} if max_tokens is None else {"maxTokens": max_tokens}
  1098. kwargs: dict[str, Any] = {"modelId": model, "messages": converse_messages, "inferenceConfig": inference_config}
  1099. if system_prompt:
  1100. kwargs["system"] = system_prompt + [dict(_CACHE_POINT)] if "system" in cache_at else system_prompt
  1101. from agent.anthropic_adapter import _forbids_sampling_params
  1102. if not _forbids_sampling_params(model) and not _BEDROCK_XAI_GROK_NO_SAMPLING_RE.match(model or ""):
  1103. inference_config.update({k: v for k, v in (("temperature", temperature), ("topP", top_p)) if v is not None})
  1104. if stop_sequences:
  1105. inference_config["stopSequences"] = stop_sequences
  1106. converse_tools = convert_tools_to_converse(tools) if tools else []
  1107. if converse_tools and not _model_supports_tool_use(model):
  1108. # Non-tool-calling models reject toolConfig (ValidationException → retry loop): strip and warn.
  1109. logger.warning(
  1110. "Model %s does not support tool calling — tools stripped. "
  1111. "The agent will operate in text-only mode.", model
  1112. )
  1113. elif converse_tools:
  1114. kwargs["toolConfig"] = {"tools": converse_tools + [dict(_CACHE_POINT)] if "tools" in cache_at else converse_tools}
  1115. if "messages" in cache_at and len(converse_messages) >= 2:
  1116. content = converse_messages[-2].get("content")
  1117. if isinstance(content, list) and content:
  1118. content.append(dict(_CACHE_POINT))
  1119. if guardrail_config:
  1120. kwargs["guardrailConfig"] = guardrail_config
  1121. if reasoning_config and isinstance(reasoning_config, dict):
  1122. _apply_converse_reasoning(kwargs, inference_config, model, reasoning_config, max_tokens)
  1123. if not inference_config:
  1124. del kwargs["inferenceConfig"] # optional on the wire; don't send {}
  1125. return kwargs
  1126. def call_converse(
  1127. region: str, model: str, messages: list[dict], tools: Optional[list[dict]] = None,
  1128. max_tokens: Optional[int] = 4096, temperature: Optional[float] = None, top_p: Optional[float] = None,
  1129. stop_sequences: Optional[list[str]] = None, guardrail_config: Optional[dict] = None,
  1130. ) -> SimpleNamespace:
  1131. """Non-streaming Converse call → OpenAI-compatible response. Retries once without a rejected cachePoint
  1132. placement; evicts the cached client on stale-connection errors."""
  1133. from agent.prompt_caching import is_one_shot_request
  1134. client = _get_bedrock_runtime_client(region, model)
  1135. # Auxiliary-only entry point: a one-shot's cachePoint is a write no later call reads.
  1136. kwargs = build_converse_kwargs(model, messages, tools, max_tokens, temperature, top_p, stop_sequences, guardrail_config,
  1137. prompt_cache=not is_one_shot_request(messages, tools))
  1138. try:
  1139. response = client.converse(**kwargs)
  1140. except Exception as exc:
  1141. retry_kwargs = recover_from_cache_point_rejection(exc, kwargs)
  1142. if retry_kwargs is not None:
  1143. return normalize_converse_response(client.converse(**retry_kwargs))
  1144. redacted_retry_kwargs = recover_from_redacted_reasoning_rejection(exc, kwargs)
  1145. if redacted_retry_kwargs is not None:
  1146. return normalize_converse_response(client.converse(**redacted_retry_kwargs))
  1147. if is_stale_connection_error(exc):
  1148. logger.warning(
  1149. "bedrock: stale-connection error on converse(region=%s, model=%s): "
  1150. "%s — evicting cached client so the next call reconnects.", region, model, type(exc).__name__,
  1151. )
  1152. invalidate_runtime_client(region)
  1153. raise
  1154. return normalize_converse_response(response)
  1155. # --- Model discovery ---
  1156. _discovery_cache: dict[str, Any] = {}
  1157. _DISCOVERY_CACHE_TTL_SECONDS = 3600
  1158. def reset_discovery_cache():
  1159. """Clear the model discovery cache. Used in tests."""
  1160. _discovery_cache.clear()
  1161. def _model_entry(model_id: str, name: Any, provider: str, input_mods: list, output_mods: list) -> dict[str, Any]:
  1162. return {"id": model_id, "name": (name or model_id).strip(), "provider": provider,
  1163. "input_modalities": input_mods, "output_modalities": output_mods, "streaming": True}
  1164. def _list_foundation_models(client, filter_set: set, models: list[dict[str, Any]]) -> None:
  1165. """Append active, streaming-capable, text-output foundation models (optionally provider-filtered)."""
  1166. for summary in client.list_foundation_models().get("modelSummaries", []):
  1167. model_id = (summary.get("modelId") or "").strip()
  1168. if not model_id:
  1169. continue
  1170. provider_name = summary.get("providerName") or ""
  1171. model_prefix = model_id.split(".")[0].lower() if "." in model_id else ""
  1172. if filter_set and provider_name.lower() not in filter_set and model_prefix not in filter_set:
  1173. continue
  1174. output_mods = summary.get("outputModalities", [])
  1175. if (summary.get("modelLifecycle", {}).get("status", "").upper() != "ACTIVE"
  1176. or not summary.get("responseStreamingSupported", False) or "TEXT" not in output_mods):
  1177. continue
  1178. models.append(_model_entry(
  1179. model_id, summary.get("modelName"), provider_name.strip(), summary.get("inputModalities", []), output_mods,
  1180. ))
  1181. def _list_inference_profiles(client, filter_set: set, models: list[dict[str, Any]]) -> None:
  1182. """Append active cross-region inference profiles whose IDs are not already present (paginated)."""
  1183. profiles, next_token = [], None
  1184. while True:
  1185. response = client.list_inference_profiles(**({"nextToken": next_token} if next_token else {}))
  1186. profiles.extend(response.get("inferenceProfileSummaries", []))
  1187. if not (next_token := response.get("nextToken")):
  1188. break
  1189. seen_ids = {m["id"].lower() for m in models}
  1190. for profile in profiles:
  1191. profile_id = (profile.get("inferenceProfileId") or "").strip()
  1192. if not profile_id or profile.get("status") != "ACTIVE" or profile_id.lower() in seen_ids:
  1193. continue
  1194. if filter_set and not any(
  1195. _extract_provider_from_arn(m.get("modelArn", "")).lower() in filter_set for m in profile.get("models", [])
  1196. ):
  1197. continue
  1198. models.append(_model_entry(profile_id, profile.get("inferenceProfileName"), "inference-profile", ["TEXT"], ["TEXT"]))
  1199. seen_ids.add(profile_id.lower())
  1200. def discover_bedrock_models(region: str, provider_filter: Optional[list[str]] = None) -> list[dict[str, Any]]:
  1201. """Foundation models + inference profiles (cached 1h per region/filter), ``global.`` profiles first then
  1202. by name; [] when the client cannot be built."""
  1203. # The list is account-scoped (whichever credentials the control client signs with), so a routed
  1204. # profile gets its own entry; unscoped keeps the region:filter key byte-for-byte.
  1205. from hermes_constants import get_hermes_home_override, hermes_home_key
  1206. cache_key = f"{region}:{','.join(sorted(provider_filter or []))}"
  1207. if get_hermes_home_override() is not None:
  1208. cache_key = f"{hermes_home_key()}|{cache_key}"
  1209. cached = _discovery_cache.get(cache_key)
  1210. if cached and (time.time() - cached["timestamp"]) < _DISCOVERY_CACHE_TTL_SECONDS:
  1211. return cached["models"]
  1212. try:
  1213. client = _get_bedrock_control_client(region)
  1214. except Exception as e:
  1215. logger.warning("Failed to create Bedrock client for model discovery: %s", e)
  1216. return []
  1217. models: list[dict[str, Any]] = []
  1218. filter_set = {f.lower() for f in (provider_filter or [])}
  1219. for step, log, message in (
  1220. (_list_foundation_models, logger.warning, "Failed to list Bedrock foundation models: %s"),
  1221. (_list_inference_profiles, logger.debug, "Skipping inference profile discovery: %s"),
  1222. ):
  1223. try:
  1224. step(client, filter_set, models)
  1225. except Exception as e:
  1226. log(message, e)
  1227. models.sort(key=lambda m: (0 if m["id"].startswith("global.") else 1, m["name"].lower()))
  1228. _discovery_cache[cache_key] = {"timestamp": time.time(), "models": models}
  1229. return models
  1230. def _extract_provider_from_arn(arn: str) -> str:
  1231. """``arn:aws:bedrock:...:foundation-model/anthropic.claude-v2`` → ``"anthropic"``."""
  1232. match = re.search(r"foundation-model/([^.]+)", arn)
  1233. return match.group(1) if match else ""
  1234. # --- Bedrock model context lengths ---
  1235. # Static fallback when the live probe is unavailable (agent/model_metadata.py). Keys match by longest
  1236. # substring, so versioned entries win over the generic "anthropic.claude-opus-4".
  1237. BEDROCK_CONTEXT_LENGTHS: dict[str, int] = {
  1238. # https://docs.aws.amazon.com/bedrock/latest/userguide/model-card-xai-grok-4-6.html
  1239. "xai.grok-4.6": 500_000,
  1240. # Anthropic Claude: 1M GA vs 200K. The 1M entries must match agent/model_metadata.py
  1241. # DEFAULT_CONTEXT_LENGTHS or context compresses early — Opus 5 reached that table and not this
  1242. # one, so the offline path resolved 128K for a 1M model (#74263); the pairing is now tested.
  1243. **dict.fromkeys((
  1244. "anthropic.claude-fable-5", "anthropic.claude-fable", "anthropic.claude-sonnet-5", "anthropic.claude-opus-5",
  1245. "anthropic.claude-opus-4-8", "anthropic.claude-opus-4-7",
  1246. "anthropic.claude-opus-4-6", "anthropic.claude-sonnet-4-6",
  1247. ), 1_000_000),
  1248. **dict.fromkeys((
  1249. "anthropic.claude-sonnet-4-5", "anthropic.claude-haiku-4-5", "anthropic.claude-opus-4", "anthropic.claude-sonnet-4",
  1250. "anthropic.claude-3-5-sonnet", "anthropic.claude-3-5-haiku", "anthropic.claude-3-opus", "anthropic.claude-3-sonnet",
  1251. "anthropic.claude-3-haiku",
  1252. ), 200_000),
  1253. # Amazon Nova
  1254. **dict.fromkeys(("amazon.nova-pro", "amazon.nova-lite"), 300_000), "amazon.nova-micro": 128_000,
  1255. # Meta Llama / Mistral / DeepSeek
  1256. **dict.fromkeys((
  1257. "meta.llama4-maverick", "meta.llama4-scout", "meta.llama3-3-70b-instruct", "mistral.mistral-large", "deepseek.v3",
  1258. ), 128_000),
  1259. # Open-weight and third-party models: the "Context window" field of each AWS card
  1260. # (docs.aws.amazon.com/bedrock/latest/userguide/model-card-<vendor>-<model>.html, read 2026-11-04). Their
  1261. # model_metadata rows are vendor-direct windows (often larger), so they get their own rows. A card above the
  1262. # model_metadata row for the same id (MiniMax M2, DeepSeek V3.2, Nemotron 3, Qwen3 235B/Next/VL) waits for that
  1263. # row first, as every entry here stays <= it; Llama 4 is #106492 (measured windows differ from the cards).
  1264. "amazon.nova-2-lite": 1_000_000, "ai21.jamba-1-5": 256_000,
  1265. **dict.fromkeys(("google.gemma-4-26b", "google.gemma-4-31b"), 256_000),
  1266. **dict.fromkeys(("minimax.minimax-m2.1", "minimax.minimax-m2.5"), 196_000),
  1267. **dict.fromkeys(("mistral.devstral-2", "mistral.mistral-large-3"), 256_000),
  1268. **dict.fromkeys(("moonshotai.kimi-k2.5", "moonshot.kimi-k2-thinking", "moonshotai.kimi-k2-thinking"), 256_000),
  1269. "moonshotai.kimi-k3": 1_000_000, "qwen.qwen3-coder": 256_000,
  1270. "zai.glm-4.7": 203_000, "zai.glm-5": 200_000, "zai.glm-5.3": 1_000_000,
  1271. # OpenAI on Bedrock: docs.aws.amazon.com/bedrock/latest/userguide/model-cards-openai.html. GPT-6.1 Sol's
  1272. # card states "1M tokens"; the other cards state 1,050,000. 272K is the long-context PRICING tier
  1273. # boundary on these cards, not the window. Each entry stays <= model_metadata.DEFAULT_CONTEXT_LENGTHS.
  1274. "openai.gpt-6.1-sol": 1_000_000,
  1275. **dict.fromkeys((
  1276. "openai.gpt-6-astra", "openai.gpt-6.1-terra", "openai.gpt-6-sol", "openai.gpt-6-luna",
  1277. "openai.gpt-5.5", "openai.gpt-5.6-sol", "openai.gpt-5.6-terra", "openai.gpt-5.6-luna", "openai.gpt-5.4",
  1278. ), 1_050_000),
  1279. }
  1280. BEDROCK_DEFAULT_CONTEXT_LENGTH = 128_000 # unknown Bedrock models
  1281. _DEFAULT_WINDOW_WARNED: set = set() # one fallback WARNING per model id per process
  1282. # Issuers whose model_metadata.DEFAULT_CONTEXT_LENGTHS family rows match their Bedrock cards (card sweep
  1283. # 2026-11-04, 34 cards: 27 equal, 6 below, 1 above: GPT-6.1 Sol at 1,050,000 vs its 1M card, which has its own
  1284. # row above). A model one of them ships on Bedrock before this table lists it takes that catalog family window
  1285. # (GPT-6.1 Terra was 128K here while the catalog had 1,050,000). Other issuers' catalog rows are vendor-direct
  1286. # windows that overstate Bedrock (Llama 3 8B: card 8K, "llama" 131,072; Qwen3 32B: card 32K, "qwen" 131,072),
  1287. # so those models keep the default.
  1288. _CATALOG_FAMILY_ID_RE = re.compile(r"(?:^|[./])(?:anthropic|openai|xai)\.([a-z0-9][a-z0-9.:-]*)$")
  1289. # Probe padding tiers (tokens): a wildly oversized payload yields an opaque InternalServerException
  1290. # instead of a clean ValidationException.
  1291. _BEDROCK_PROBE_TIERS = (1_300_000, 2_200_000)
  1292. _WORDS_PER_TOKEN = 0.9 # conservative: ensures the padded prompt clears the tier
  1293. def probe_bedrock_context_length(model_id: str, region: str) -> Optional[int]:
  1294. """Discover a model's real context window by provoking a length error — the only authoritative source
  1295. ("prompt is too long: 1300032 tokens > 1000000 maximum"); length validation runs before inference so the
  1296. probe costs nothing. An accepted tier is a safe lower bound; None (no creds/network/unparseable) → static table."""
  1297. from agent.model_metadata import parse_context_limit_from_error
  1298. try:
  1299. client = _get_bedrock_runtime_client(region, model_id)
  1300. except Exception as exc: # boto3 missing / credential resolution failure
  1301. logger.debug("Bedrock context probe skipped for %s: %s", model_id, exc)
  1302. return None
  1303. last_error = ""
  1304. for tier_tokens in _BEDROCK_PROBE_TIERS:
  1305. oversized = "data " * int(tier_tokens / _WORDS_PER_TOKEN)
  1306. try:
  1307. client.converse(modelId=model_id, messages=[{"role": "user", "content": [{"text": oversized}]}],
  1308. inferenceConfig={"maxTokens": 8})
  1309. logger.debug("Bedrock context probe for %s accepted ~%s-token prompt; "
  1310. "window is at least that", model_id, f"{tier_tokens:,}")
  1311. return tier_tokens
  1312. except Exception as exc:
  1313. last_error = str(exc)
  1314. limit = parse_context_limit_from_error(last_error)
  1315. if limit and limit >= 1024:
  1316. logger.info("Probed Bedrock context window for %s: %s tokens", model_id, f"{limit:,}")
  1317. return limit
  1318. # Opaque server error / auth / throttle at this tier — try the next.
  1319. logger.debug("Bedrock context probe for %s returned no parseable limit: %s", model_id, last_error[:200])
  1320. return None
  1321. def bedrock_static_context_length(model_id: str) -> Optional[int]:
  1322. """Offline window: the table row (longest substring), else the catalog family window for a covered issuer
  1323. (``_CATALOG_FAMILY_ID_RE``); None when neither knows the model. An application-profile ARN reads only the
  1324. per-process profile cache, so display callers never make a control-plane call."""
  1325. if _APPLICATION_PROFILE_ARN_RE.search(model_id):
  1326. model_id = _inference_profile_model_cache.get(model_id, model_id)
  1327. lowered = model_id.lower()
  1328. matches = [key for key in BEDROCK_CONTEXT_LENGTHS if key in lowered]
  1329. if matches:
  1330. return BEDROCK_CONTEXT_LENGTHS[max(matches, key=len)]
  1331. family = _CATALOG_FAMILY_ID_RE.search(lowered)
  1332. if family is None:
  1333. return None
  1334. from agent.model_metadata import DEFAULT_CONTEXT_LENGTHS, _longest_key_match
  1335. hit = _longest_key_match(DEFAULT_CONTEXT_LENGTHS, family.group(1))
  1336. return hit[1] if hit else None
  1337. def get_bedrock_context_length(model_id: str, region: str = "", probe: bool = True) -> int:
  1338. """Context window: live probe (if ``probe`` and ``region``) → static table → catalog family → default. The
  1339. table is fallback only: a stale substring match silently caps the window (a 1M Opus pinned to 200K via
  1340. "opus-4"). An application-inference-profile ARN is first resolved to the model it wraps (#114476)."""
  1341. profile_arn = model_id if _APPLICATION_PROFILE_ARN_RE.search(model_id) else ""
  1342. if profile_arn:
  1343. model_id = _resolve_inference_profile_model_id(profile_arn, region)
  1344. if probe and region and (probed := probe_bedrock_context_length(model_id, region)):
  1345. return probed
  1346. known = bedrock_static_context_length(model_id)
  1347. if known:
  1348. return known
  1349. override = ("Set model.context_length, or model_overrides.bedrock.<model id>.context_window, in config.yaml "
  1350. "if the model's window is larger.")
  1351. if profile_arn:
  1352. logger.warning(
  1353. "Bedrock inference profile %s resolved no known model window; using the static %s-token fallback "
  1354. "(not a detected limit). Grant bedrock:GetInferenceProfile, or: %s",
  1355. profile_arn, f"{BEDROCK_DEFAULT_CONTEXT_LENGTH:,}", override,
  1356. )
  1357. elif model_id not in _DEFAULT_WINDOW_WARNED:
  1358. _DEFAULT_WINDOW_WARNED.add(model_id)
  1359. logger.warning(
  1360. "Bedrock model %s has no known context window; using the static %s-token fallback "
  1361. "(not a detected limit). %s", model_id, f"{BEDROCK_DEFAULT_CONTEXT_LENGTH:,}", override,
  1362. )
  1363. from agent.context_default_record import mark_context_defaulted
  1364. mark_context_defaulted()
  1365. return BEDROCK_DEFAULT_CONTEXT_LENGTH
  1366. # An application-inference-profile ARN (cost-allocation wrapper) carries an opaque id, so the probe
  1367. # error text and the static substring table both miss and the 128k default silently applies
  1368. # (#114476). System-defined `inference-profile/us.anthropic...` ARNs embed the model id and need no
  1369. # lookup. The ARN's own region (field 4) is authoritative for the control-plane call: the runtime
  1370. # region / base_url may differ, and an empty region must not skip the lookup because the
  1371. # production caller (agent/model_metadata.py::_resolve_bedrock_context_length) passes none.
  1372. _APPLICATION_PROFILE_ARN_RE = re.compile(r":application-inference-profile/")
  1373. _ARN_REGION_RE = re.compile(r"^arn:[^:]+:bedrock:([a-z0-9-]+):", re.IGNORECASE)
  1374. _inference_profile_model_cache: dict[str, str] = {}
  1375. def _resolve_inference_profile_model_id(profile_arn: str, region: str = "") -> str:
  1376. """Application-profile ARN → the wrapped model's ARN (its ``foundation-model/<id>`` tail satisfies the
  1377. static-table substring match); the profile ARN itself when ``bedrock:GetInferenceProfile`` is not
  1378. granted or unavailable, so callers keep the default-window behaviour. Both outcomes are cached per
  1379. process: this runs on every context-length resolution, not once per model."""
  1380. if profile_arn in _inference_profile_model_cache:
  1381. return _inference_profile_model_cache[profile_arn]
  1382. arn_region = _ARN_REGION_RE.match(profile_arn)
  1383. region = (arn_region.group(1) if arn_region else "") or region or resolve_bedrock_region()
  1384. resolved = profile_arn
  1385. try:
  1386. client = _get_bedrock_control_client(region)
  1387. models = client.get_inference_profile(inferenceProfileIdentifier=profile_arn).get("models") or []
  1388. resolved = next((m["modelArn"] for m in models if m.get("modelArn")), profile_arn)
  1389. except Exception as exc: # no boto3 / credentials / GetInferenceProfile not granted
  1390. logger.debug("Inference profile resolution skipped for %s: %s", profile_arn, exc)
  1391. _inference_profile_model_cache[profile_arn] = resolved
  1392. return resolved