Format messages for debug output, truncating long content. Accepts both plain-string content and the list-of-blocks shape used by cache_control-tagged messages (joins all text blocks for preview).
(messages: list[dict], max_content: int = 200)
| 371 | |
| 372 | |
| 373 | def _fmt_messages(messages: list[dict], max_content: int = 200) -> str: |
| 374 | """Format messages for debug output, truncating long content. |
| 375 | |
| 376 | Accepts both plain-string content and the list-of-blocks shape used by |
| 377 | cache_control-tagged messages (joins all text blocks for preview). |
| 378 | """ |
| 379 | parts = [] |
| 380 | for msg in messages: |
| 381 | role = msg["role"] |
| 382 | raw = msg["content"] |
| 383 | if isinstance(raw, list): |
| 384 | text = "".join(b.get("text", "") for b in raw if isinstance(b, dict)) |
| 385 | else: |
| 386 | text = raw |
| 387 | if len(text) > max_content: |
| 388 | preview = text[:max_content] + f"... ({len(text)} chars)" |
| 389 | else: |
| 390 | preview = text |
| 391 | parts.append(f" [{role}] {preview}") |
| 392 | return "\n".join(parts) |
| 393 | |
| 394 | |
| 395 | class TruncatedResponseError(Exception): |
no test coverage detected