Drop advisor blocks from assistant content for API replay. Mirror of TS ``stripAdvisorBlocks`` at typescript/src/utils/messages.ts:5478. Used on requests that will NOT carry the advisor beta header — the API 400s on advisor blocks in history when the header is absent. When strippin
(
messages: list[dict[str, Any]],
)
| 223 | |
| 224 | |
| 225 | def strip_advisor_blocks( |
| 226 | messages: list[dict[str, Any]], |
| 227 | ) -> list[dict[str, Any]]: |
| 228 | """Drop advisor blocks from assistant content for API replay. |
| 229 | |
| 230 | Mirror of TS ``stripAdvisorBlocks`` at typescript/src/utils/messages.ts:5478. |
| 231 | Used on requests that will NOT carry the advisor beta header — the API |
| 232 | 400s on advisor blocks in history when the header is absent. |
| 233 | |
| 234 | When stripping empties an assistant message (or leaves only |
| 235 | thinking/blank text), inserts a ``[Advisor response]`` placeholder so |
| 236 | the API doesn't reject empty assistant content. |
| 237 | |
| 238 | The input list is not mutated; messages whose content changed are |
| 239 | shallow-cloned, others are passed by reference. |
| 240 | """ |
| 241 | changed = False |
| 242 | result: list[dict[str, Any]] = [] |
| 243 | for msg in messages: |
| 244 | if not isinstance(msg, Mapping) or msg.get("role") != "assistant": |
| 245 | result.append(msg) |
| 246 | continue |
| 247 | content = msg.get("content") |
| 248 | if not isinstance(content, list): |
| 249 | result.append(msg) |
| 250 | continue |
| 251 | filtered = [b for b in content if not is_advisor_block(b)] |
| 252 | if len(filtered) == len(content): |
| 253 | result.append(msg) |
| 254 | continue |
| 255 | changed = True |
| 256 | if not filtered or _content_is_only_placeholders(filtered): |
| 257 | filtered = list(filtered) + [ |
| 258 | {"type": "text", "text": _ADVISOR_PLACEHOLDER_TEXT} |
| 259 | ] |
| 260 | new_msg = dict(msg) |
| 261 | new_msg["content"] = filtered |
| 262 | result.append(new_msg) |
| 263 | return result if changed else messages |
| 264 | |
| 265 | |
| 266 | # --------------------------------------------------------------------------- |