Skip to content

Building AI agents, atomically

Address PR review: tuple/dict support, direct unit tests, dict coverage

KennyVaneetvelded137da25cffd
diff --git a/atomic-agents/atomic_agents/context/chat_history.py b/atomic-agents/atomic_agents/context/chat_history.py
index 23f88e4..44f1496 100644
--- a/atomic-agents/atomic_agents/context/chat_history.py
+++ b/atomic-agents/atomic_agents/context/chat_history.py
@@ -151,7 +151,7 @@ class ChatHistory:
exclude[field_name] = sub_exclude
return all_objects, (exclude if exclude else None)
- if isinstance(obj, list):
+ if isinstance(obj, (list, tuple)):
all_objects = []
exclude = {}
for i, item in enumerate(obj):
@@ -174,7 +174,12 @@ class ChatHistory:
if objects:
all_objects.extend(objects)
exclude[k] = sub_exclude
- return all_objects, (exclude if exclude else None)
+ if not all_objects:
+ return [], None
+ # If every value in the dict is fully multimodal, exclude the entire field
+ if len(exclude) == len(obj) and all(v is True for v in exclude.values()):
+ return all_objects, True
+ return all_objects, exclude
return [], None
diff --git a/atomic-agents/tests/context/test_chat_history.py b/atomic-agents/tests/context/test_chat_history.py
index 2f850c9..ec792b0 100644
--- a/atomic-agents/tests/context/test_chat_history.py
+++ b/atomic-agents/tests/context/test_chat_history.py
@@ -637,6 +637,7 @@ def test_get_history_mixed_nested_and_toplevel_multimodal(history):
assert len(result) == 1
assert isinstance(result[0]["content"], list)
+ assert len(result[0]["content"]) == 3 # JSON + 2 images
json_part = json.loads(result[0]["content"][0])
assert json_part["text"] == "Check this out"
assert json_part["attachment"] == {"caption": "See here"}
@@ -674,6 +675,7 @@ def test_get_history_list_of_nested_schemas_with_multimodal(history):
assert len(result) == 1
assert isinstance(result[0]["content"], list)
+ assert len(result[0]["content"]) == 3 # JSON + 2 PDFs
json_part = json.loads(result[0]["content"][0])
assert json_part["documents"] == [{"title": "First"}, {"title": "Second"}]
assert pdf1 in result[0]["content"]
@@ -698,6 +700,7 @@ def test_get_history_only_multimodal_fields(history):
assert len(result) == 1
assert isinstance(result[0]["content"], list)
# No JSON string should be present since all fields are multimodal
+ assert len(result[0]["content"]) == 2
assert all(not isinstance(item, str) for item in result[0]["content"])
assert img1 in result[0]["content"]
assert img2 in result[0]["content"]
@@ -720,3 +723,157 @@ def test_get_history_no_multimodal_unchanged(history):
assert result[0]["role"] == "user"
assert isinstance(result[0]["content"], str)
assert json.loads(result[0]["content"]) == {"text": "hello", "count": 42}
+
+
+# ---------------------------------------------------------------------------
+# Direct unit tests for _extract_multimodal_info
+# ---------------------------------------------------------------------------
+
+
+def test_extract_multimodal_info_plain_schema():
+ """No multimodal content returns empty list and None exclude spec"""
+
+ class Plain(BaseIOSchema):
+ """Plain schema."""
+
+ text: str = Field(..., description="Text")
+
+ objs, spec = ChatHistory._extract_multimodal_info(Plain(text="hello"))
+ assert objs == []
+ assert spec is None
+
+
+def test_extract_multimodal_info_toplevel_image():
+ """Top-level Image returns the object and True exclude spec"""
+ img = instructor.Image(source="url", media_type="image/jpeg", detail="low")
+ objs, spec = ChatHistory._extract_multimodal_info(img)
+ assert objs == [img]
+ assert spec is True
+
+
+def test_extract_multimodal_info_nested_schema():
+ """Nested schema with multimodal returns correct exclude spec shape"""
+
+ class Inner(BaseIOSchema):
+ """Inner schema."""
+
+ image: instructor.Image = Field(..., description="Image")
+ label: str = Field(..., description="Label")
+
+ class Outer(BaseIOSchema):
+ """Outer schema."""
+
+ inner: Inner = Field(..., description="Inner")
+ text: str = Field(..., description="Text")
+
+ img = instructor.Image(source="url", media_type="image/jpeg", detail="low")
+ objs, spec = ChatHistory._extract_multimodal_info(
+ Outer(inner=Inner(image=img, label="test"), text="hello")
+ )
+
+ assert objs == [img]
+ # Exclude spec should be {"inner": {"image": True}}
+ assert spec == {"inner": {"image": True}}
+
+
+def test_extract_multimodal_info_list_all_multimodal():
+ """List where every item is multimodal collapses to True"""
+
+ class Schema(BaseIOSchema):
+ """Schema with all-multimodal list."""
+
+ images: List[instructor.Image] = Field(..., description="Images")
+
+ img1 = instructor.Image(source="url1", media_type="image/jpeg", detail="low")
+ img2 = instructor.Image(source="url2", media_type="image/jpeg", detail="low")
+ objs, spec = ChatHistory._extract_multimodal_info(
+ Schema(images=[img1, img2])
+ )
+
+ assert objs == [img1, img2]
+ # All items multimodal → field excluded entirely
+ assert spec == {"images": True}
+
+
+def test_extract_multimodal_info_list_partial_multimodal():
+ """List with mixed content returns index-based exclude spec"""
+
+ class Schema(BaseIOSchema):
+ """Schema with mixed list."""
+
+ items: List[Union[str, instructor.Image]] = Field(..., description="Items")
+
+ img = instructor.Image(source="url", media_type="image/jpeg", detail="low")
+ objs, spec = ChatHistory._extract_multimodal_info(
+ Schema(items=["text", img])
+ )
+
+ assert objs == [img]
+ # Only index 1 is multimodal
+ assert spec == {"items": {1: True}}
+
+
+def test_extract_multimodal_info_tuple_support():
+ """Tuples are handled identically to lists"""
+ img = instructor.Image(source="url", media_type="image/jpeg", detail="low")
+ objs, spec = ChatHistory._extract_multimodal_info((img, "text"))
+ assert objs == [img]
+ assert spec == {0: True}
+
+
+def test_extract_multimodal_info_dict_with_multimodal():
+ """Dict values containing multimodal objects return correct exclude spec"""
+ img = instructor.Image(source="url", media_type="image/jpeg", detail="low")
+ objs, spec = ChatHistory._extract_multimodal_info(
+ {"key1": img, "key2": "text"}
+ )
+
+ assert objs == [img]
+ assert spec == {"key1": True}
+
+
+def test_extract_multimodal_info_dict_all_multimodal():
+ """Dict where all values are multimodal collapses to True"""
+ img1 = instructor.Image(source="url1", media_type="image/jpeg", detail="low")
+ img2 = instructor.Image(source="url2", media_type="image/jpeg", detail="low")
+ objs, spec = ChatHistory._extract_multimodal_info(
+ {"a": img1, "b": img2}
+ )
+
+ assert objs == [img1, img2]
+ assert spec is True
+
+
+def test_extract_multimodal_info_dict_no_multimodal():
+ """Dict with no multimodal returns empty list and None"""
+ objs, spec = ChatHistory._extract_multimodal_info(
+ {"key1": "text", "key2": 42}
+ )
+ assert objs == []
+ assert spec is None
+
+
+def test_get_history_dict_of_images(history):
+ """Dict[str, Image] field exercises the dict code path in get_history"""
+
+ class DictImageSchema(BaseIOSchema):
+ """Schema with dict of images."""
+
+ image_map: Dict[str, instructor.Image] = Field(..., description="Named images")
+ note: str = Field(..., description="A note")
+
+ img_a = instructor.Image(source="url_a", media_type="image/jpeg", detail="low")
+ img_b = instructor.Image(source="url_b", media_type="image/jpeg", detail="low")
+ content = DictImageSchema(image_map={"front": img_a, "back": img_b}, note="Two views")
+
+ history.add_message("user", content)
+ result = history.get_history()
+
+ assert len(result) == 1
+ assert isinstance(result[0]["content"], list)
+ assert len(result[0]["content"]) == 3 # JSON + 2 images
+ json_part = json.loads(result[0]["content"][0])
+ assert json_part["note"] == "Two views"
+ assert "image_map" not in json_part
+ assert img_a in result[0]["content"]
+ assert img_b in result[0]["content"]