Skip to content

Building AI agents, atomically

Simplify VideoURL implementation after review

Eigenwisece7a1d545652
diff --git a/atomic-agents/atomic_agents/agents/atomic_agent.py b/atomic-agents/atomic_agents/agents/atomic_agent.py
index 801026b..10c77aa 100644
--- a/atomic-agents/atomic_agents/agents/atomic_agent.py
+++ b/atomic-agents/atomic_agents/agents/atomic_agent.py
@@ -431,8 +431,8 @@ class AtomicAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:
This method converts instructor multimodal objects (Image, Audio, PDF) to the
OpenAI format that LiteLLM's token counter expects. Text content is also
- converted to the proper multimodal text format when mixed with media. Video
- content parts become text placeholders because LiteLLM cannot count them.
+ converted to the proper multimodal text format when mixed with media. Content-part
+ dicts (e.g. video) become text placeholders because LiteLLM cannot count them.
Returns:
List[Dict[str, Any]]: History messages in LiteLLM-compatible format.
@@ -463,10 +463,11 @@ class AtomicAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:
f"Using placeholder for estimation."
)
serialized_content.append({"type": "text", "text": f"[{media_type.lower()} content]"})
- elif isinstance(item, dict) and item.get("type") == "video_url":
- # LiteLLM's token counter raises on video_url parts, so use a
- # placeholder for estimation, same as failed media serialization.
- serialized_content.append({"type": "text", "text": "[video content]"})
+ elif isinstance(item, dict):
+ # get_history() emits pre-lowered content-part dicts (e.g. video).
+ # LiteLLM's token counter only accepts text and image_url parts,
+ # so estimate them with a placeholder.
+ serialized_content.append({"type": "text", "text": f"[{item.get('type', 'unknown')} content]"})
else:
# Unknown type - convert to string
serialized_content.append({"type": "text", "text": str(item)})
diff --git a/atomic-agents/atomic_agents/base/multimodal.py b/atomic-agents/atomic_agents/base/multimodal.py
index 69f6d4b..e9e2df8 100644
--- a/atomic-agents/atomic_agents/base/multimodal.py
+++ b/atomic-agents/atomic_agents/base/multimodal.py
@@ -32,9 +32,4 @@ class VideoURL(BaseModel):
Dict[str, Any]: A ``{"type": "video_url", "video_url": {...}}`` content part,
omitting optional parameters that were not set.
"""
- video_url: Dict[str, Any] = {"url": self.url}
- if self.fps is not None:
- video_url["fps"] = self.fps
- if self.detail is not None:
- video_url["detail"] = self.detail
- return {"type": "video_url", "video_url": video_url}
+ return {"type": "video_url", "video_url": self.model_dump(exclude_none=True)}
diff --git a/atomic-agents/atomic_agents/context/chat_history.py b/atomic-agents/atomic_agents/context/chat_history.py
index d4ec83a..b679a74 100644
--- a/atomic-agents/atomic_agents/context/chat_history.py
+++ b/atomic-agents/atomic_agents/context/chat_history.py
@@ -12,11 +12,7 @@ from atomic_agents.base.multimodal import VideoURL
from atomic_agents.context.base_chat_history import BaseChatHistory
-INSTRUCTOR_MULTIMODAL_TYPES = (Image, Audio, PDF)
-MULTIMODAL_TYPES = INSTRUCTOR_MULTIMODAL_TYPES + (VideoURL,)
-
-MultimodalObject = Union[Image, Audio, PDF, VideoURL]
-ExcludeSpec = Union[Dict, bool, None]
+MULTIMODAL_TYPES = (Image, Audio, PDF, VideoURL)
class Message(BaseModel):
@@ -114,8 +110,9 @@ class ChatHistory(BaseChatHistory):
if multimodal_objects:
processed_content = []
- excluded_fields = exclude_spec if isinstance(exclude_spec, dict) else None
- content_json = input_content.model_dump_json(exclude=excluded_fields)
+ # Message.content is a BaseIOSchema, so extraction never excludes the whole object.
+ assert not isinstance(exclude_spec, bool)
+ content_json = input_content.model_dump_json(exclude=exclude_spec)
if content_json and content_json != "{}":
processed_content.append(content_json)
# Instructor forwards dict content parts to the provider unchanged but
@@ -132,7 +129,7 @@ class ChatHistory(BaseChatHistory):
return history
@staticmethod
- def _extract_multimodal_info(obj) -> Tuple[List[MultimodalObject], ExcludeSpec]:
+ def _extract_multimodal_info(obj) -> Tuple[List[Union[Image, Audio, PDF, VideoURL]], Union[Dict, bool, None]]:
"""
Recursively extract multimodal objects and build a Pydantic-compatible exclude spec.
diff --git a/atomic-agents/tests/agents/test_atomic_agent.py b/atomic-agents/tests/agents/test_atomic_agent.py
index 90622af..504cdd3 100644
--- a/atomic-agents/tests/agents/test_atomic_agent.py
+++ b/atomic-agents/tests/agents/test_atomic_agent.py
@@ -4,7 +4,6 @@ from enum import Enum
from pydantic import BaseModel, Field
from pydantic import ValidationError
import instructor
-import litellm
from atomic_agents import (
BaseIOSchema,
AtomicAgent,
@@ -1129,9 +1128,8 @@ def test_serialize_history_for_token_count_video_placeholder(agent, mock_history
assert serialized[0]["content"] == [
{"type": "text", "text": '{"prompt":"Summarize the video"}'},
- {"type": "text", "text": "[video content]"},
+ {"type": "text", "text": "[video_url content]"},
]
- assert litellm.token_counter(model="gpt-4o", messages=serialized) > 0
# --- Tests for tool_result_role and Gemini system message remapping (issue #221) ---