Skip to content

Conversation

@majiayu000
Copy link

Summary

  • Fixed token explosion issue when passing BinaryImage to marvin.cast()
  • UserContent types (images, audio, documents, video) are now properly handled as attachments rather than serialized into context

Problem

When passing a BinaryImage to marvin.cast(), the binary data was being serialized into the task context as a string (base64), causing token explosion (207K+ tokens for a ~200KB image).

Solution

Added detection for pydantic-ai's UserContent types and pass them as task attachments instead of putting them in the context dictionary.

Test Plan

  • Added unit tests for _is_user_content() helper function
  • All basic tests pass (267 passed)
  • All pre-commit checks pass

Fixes #1246

@github-actions github-actions bot added the tests label Dec 27, 2025
Previously, when passing a BinaryImage or other attachment types to
marvin.cast(), the data was serialized as text in the context dictionary,
causing token explosion (e.g., 207k+ tokens for a small image).

This fix:
- Detects UserContent attachment types (ImageUrl, AudioUrl, DocumentUrl,
  VideoUrl, BinaryContent) when passed as data to cast()
- Passes these as task attachments instead of serializing them in context
- Adds a placeholder "(provided as attachment)" in context

Fixes PrefectHQ#1246

Signed-off-by: majiayu000 <[email protected]>
@majiayu000 majiayu000 force-pushed the fix/binary-image-token-explosion branch from ba25a72 to fea040d Compare December 30, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BinaryImage causes token explosion when passed to marvin.cast()

1 participant