anthropic_message_convert.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. """OpenAI-style -> Anthropic Messages API request conversion: model-id normalization, tool
  2. schemas, and the message list (content blocks, thinking blocks and their signatures,
  3. tool_use/tool_result pairing, cache_control placement, screenshot eviction, blank-block
  4. scrubbing). Endpoint predicates come from ``agent/anthropic_endpoints.py`` so this module never
  5. imports the adapter (no cycle)."""
  6. import copy
  7. import json
  8. import logging
  9. import re
  10. from typing import Any, Dict, List, Optional, Tuple
  11. from agent.image_eviction_policy import images_all_within_many_image_limit, outbound_image_retire_count
  12. from agent.anthropic_thinking_policy import anthropic_thinking_route, route_replays_prior_thinking
  13. logger = logging.getLogger(__name__)
  14. _THINKING_TYPES = frozenset(("thinking", "redacted_thinking"))
  15. _CACHEABLE_TYPES = frozenset(("text", "tool_use"))
  16. _EMPTY_TEXT_PLACEHOLDER = "(empty)"
  17. _EMPTY_SCHEMA = {"type": "object", "properties": {}}
  18. _BEDROCK_REGION_PREFIXES = ("global.", "us.", "eu.", "apac.", "ap.", "au.", "jp.", "ca.", "sa.", "me.", "af.")
  19. def _block_type(b: Any) -> Any:
  20. """``type`` of a dict block, None for non-dicts."""
  21. return b.get("type") if isinstance(b, dict) else None
  22. def _has_block_type(blocks: list[Any], types) -> bool:
  23. return any(_block_type(b) in types for b in blocks)
  24. def _is_blank_text_block(b: Any) -> bool:
  25. """A text block whose ``text`` is not a non-whitespace string (None/int/blank all count) —
  26. Anthropic 400s on them ("text content blocks must contain non-whitespace text")."""
  27. return _block_type(b) == "text" and not (isinstance(b.get("text"), str) and b["text"].strip())
  28. def _cache_control_of(b: Any) -> Optional[dict[str, Any]]:
  29. cc = b.get("cache_control") if isinstance(b, dict) else None
  30. return cc if isinstance(cc, dict) else None
  31. def _text_block(text: str) -> dict[str, str]:
  32. return {"type": "text", "text": text}
  33. def _text_block_with_citations(text: Any, cits: Any) -> dict[str, Any]:
  34. """Text block carrying ``citations`` only when it is a non-empty list (the only input-valid shape)."""
  35. block: dict[str, Any] = _text_block(text)
  36. if isinstance(cits, list) and cits:
  37. block["citations"] = cits
  38. return block
  39. def _parse_tool_args(raw: Any) -> Any:
  40. """JSON-decode a tool_call ``arguments`` string; non-strings pass through, bad JSON -> {}."""
  41. try:
  42. return json.loads(raw) if isinstance(raw, str) else raw
  43. except (json.JSONDecodeError, ValueError):
  44. return {}
  45. def _strip_thinking(blocks: list[Any]) -> list[Any]:
  46. return [b for b in blocks if _block_type(b) not in _THINKING_TYPES]
  47. def _block_ids(blocks: list[Any], btype: str, key: str) -> set:
  48. return {b.get(key) for b in blocks if _block_type(b) == btype}
  49. def _assistant_block_lists(result: list[dict[str, Any]]):
  50. """``(index, message)`` for every assistant message whose content is a block list."""
  51. return ((i, m) for i, m in enumerate(result) if m.get("role") == "assistant" and isinstance(m.get("content"), list))
  52. def _carry_cache_control(out: dict[str, Any], b: Any, *, copy: bool = False) -> dict[str, Any]:
  53. """Carry a dict-valued ``cache_control`` marker from ``b`` onto ``out`` (returned); ``copy``
  54. shallow-copies it so the caller's dict is never shared with the wire payload."""
  55. cc = _cache_control_of(b)
  56. if cc is not None:
  57. out["cache_control"] = dict(cc) if copy else cc
  58. return out
  59. def _split_blank_text_blocks(blocks: list[Any]) -> tuple[list[Any], Any, list[int]]:
  60. """``(kept, relocated_cache_control, dropped_indexes)``: drop blank text blocks, remembering
  61. the cache_control of the last one dropped so the caller can relocate the breakpoint."""
  62. dropped = [i for i, blk in enumerate(blocks) if _is_blank_text_block(blk)]
  63. kept = [blk for i, blk in enumerate(blocks) if i not in dropped]
  64. relocated = [cc for i in dropped if (cc := _cache_control_of(blocks[i])) is not None]
  65. return kept, relocated[-1] if relocated else None, dropped
  66. def _is_bedrock_model_id(model: str) -> bool:
  67. """Bedrock ids (``anthropic.claude-opus-4-7``, ``us.anthropic.claude-*``) use dots as namespace
  68. separators that must be preserved verbatim."""
  69. return model.lower().startswith(_BEDROCK_REGION_PREFIXES + ("anthropic.",))
  70. def normalize_model_name(model: str, preserve_dots: bool = False) -> str:
  71. """Strip the ``anthropic/`` prefix (case-insensitive) and, unless ``preserve_dots`` (DashScope:
  72. ``qwen3.5-plus``), convert version dots to hyphens for Claude models only (``claude-opus-4.6``
  73. -> ``claude-opus-4-6``). Bedrock ids keep their namespace dots; non-Anthropic names
  74. (``gpt-5.4``) keep dots as part of their canonical form."""
  75. if model.lower().startswith("anthropic/"):
  76. model = model[len("anthropic/"):]
  77. if not preserve_dots and not _is_bedrock_model_id(model) and model.lower().startswith(("claude-", "anthropic/")):
  78. # Only convert dots to hyphens for Anthropic/Claude models. See issue #17171.
  79. model = model.replace(".", "-")
  80. return model
  81. def _sanitize_tool_id(tool_id: str) -> str:
  82. """Anthropic requires ids matching [a-zA-Z0-9_-]; replace the rest, never empty."""
  83. return (re.sub(r"[^a-zA-Z0-9_-]", "_", tool_id) if tool_id else "") or "tool_0"
  84. def _tool_use_block(tool_id: Any, name: Any, tool_input: Any) -> dict[str, Any]:
  85. return {"type": "tool_use", "id": _sanitize_tool_id(tool_id), "name": name, "input": tool_input}
  86. def _normalize_tool_input_schema(schema: Any) -> dict[str, Any]:
  87. """Normalize a tool schema for Anthropic's validator: collapse nullable unions (``anyOf:
  88. [{type: string}, {type: null}]`` from Pydantic/MCP optional fields) to the non-null branch —
  89. optionality is already expressed by ``required``; ``keep_nullable_hint=False`` because the
  90. OpenAPI ``nullable`` keyword is not recognized. Top-level oneOf/allOf/anyOf are rejected with a
  91. generic 400, so they are dropped in favour of a plain object."""
  92. from tools.schema_sanitizer import strip_nullable_unions
  93. normalized = strip_nullable_unions(schema, keep_nullable_hint=False) if schema else None
  94. if not isinstance(normalized, dict):
  95. return dict(_EMPTY_SCHEMA)
  96. banned = {"oneOf", "allOf", "anyOf"}
  97. if banned & normalized.keys():
  98. normalized = {k: v for k, v in normalized.items() if k not in banned}
  99. normalized.setdefault("type", "object")
  100. if normalized.get("type") == "object" and not isinstance(normalized.get("properties"), dict):
  101. normalized = {**normalized, "properties": {}}
  102. return normalized
  103. def convert_tools_to_anthropic(tools: list[dict]) -> list[dict]:
  104. """Convert OpenAI tool definitions to Anthropic format. Duplicate names are dropped with a
  105. warning (Anthropic hard-400s on them); ``cache_control`` on the OpenAI tool dict is forwarded."""
  106. result = []
  107. seen_names: set = set()
  108. for t in tools or []:
  109. fn = t.get("function", {})
  110. name = fn.get("name", "")
  111. # Defensive dedup: Anthropic rejects requests with duplicate tool names. Upstream injection paths
  112. # already dedup, but this guard converts a hard API failure into a warning. See: #18478
  113. if name and name in seen_names:
  114. logger.warning("convert_tools_to_anthropic: duplicate tool name '%s' — dropping second occurrence", name)
  115. continue
  116. if name:
  117. seen_names.add(name)
  118. anthropic_tool: dict[str, Any] = {
  119. "name": name, "description": fn.get("description", ""),
  120. "input_schema": _normalize_tool_input_schema(fn.get("parameters") or {}),
  121. }
  122. result.append(_carry_cache_control(anthropic_tool, t, copy=True))
  123. return result
  124. def _image_block_from_openai_url(url: str) -> dict[str, Any]:
  125. """OpenAI image URL / data URL -> Anthropic ``image`` block. An inline subtype the API rejects
  126. (svg+xml, bmp, tiff) 400s every replay once in history: an SVG is rasterized to PNG when a
  127. rasterizer is installed, anything else unsupported becomes a text placeholder."""
  128. from tools.vision_tools_image_prep import rasterize_svg_data_url, unsupported_inline_image_media_type
  129. url = str(url or "").strip()
  130. if not url.startswith("data:"):
  131. return {"type": "image", "source": {"type": "url", "url": url}}
  132. unsupported = unsupported_inline_image_media_type(url)
  133. if unsupported == "image/svg+xml" and (png_url := rasterize_svg_data_url(url)) is not None:
  134. url, unsupported = png_url, None
  135. if unsupported is not None:
  136. return _text_block(f"[image omitted: {unsupported} is not a supported image format]")
  137. header, _, data = url.partition(",")
  138. mime_part = header[len("data:"):].split(";", 1)[0].strip()
  139. media_type = mime_part if mime_part.startswith("image/") else "image/jpeg"
  140. media_type = "image/jpeg" if media_type.lower() == "image/jpg" else media_type
  141. return {"type": "image", "source": {"type": "base64", "media_type": media_type, "data": data}}
  142. def _convert_content_part_to_anthropic(part: Any) -> Optional[dict[str, Any]]:
  143. """Convert one OpenAI-style content part to an Anthropic block (None -> dropped)."""
  144. if part is None:
  145. return None
  146. if not isinstance(part, dict):
  147. return _text_block(part if isinstance(part, str) else str(part))
  148. ptype = part.get("type")
  149. if ptype in ("input_text", "text"):
  150. # Rebuild from whitelisted fields only: stored SDK text blocks carry output-only siblings
  151. # (parsed_output, citations=None) that the INPUT schema rejects with 400.
  152. block = _text_block_with_citations(part.get("text", ""), part.get("citations") if ptype == "text" else None)
  153. elif ptype in {"image_url", "input_image"}:
  154. image_value = part.get("image_url", {})
  155. url = image_value.get("url", "") if isinstance(image_value, dict) else str(image_value or "")
  156. block = _image_block_from_openai_url(url)
  157. else:
  158. block = dict(part)
  159. if (cache_control := _cache_control_of(part)) is not None:
  160. block.setdefault("cache_control", dict(cache_control))
  161. return block
  162. def _to_plain_data(value: Any, *, _depth: int = 0, _path: Optional[set] = None) -> Any:
  163. """Recursively convert SDK objects to plain Python data. ``_path`` tracks ids on the *current*
  164. recursion path (shared but non-cyclic objects convert normally while true cycles stringify);
  165. depth is capped at 20."""
  166. _path = set() if _path is None else _path
  167. obj_id = id(value)
  168. if _depth > 20 or obj_id in _path:
  169. return str(value)
  170. def rec(v):
  171. return _to_plain_data(v, _depth=_depth + 1, _path=_path)
  172. _path.add(obj_id)
  173. if hasattr(value, "model_dump"):
  174. try:
  175. # warnings=False: streaming-accumulator blocks trip pydantic's serializer-mismatch
  176. # UserWarning, which otherwise leaks to the terminal.
  177. dumped = value.model_dump(warnings=False)
  178. except TypeError: # duck-typed model_dump without pydantic's signature
  179. dumped = value.model_dump()
  180. result = rec(dumped)
  181. elif isinstance(value, dict):
  182. result = {k: rec(v) for k, v in value.items()}
  183. elif isinstance(value, (list, tuple)):
  184. result = [rec(v) for v in value]
  185. elif hasattr(value, "__dict__"):
  186. result = {k: rec(v) for k, v in vars(value).items() if not k.startswith("_")}
  187. else:
  188. result = value
  189. _path.discard(obj_id)
  190. return result
  191. def _extract_preserved_thinking_blocks(message: dict[str, Any]) -> list[dict[str, Any]]:
  192. """Deep-copied thinking/redacted_thinking blocks from ``reasoning_details``; none from a turn a non-Claude
  193. Bedrock Converse model stamped (a GPT -> Claude switch or fallback): Claude rejects another family's
  194. sealed blobs (``Invalid `data` in `redacted_thinking` block``), and no Anthropic route signed them."""
  195. raw_details = message.get("reasoning_details")
  196. if not isinstance(raw_details, list):
  197. return []
  198. from agent.bedrock_adapter import reasoning_minted_outside_claude
  199. if reasoning_minted_outside_claude(message):
  200. return []
  201. return [
  202. copy.deepcopy(d)
  203. for d in raw_details
  204. if isinstance(d, dict) and str(d.get("type", "") or "").strip().lower() in _THINKING_TYPES
  205. ]
  206. def _convert_content_to_anthropic(content: Any) -> Any:
  207. """Convert an OpenAI multimodal content list to Anthropic blocks (non-lists pass through)."""
  208. if not isinstance(content, list):
  209. return content
  210. return [b for b in map(_convert_content_part_to_anthropic, content) if b is not None]
  211. def _content_parts_to_anthropic_blocks(parts: Any) -> list[dict[str, Any]]:
  212. """Tool-message content parts -> tool_result inner blocks (text + image only, the types
  213. Anthropic accepts there). Used for multimodal tool results."""
  214. out: list[dict[str, Any]] = []
  215. for block in map(_convert_content_part_to_anthropic, parts if isinstance(parts, list) else []):
  216. if not block:
  217. continue
  218. btype, text_val, src = block.get("type"), block.get("text"), block.get("source")
  219. if btype == "text" and isinstance(text_val, str) and text_val:
  220. out.append(_text_block(text_val))
  221. elif btype == "image" and isinstance(src, dict) and src:
  222. out.append({"type": "image", "source": src})
  223. return out
  224. def _safe_text(text: Any) -> str:
  225. """``text`` if non-whitespace, else the placeholder. A blank text block stored in history (e.g.
  226. by compression) is replayed on every turn and wedges the session with HTTP 400; the placeholder
  227. is self-healing. Mirrors ``bedrock_adapter._safe_text`` (kept separate on purpose)."""
  228. text = "" if text is None else str(text)
  229. return text if text.strip() else _EMPTY_TEXT_PLACEHOLDER
  230. def _replay_text(b: dict[str, Any]) -> Optional[dict[str, Any]]:
  231. # Drop blank blocks rather than coerce in place: the caller relocates any cache_control and
  232. # falls back to a placeholder only when nothing survives, so "(empty)" never sits as
  233. # model-visible noise next to real blocks.
  234. if _is_blank_text_block(b):
  235. return None
  236. return _carry_cache_control(_text_block_with_citations(b["text"], b.get("citations")), b)
  237. def _replay_thinking(b: dict[str, Any]) -> dict[str, Any]:
  238. out = {"type": "thinking", "thinking": b.get("thinking", "")}
  239. return {**out, "signature": b["signature"]} if b.get("signature") else out
  240. def _replay_redacted_thinking(b: dict[str, Any]) -> Optional[dict[str, Any]]:
  241. return {"type": "redacted_thinking", "data": b["data"]} if b.get("data") else None
  242. def _replay_tool_use(b: dict[str, Any]) -> dict[str, Any]:
  243. return _carry_cache_control(_tool_use_block(b.get("id", ""), b.get("name", ""), b.get("input", {})), b)
  244. def _replay_image(b: dict[str, Any]) -> Optional[dict[str, Any]]:
  245. src = b.get("source")
  246. return {"type": "image", "source": src} if isinstance(src, dict) else None
  247. _REPLAY_SANITIZERS = {
  248. "text": _replay_text, "thinking": _replay_thinking, "redacted_thinking": _replay_redacted_thinking,
  249. "tool_use": _replay_tool_use, "image": _replay_image,
  250. }
  251. def _sanitize_replay_block(b: dict[str, Any]) -> Optional[dict[str, Any]]:
  252. """Whitelist a stored Anthropic block so it is valid as REQUEST input. SDK response blocks carry
  253. output-only fields the INPUT schema forbids ("Extra inputs are not permitted": ``parsed_output``,
  254. ``caller``, ``citations=None``), and ``_to_plain_data`` captured them verbatim. Whitelist per
  255. type (not blacklist) so future SDK fields can't reintroduce the bug; unknown types are dropped.
  256. Returns a clean block or None."""
  257. if not isinstance(b, dict):
  258. return None
  259. sanitizer = _REPLAY_SANITIZERS.get(b.get("type"))
  260. return sanitizer(b) if sanitizer else None
  261. def _apply_assistant_cache_control_to_last_cacheable_block(blocks: list[dict[str, Any]], cache_control: Any) -> None:
  262. if not isinstance(cache_control, dict):
  263. return
  264. for block in reversed(blocks):
  265. if _block_type(block) in _CACHEABLE_TYPES:
  266. block.setdefault("cache_control", dict(cache_control))
  267. break
  268. def _replay_ordered_blocks(m: dict[str, Any], ordered_blocks: list[Any]) -> Optional[list[dict[str, Any]]]:
  269. """Interleaved-thinking replay: rebuild the assistant turn from the verbatim block list
  270. normalize_response stored (only for turns interleaving SIGNED thinking with tool_use).
  271. Preserves block ORDER; returns None if nothing survives. tool_use ``input`` is re-sourced from
  272. ``tool_calls`` (redacted at storage time) rather than the captured block (raw API response, NOT
  273. redacted), so a secret the model inlined into a tool call never goes back on the wire."""
  274. redacted_input_by_id = {
  275. _sanitize_tool_id(tc.get("id", "")): _parse_tool_args((tc.get("function", {}) or {}).get("arguments", "{}"))
  276. for tc in m.get("tool_calls", []) or []
  277. if isinstance(tc, dict)
  278. }
  279. replayed: list[dict[str, Any]] = []
  280. relocated_cc = None
  281. dropped_blank_text = False
  282. for b in ordered_blocks:
  283. clean = _sanitize_replay_block(b)
  284. if clean is None:
  285. dropped_blank_text = dropped_blank_text or _block_type(b) == "text"
  286. if (cc := _cache_control_of(b)) is not None: # relocate a dropped block's breakpoint
  287. relocated_cc = cc
  288. continue
  289. if clean.get("type") == "tool_use" and (redacted := redacted_input_by_id.get(clean.get("id", ""))) is not None:
  290. clean["input"] = redacted
  291. replayed.append(clean)
  292. # Nothing cacheable survived (e.g. signed thinking + blank text): emit the placeholder so the
  293. # turn stays schema-valid and a relocated marker has a carrier.
  294. if not _has_block_type(replayed, _CACHEABLE_TYPES) and (dropped_blank_text or relocated_cc is not None):
  295. replayed.append(_text_block(_EMPTY_TEXT_PLACEHOLDER))
  296. if not replayed:
  297. return None
  298. _apply_assistant_cache_control_to_last_cacheable_block(replayed, relocated_cc)
  299. _apply_assistant_cache_control_to_last_cacheable_block(replayed, m.get("cache_control"))
  300. # prompt_caching marks an assistant turn with text by writing cache_control INTO ``content``
  301. # (not top-level). This path never reads ``content``, so carry that marker over or the
  302. # breakpoint is burned rather than relocated.
  303. msg_content = m.get("content")
  304. if isinstance(msg_content, list):
  305. inline_cc = next((cc for cc in map(_cache_control_of, msg_content) if cc is not None), None)
  306. _apply_assistant_cache_control_to_last_cacheable_block(replayed, inline_cc)
  307. return replayed
  308. def assistant_replay_carrier(m: dict[str, Any]) -> tuple[bool, list[dict[str, Any]]]:
  309. """``(ordered, blocks)``: the sanitized ordered sidecar when any block survives it (the whole turn
  310. then replays from it), else the ``reasoning_details`` thinking blocks. Single owner of carrier
  311. precedence for conversion, accounting and rejected-signature mirrors."""
  312. ordered_blocks = m.get("anthropic_content_blocks")
  313. if isinstance(ordered_blocks, list) and ordered_blocks:
  314. replayed = _replay_ordered_blocks(m, ordered_blocks)
  315. if replayed:
  316. return True, replayed
  317. return False, _extract_preserved_thinking_blocks(m)
  318. def _convert_assistant_message(m: dict[str, Any]) -> dict[str, Any]:
  319. """Assistant message -> Anthropic content blocks (thinking, text, tool_use, Kimi/DeepSeek
  320. reasoning_content injection)."""
  321. # apply_anthropic_cache_control marks an assistant turn with non-empty text by writing cache_control
  322. # INTO ``content`` (see _apply_cache_marker's list branch), not at the top level. This branch rebuilds
  323. # the message from ordered_blocks and never reads ``content``, so that marker would be dropped -- and
  324. # because _can_carry_marker already counted this message as a carrier, the breakpoint is burned rather
  325. # than relocated. #56195 covered the complementary shape (blank content -> top-level marker); this is
  326. # the interleaved thinking + preamble-text + tool_use shape.
  327. content = m.get("content", "")
  328. ordered, blocks = assistant_replay_carrier(m)
  329. if ordered:
  330. return {"role": "assistant", "content": blocks}
  331. # Blank text blocks are dropped; a cache marker riding on one is relocated onto the last
  332. # surviving cacheable block (prompt_caching sets cache_control on content[-1], which may be
  333. # exactly the blank block).
  334. relocated_cc = None
  335. if isinstance(content, list):
  336. kept, relocated_cc, _ = _split_blank_text_blocks(_convert_content_to_anthropic(content))
  337. blocks.extend(kept)
  338. elif content and str(content).strip():
  339. blocks.append(_text_block(str(content)))
  340. for tc in m.get("tool_calls", []):
  341. if not tc or not isinstance(tc, dict):
  342. continue
  343. fn = tc.get("function", {})
  344. blocks.append(_tool_use_block(tc.get("id", ""), fn.get("name", ""), _parse_tool_args(fn.get("arguments", "{}"))))
  345. # Kimi's /coding endpoint requires reasoning_content on replayed tool-call turns — even ""
  346. # (injected as a fallback upstream). Prepend, since thinking must precede text/tool_use. Skip
  347. # when reasoning_details already supplied (signed) thinking blocks: a duplicate unsigned one
  348. # would be downgraded to a spurious text block on the last assistant message.
  349. # See hermes-agent#13848. Accept empty string "" — _copy_reasoning_content_for_api() injects "" as a
  350. # tier-3 fallback for Kimi tool-call messages that had no reasoning.
  351. reasoning_content = m.get("reasoning_content")
  352. if isinstance(reasoning_content, str) and not _has_block_type(blocks, _THINKING_TYPES):
  353. blocks.insert(0, {"type": "thinking", "thinking": reasoning_content})
  354. # Empty assistant content is rejected. Fall back ONLY to the placeholder, never to raw
  355. # ``content`` — that is the unfiltered blank payload just removed. Markers are applied after
  356. # the fallback so one from a sole dropped blank block lands on the placeholder.
  357. effective = blocks or [_text_block(_EMPTY_TEXT_PLACEHOLDER)]
  358. _apply_assistant_cache_control_to_last_cacheable_block(effective, relocated_cc)
  359. _apply_assistant_cache_control_to_last_cacheable_block(effective, m.get("cache_control"))
  360. return {"role": "assistant", "content": effective}
  361. def _tool_result_content(m: dict[str, Any]) -> Any:
  362. """Resolve a tool message's content into tool_result content (blocks or string)."""
  363. content = m.get("content", "")
  364. multimodal_blocks: Optional[list[dict[str, Any]]] = None
  365. if isinstance(content, dict) and content.get("_multimodal"):
  366. multimodal_blocks = _content_parts_to_anthropic_blocks(content.get("content") or [])
  367. if not multimodal_blocks and content.get("text_summary"):
  368. multimodal_blocks = [_text_block(str(content["text_summary"]))]
  369. elif isinstance(content, list):
  370. converted = _content_parts_to_anthropic_blocks(content)
  371. if _has_block_type(converted, {"image"}):
  372. multimodal_blocks = converted
  373. if multimodal_blocks is None: # back-compat: blocks stashed under a private key
  374. stashed = m.get("_anthropic_content_blocks")
  375. if isinstance(stashed, list) and stashed:
  376. text_content = content if isinstance(content, str) and content.strip() else None
  377. multimodal_blocks = [_text_block(text_content)] + stashed if text_content else list(stashed)
  378. if multimodal_blocks:
  379. return multimodal_blocks
  380. return (content if isinstance(content, str) else json.dumps(content) if content else "") or "(no output)"
  381. def _convert_tool_message_to_result(result: list[dict[str, Any]], m: dict[str, Any]) -> None:
  382. """Append a tool_result to ``result``, merging into a trailing tool_result user message when
  383. there is one. Mutates ``result`` in place."""
  384. tool_result = {
  385. "type": "tool_result", "tool_use_id": _sanitize_tool_id(m.get("tool_call_id", "")),
  386. "content": _tool_result_content(m),
  387. }
  388. _carry_cache_control(tool_result, m, copy=True)
  389. last = result[-1] if result else {}
  390. last_content = last.get("content") if last.get("role") == "user" else None
  391. if isinstance(last_content, list) and last_content and last_content[0].get("type") == "tool_result":
  392. last_content.append(tool_result)
  393. else:
  394. result.append({"role": "user", "content": [tool_result]})
  395. def _convert_user_message(content: Any) -> dict[str, Any]:
  396. """Validate and convert a user message to Anthropic format."""
  397. if isinstance(content, list):
  398. content = _fix_blank_text_blocks_in_list(
  399. _convert_content_to_anthropic(content), placeholder_text="(empty message)",
  400. msg_index=-1, role="user", location="_convert_user_message",
  401. )
  402. elif not content or (isinstance(content, str) and not content.strip()):
  403. content = "(empty message)"
  404. return {"role": "user", "content": content}
  405. def _strip_orphaned_tool_blocks(result: list[dict[str, Any]]) -> None:
  406. """Strip tool_use blocks with no matching tool_result, and vice versa. Compression/truncation
  407. can remove either side of a pair or insert messages between them. Anthropic requires the
  408. tool_result in the IMMEDIATELY FOLLOWING user message — a global id match is not enough.
  409. Mutates ``result`` in place."""
  410. # Pass 1: tool_use without an adjacent result.
  411. for i, m in _assistant_block_lists(result):
  412. tool_use_ids_in_turn = _block_ids(m["content"], "tool_use", "id")
  413. if not tool_use_ids_in_turn:
  414. continue
  415. adjacent_result_ids: set = set()
  416. if i + 1 < len(result):
  417. nxt = result[i + 1]
  418. if nxt.get("role") == "user" and isinstance(nxt.get("content"), list):
  419. adjacent_result_ids = _block_ids(nxt["content"], "tool_result", "tool_use_id")
  420. orphaned = tool_use_ids_in_turn - adjacent_result_ids
  421. if not orphaned:
  422. continue
  423. kept = [b for b in m["content"] if not (_block_type(b) == "tool_use" and b.get("id") in orphaned)]
  424. # A signed thinking block on this turn was signed against the ORIGINAL content and is now
  425. # dead (400 "thinking blocks in the latest assistant message cannot be modified"). Flag so
  426. # _manage_thinking_signatures demotes it.
  427. if len(kept) != len(m["content"]) and _has_block_type(m["content"], _THINKING_TYPES):
  428. m["_thinking_signature_invalidated"] = True
  429. m["content"] = kept if kept else [_text_block("(tool call removed)")]
  430. # Pass 2: tool_result whose tool_use no longer exists anywhere.
  431. surviving_tool_use_ids: set = set()
  432. for _, m in _assistant_block_lists(result):
  433. surviving_tool_use_ids |= _block_ids(m["content"], "tool_use", "id")
  434. for m in result:
  435. if m.get("role") != "user" or not isinstance(m.get("content"), list):
  436. continue
  437. new_content = [
  438. b for b in m["content"] if _block_type(b) != "tool_result" or b.get("tool_use_id") in surviving_tool_use_ids
  439. ]
  440. if len(new_content) != len(m["content"]):
  441. m["content"] = new_content if new_content else [_text_block("(tool result removed)")]
  442. def _concat_content(prev: Any, curr: Any) -> list[Any]:
  443. """Merge two message contents into one block list, each side's blocks kept intact (a string
  444. becomes its own text block). Strings are never joined: the first turn's bytes must equal what a
  445. later request replays as a standalone turn, or the prompt-cache prefix diverges at that block
  446. (MoA appends per-turn guidance after ``user(task)`` on iteration 1 and replays ``user(task)``
  447. alone on iteration 2 — #112358)."""
  448. as_blocks = lambda c: [_text_block(c)] if isinstance(c, str) else list(c)
  449. return as_blocks(prev) + as_blocks(curr)
  450. def _merge_consecutive_roles(result: list[dict[str, Any]]) -> list[dict[str, Any]]:
  451. """Merge consecutive same-role messages to enforce alternation. Returns a new list."""
  452. fixed: list[dict[str, Any]] = []
  453. for m in result:
  454. if not (fixed and fixed[-1]["role"] == m["role"]):
  455. fixed.append(m)
  456. continue
  457. if m["role"] != "user":
  458. # Keep the orphan-strip flag visible to _manage_thinking_signatures.
  459. if m.get("_thinking_signature_invalidated"):
  460. fixed[-1]["_thinking_signature_invalidated"] = True
  461. # The second message's thinking blocks were signed against a different turn boundary
  462. # and become invalid once merged.
  463. if isinstance(m["content"], list):
  464. m["content"] = _strip_thinking(m["content"])
  465. fixed[-1]["content"] = _concat_content(fixed[-1]["content"], m["content"])
  466. return fixed
  467. def _keep_valid_thinking(content: list[Any], signature_dead: bool) -> list[Any]:
  468. """Keep valid signed Anthropic thinking, demoting readable unsigned/invalidated thinking.
  469. Preserved-thinking models apply this to historical turns too; older models apply it only
  470. to the latest assistant turn."""
  471. new_content = []
  472. for b in content:
  473. if _block_type(b) not in _THINKING_TYPES:
  474. new_content.append(b)
  475. continue
  476. is_redacted = b.get("type") == "redacted_thinking"
  477. signed = b.get("data") if is_redacted else b.get("signature") # redacted 'data' IS the signature
  478. if signed and not signature_dead:
  479. new_content.append(b)
  480. elif (signature_dead or not is_redacted) and b.get("thinking"):
  481. new_content.append(_text_block(b["thinking"])) # demote to plain text; dataless redacted dropped
  482. return new_content
  483. def _current_tool_loop_start(result: list[dict[str, Any]]) -> int:
  484. """Index after the last user message carrying no tool result: every assistant turn from there on belongs
  485. to the in-flight tool loop, whose thinking Anthropic requires back. A steer merged into a tool_result
  486. turn still continues the loop."""
  487. for i in range(len(result) - 1, -1, -1):
  488. m = result[i]
  489. if m.get("role") != "user":
  490. continue
  491. content = m.get("content")
  492. if not (isinstance(content, list) and any(_block_type(b) == "tool_result" for b in content)):
  493. return i + 1
  494. return 0
  495. def _manage_thinking_signatures(result: list[dict[str, Any]], base_url: str | None, model: str | None) -> None:
  496. """Strip or preserve thinking blocks per endpoint. Mutates ``result`` in place.
  497. Anthropic signs thinking blocks against the full turn; any upstream mutation invalidates them
  498. (400 "Invalid signature in thinking block"). Signed routes (Anthropic, Nous Portal, Bedrock,
  499. Vertex, Foundry; MiniMax for its own blocks) keep valid signed blocks on every assistant turn
  500. when the model keeps prior thinking, else on every turn of the in-flight tool loop (the API
  501. requires those back and strips older turns itself; cutting at the latest assistant message
  502. broke the loop's earlier steps). Unknown relays cannot verify signatures and strip all; Kimi
  503. replays as-is; DeepSeek needs unsigned blocks round-tripped but rejects signed ones.
  504. """
  505. route = anthropic_thinking_route(base_url, model)
  506. # The latest assistant turn always keeps its thinking, even once a new user message closes its loop.
  507. loop_start = min(_current_tool_loop_start(result),
  508. next((i for i in range(len(result) - 1, -1, -1) if result[i].get("role") == "assistant"), len(result)))
  509. preserve_prior = route_replays_prior_thinking(route, model)
  510. for idx, m in _assistant_block_lists(result):
  511. if route == "kimi":
  512. pass # shared cleanup below still strips cache markers + the flag
  513. elif route == "deepseek":
  514. # Strip signed (or redacted-with-data), keep unsigned.
  515. new_content = [
  516. b for b in m["content"]
  517. if _block_type(b) not in _THINKING_TYPES or not (b.get("signature") or b.get("data"))
  518. ]
  519. m["content"] = new_content or [_text_block("(empty)")]
  520. elif route == "third_party" or (idx < loop_start and not preserve_prior):
  521. m["content"] = _strip_thinking(m["content"]) or [_text_block("(thinking elided)")]
  522. else:
  523. new_content = _keep_valid_thinking(m["content"], bool(m.get("_thinking_signature_invalidated")))
  524. m["content"] = new_content or [_text_block("(empty)")]
  525. # cache_control on thinking blocks interferes with signature validation.
  526. for b in m["content"]:
  527. if _block_type(b) in _THINKING_TYPES:
  528. b.pop("cache_control", None)
  529. m.pop("_thinking_signature_invalidated", None) # internal flag, never on the wire
  530. def _image_source_data(block: Any) -> Optional[str]:
  531. """Base64 payload of a wire ``image`` block, or None for a URL/native source (unmeasurable)."""
  532. source = block.get("source") if isinstance(block, dict) else None
  533. data = source.get("data") if isinstance(source, dict) else None
  534. return data if isinstance(data, str) else None
  535. def _evict_old_screenshots(result: list[dict[str, Any]]) -> None:
  536. """Retire screenshot payloads once the request would cross the API's per-request image limit.
  537. Mutates ``result`` in place. The block ceiling is conditional on the request's actual image
  538. dimensions (the strict 20 only when some image exceeds the many-image per-side cap), so this
  539. pass reads the headers of the ``source.data`` payloads it already holds. It sizes the block
  540. budget AND the byte budget, the latter because the auxiliary Anthropic client
  541. (``agent.auxiliary_client`` via ``anthropic_adapter.build_anthropic_kwargs``) reaches this pass
  542. without the compressor's send-path pass, so it must hold the invariant alone -- previously with
  543. no request-size guard at all (post-#113953 review). Policy and rationale:
  544. :mod:`agent.image_eviction_policy`.
  545. """
  546. reserved = [
  547. (block, _image_source_data(block))
  548. for msg in result
  549. for block in (msg.get("content") if isinstance(msg.get("content"), list) else [])
  550. if _block_type(block) == "image"
  551. ]
  552. # Parallel tool calls land as sibling tool_result blocks inside ONE user message
  553. # (oldest first), so the inner walk must also run newest -> oldest or a batch that
  554. # ends mid-message retires the newest frames instead of the oldest (#103217).
  555. carriers = [
  556. (block, [(b, _image_source_data(b)) for b in block["content"] if _block_type(b) == "image"])
  557. for msg in reversed(result)
  558. for block in reversed(msg.get("content") if isinstance(msg.get("content"), list) else [])
  559. if _block_type(block) == "tool_result"
  560. and isinstance(block.get("content"), list)
  561. and _has_block_type(block["content"], {"image"})
  562. ]
  563. many_image_safe = images_all_within_many_image_limit(
  564. [
  565. *(data for _, data in reserved),
  566. *(data for _, images in carriers for _, data in images),
  567. ]
  568. )
  569. retire = outbound_image_retire_count(
  570. [len(images) for _, images in carriers],
  571. len(reserved),
  572. carrier_bytes_newest_first=[
  573. sum(len(data) for _, data in images if data) for _, images in carriers
  574. ],
  575. reserved_bytes=sum(len(data) for _, data in reserved if data),
  576. many_image_safe=many_image_safe,
  577. )
  578. for block, _ in carriers[len(carriers) - retire:]:
  579. placeholder = _text_block("[screenshot removed to save context]")
  580. block["content"] = [
  581. placeholder if _block_type(b) == "image" else b for b in block["content"]
  582. ]
  583. def _ensure_leading_user_turn(result: list[dict[str, Any]]) -> None:
  584. """Anthropic requires messages[0].role == user; prepend a placeholder turn otherwise. A second
  585. auto-compaction can leave a role=assistant summary first, which the API rejects (often masked
  586. as a misleading tool_use/tool_result 400). The filler must be non-whitespace text or it trades
  587. that 400 for the blank-block one.
  588. The inserted text block must be non-whitespace: Anthropic separately rejects any text content block
  589. whose text is empty or whitespace-only ("text content blocks must contain non-whitespace text"), so a
  590. single space here traded the "leading assistant turn" 400 for that one (#69512 class). Uses the same
  591. placeholder as every other synthesized filler block in this module for consistency.
  592. """
  593. if result and result[0].get("role") != "user":
  594. result.insert(0, {"role": "user", "content": [_text_block(_EMPTY_TEXT_PLACEHOLDER)]})
  595. def _fix_blank_text_blocks_in_list(
  596. blocks: list[Any], *, placeholder_text: str, msg_index: int, role: Any, location: str
  597. ) -> list[Any]:
  598. """Drop blank text blocks; relocate any cache_control they carried onto the last surviving
  599. cacheable block; if nothing survives, substitute one placeholder block (carrying the relocated
  600. marker). Non-text blocks and order are untouched. Returns a new list; logs structure only."""
  601. kept, relocated_cache_control, dropped = _split_blank_text_blocks(blocks)
  602. for block_index in dropped:
  603. logger.warning(
  604. "Pre-call sanitizer: dropped blank text content block "
  605. "(message_index=%d role=%s location=%s block_index=%d block_type=text)",
  606. msg_index, role, location, block_index,
  607. )
  608. kept = kept or [_text_block(placeholder_text)]
  609. _apply_assistant_cache_control_to_last_cacheable_block(kept, relocated_cache_control)
  610. return kept
  611. def _scrub_blank_text_blocks(result: list[dict[str, Any]]) -> None:
  612. """Final boundary guard against blank text blocks (HTTP 400 "text content blocks must contain
  613. non-whitespace text"), including inside tool_result content. Runs LAST so a blank block from
  614. any current or future producer never reaches the wire. Mutates ``result`` in place."""
  615. for msg_index, msg in enumerate(result):
  616. if not isinstance(msg, dict):
  617. continue
  618. role = msg.get("role")
  619. content = msg.get("content")
  620. if not isinstance(content, list) or not content:
  621. continue
  622. msg["content"] = _fix_blank_text_blocks_in_list(
  623. content, placeholder_text=_EMPTY_TEXT_PLACEHOLDER if role == "assistant" else "(empty message)",
  624. msg_index=msg_index, role=role, location="content",
  625. )
  626. for blk in msg["content"]:
  627. inner = blk.get("content") if _block_type(blk) == "tool_result" else None
  628. if isinstance(inner, list) and inner:
  629. blk["content"] = _fix_blank_text_blocks_in_list(
  630. inner, placeholder_text="(no output)", msg_index=msg_index, role=role, location="tool_result",
  631. )
  632. def _convert_system_content(content: Any) -> Any:
  633. """System message content -> Anthropic ``system`` param (str, or block list when cache_control
  634. is present). With cache markers the blocks are copied (never mutating the caller's dicts) and
  635. blank text is replaced by the placeholder: Anthropic rejects blank system blocks too, and a
  636. blank block carrying a breakpoint can't simply be dropped."""
  637. if not isinstance(content, list):
  638. return content
  639. if not any(p.get("cache_control") for p in content if isinstance(p, dict)):
  640. return "\n".join(p["text"] for p in content if p.get("type") == "text")
  641. return [
  642. {**p, "text": _EMPTY_TEXT_PLACEHOLDER} if _is_blank_text_block(p) and isinstance(p.get("text"), str) else p
  643. for p in content
  644. if isinstance(p, dict)
  645. ]
  646. def convert_messages_to_anthropic(
  647. messages: list[dict], base_url: str | None = None, model: str | None = None
  648. ) -> tuple[Optional[Any], list[dict]]:
  649. """Convert OpenAI-format messages to Anthropic format -> ``(system, messages)``. System is
  650. extracted into its own param (a string, or a block list when cache_control is present).
  651. ``base_url``/``model`` drive thinking-signature policy — unknown third-party relays strip
  652. signatures (they cannot verify them); Kimi-family endpoints/models keep unsigned
  653. reasoning_content-derived blocks, which Kimi requires even when empty."""
  654. system = None
  655. result: list[dict[str, Any]] = []
  656. for m in messages:
  657. role = m.get("role", "user")
  658. if role == "system":
  659. system = _convert_system_content(m.get("content", ""))
  660. elif role == "assistant":
  661. result.append(_convert_assistant_message(m))
  662. elif role == "tool":
  663. _convert_tool_message_to_result(result, m)
  664. else:
  665. result.append(_convert_user_message(m.get("content", "")))
  666. _strip_orphaned_tool_blocks(result)
  667. result = _merge_consecutive_roles(result)
  668. _ensure_leading_user_turn(result)
  669. _manage_thinking_signatures(result, base_url, model)
  670. _evict_old_screenshots(result)
  671. _scrub_blank_text_blocks(result)
  672. return system, result