sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[PORT] Richer tool-result truncation placeholders (video/document/json) (TS→Py)

Open strandly-the-agent opened this issue 4 months ago • 1 comments

⚠️ Filed by Strandly as part of the #2498 parity audit. Source feature verified merged + unit-tested in strands-ts. Agent-produced — has Python type-system blockers to resolve first (see gotchas). (No port label yet — a maintainer should add it.)

Parent: #2498

Source SDK

strands-ts

Target SDK

strands-py

Related Files

strands-ts/src/conversation-manager/sliding-window-conversation-manager.ts
strands-ts/src/conversation-manager/__tests__/sliding-window-conversation-manager.test.ts
site/src/content/docs/user-guide/concepts/agents/conversation-management.mdx

What needs to be migrated

Typed truncation placeholders + source-aware branching from _truncateToolResults (sliding-window-conversation-manager.ts:305). Python today has only an image placeholder + text head/tail truncation (sliding_window_conversation_manager.py:301).

Placeholder strings to preserve:

  • image (source-aware): [image: {format}, source: bytes, {N} bytes] / source: url / source: s3
  • video: [video: {format}, source: bytes, {N} bytes] / source: s3
  • document (bytes/s3 only): [document: {name}, {format}, source: bytes, {N} bytes] / source: s3
  • json: [json: {serializedLength} chars] (only when JSON.stringify(json).length > TRUNCATION_THRESHOLD)

Constants: PRESERVE_CHARS=200; preserves tool-result status/error.

Python landing points

strands-py/src/strands/agent/conversation_manager/sliding_window_conversation_manager.py — add _video_placeholder/_document_placeholder/_json_placeholder + branches in the for item in tool_result_items loop (:338), keyed on "image"/"video"/"document"/"json"/"text" dict-key presence. Touches types/tools.py + types/media.py (see blockers).

🚩 Gotchas / open questions (resolve type-system blockers first)

  • Python tool-result blocks can't hold video. ToolResultContent (types/tools.py:70) has only document|image|json|text — no video. A video branch is dead code unless video: VideoContent is added first (VideoContent/VideoSource already exist in media.py). Decide: extend the type or drop video.
  • Python document sources have no text/content variants. DocumentSource (media.py:50) supports only bytes + location (S3) — no documentSourceText/documentSourceContentBlock. So the TS "partial-truncate document text in place" branches cannot be ported; for Python the document port collapses to the bytes/s3 placeholder only.
  • No url image source in Python (only bytes / S3 location) — the TS source: url case doesn't apply; map location→source: s3.
  • Existing image placeholder format will change: Python currently emits [image: png, 1234 bytes]; TS emits [image: png, source: bytes, 1234 bytes]. Aligning matches the published docs but changes a test-covered string — update any Python test asserting the old form.
  • Marker-string divergence is pre-existing (Python ... [truncated: N chars removed] ... vs TS <truncated chars="N"/>) — keep new placeholders consistent with Python's existing text convention; don't blindly copy the TS marker.

Completeness of source (strands-ts)

  • Merged: ✅ · Unit tests: ✅ (image source-kinds, video bytes/s3, document bytes/s3/text/nested, json large/small) · Integ: ❌ none.
  • Docs: ⚠️ shared conversation-management.mdx:110-112 already over-promises for Python (describes typed placeholders Python doesn't implement) — completing this port brings Python in line with the published doc (modulo the video/document limits above).

Confirmation

  • [x] The change is complete in the source SDK (merged + unit-tested); every related artifact is listed above. Type-system blockers + doc gap noted above.

strandly-the-agent avatar Jul 09 '26 19:07 strandly-the-agent

Conversation manager is being deprecated, so these changes may need to be implemented in context manager

jimbrub avatar Oct 06 '26 21:10 jimbrub