Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Add ListContent check

thomashebrard4f5bc498fac2
diff --git a/API_GUIDE.md b/API_GUIDE.md
index a89f70a77..e4090fe30 100644
--- a/API_GUIDE.md
+++ b/API_GUIDE.md
@@ -135,8 +135,9 @@ The `inputs` field uses **ImplicitMemory** format - a smart, flexible way to pro
- 1.1: String → `"my text"`
- 1.2: List of strings → `["text1", "text2"]`
- 1.3: StuffContent object → `MyClass(arg1="value")`
+ - 1.3a: Regular StuffContent → Infer concept from class name
+ - 1.3b: ListContent[StuffContent] → Infer concept from first item's class (subcase)
- 1.4: List of StuffContent objects → `[MyClass(...), MyClass(...)]`
-- 1.5: ListContent of StuffContent objects → `ListContent(items=[MyClass(...), MyClass(...)])`
**Case 2: Explicit Format** - Use `{"concept": "...", "content": "..."}` for control
- 2.1: String with concept → `{"concept": "Text", "content": "my text"}`
@@ -218,7 +219,9 @@ inputs = {
- All items must be of the same type
- Concept resolution follows the same rules as 1.3
-### 1.5: ListContent of StuffContent Objects
+### 1.3b: ListContent of StuffContent Objects (Subcase of 1.3)
+
+Since `ListContent` is itself a `StuffContent`, this is a special subcase of 1.3 where the content is inferred from the items inside rather than from the `ListContent` class itself.
Provide a `ListContent` object containing StuffContent items (Python clients):
@@ -235,14 +238,19 @@ inputs = {
```
**Key Differences from Case 1.4:**
-- Case 1.4 uses a plain Python list: `[item1, item2]`
-- Case 1.5 uses a `ListContent` wrapper: `ListContent(items=[item1, item2])`
+- Case 1.4 uses a plain Python list: `[item1, item2]` → Creates a new `ListContent` wrapper
+- Case 1.3b uses an existing `ListContent` wrapper: `ListContent(items=[item1, item2])` → Uses the wrapper directly
+
+**Why is this a subcase of 1.3?**
+- `ListContent` is a subclass of `StuffContent`
+- But we don't infer the concept from "ListContent" itself
+- Instead, we look inside and infer from the first item's class
**Requirements:**
- All items within the `ListContent` must be subclasses of `StuffContent`
- All items must be of the same type
- The `ListContent` cannot be empty
-- Concept resolution follows the same rules as 1.3 (inferred from the first item's class name)
+- Concept resolution follows Case 1.3 rules (inferred from the first item's class name, not from ListContent)
**Use Case:** This format is useful when you already have data wrapped in a `ListContent` object from a previous pipeline execution or when working with Pipelex's internal data structures.
diff --git a/pipelex/client/implicit_memory.py b/pipelex/client/implicit_memory.py
index 2c37cf442..0d2926414 100644
--- a/pipelex/client/implicit_memory.py
+++ b/pipelex/client/implicit_memory.py
@@ -22,8 +22,9 @@ class MyConcept(StructuredContent):
# 1.1: stuff_data is a string.
# 1.2: stuff_data is a list of strings.
# 1.3: stuff_data is a StuffContent object.
+# 1.3a: Regular StuffContent object
+# 1.3b: ListContent of StuffContent objects (special subcase)
# 1.4: stuff_data is a list of StuffContent objects.
-# 1.5: stuff_data is a ListContent of StuffContent objects.
####################################################################################################
# 1.1: Content is a string.
@@ -80,14 +81,15 @@ stuff14 = Stuff(
),
)
-# 1.5: content is a ListContent of StuffContent objects.
-stuff_data15: ListContent[MyConcept] = ListContent(
+# 1.3b (formerly 1.5): content is a ListContent of StuffContent objects.
+# This is a special subcase of 1.3 since ListContent is itself a StuffContent.
+stuff_data13b: ListContent[MyConcept] = ListContent(
items=[MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")), MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4"))]
)
# It should check that every item in the list is a subclass of StuffContent.
# If not, it should raise an error.
-# If every item is of the same type, it should create a Stuff with the concept of the first item.
+# If every item is of the same type, it should create a Stuff with the concept of the first item (not from ListContent itself).
# If every item is not of the same type, it should raise an error.
# If the ListContent is empty, it should raise an error.
# If no concept is found, it should raise an error. To find the concept, it should look in all the library for the concept with the same name.
@@ -95,7 +97,7 @@ stuff_data15: ListContent[MyConcept] = ListContent(
# If the domain is specified or found, it should check that the structure corresponds with the given content.
# Otherwise it should raise an error.
-stuff15 = Stuff(
+stuff13b = Stuff(
stuff_code="stuff_code",
stuff_name="stuff_name",
concept=ConceptFactory.make(concept_code="MyConcept", domain="domain", description="description", structure_class_name=MyConcept.__name__),
diff --git a/pipelex/core/stuffs/stuff_factory.py b/pipelex/core/stuffs/stuff_factory.py
index f7ae750d0..ea487a294 100644
--- a/pipelex/core/stuffs/stuff_factory.py
+++ b/pipelex/core/stuffs/stuff_factory.py
@@ -133,8 +133,9 @@ class StuffFactory:
1.1: str → TextContent with Text concept
1.2: list[str] → ListContent[TextContent] with Text concept
1.3: StuffContent → Use the StuffContent, infer concept from class name
+ 1.3a: Regular StuffContent → Infer concept from the class itself
+ 1.3b (was 1.5): ListContent[StuffContent] → Infer concept from first item's class
1.4: list[StuffContent] → ListContent[StuffContent], infer concept from first item
- 1.5: ListContent[StuffContent] → Use the ListContent, infer concept from first item
Case 2: Dict with 'concept' AND 'content' keys
2.1/2.1b: {"concept": "Text"/"native.Text", "content": str} → TextContent with Text concept
@@ -160,9 +161,42 @@ class StuffFactory:
# Case 1.3: StuffContent object → Infer concept from class name
if issubclass(type(stuff_content_or_data), StuffContent):
- # Get the concept from the StuffContent class name
- content_class_name = stuff_content_or_data.__class__.__name__
stuff_content = cast("StuffContent", stuff_content_or_data)
+
+ # Case 1.3b: Special handling for ListContent (subcase of 1.3)
+ if isinstance(stuff_content_or_data, ListContent):
+ list_content = cast("ListContent[StuffContent]", stuff_content_or_data)
+
+ if len(list_content.items) == 0:
+ msg = f"Cannot create Stuff '{name}' from empty ListContent"
+ raise StuffFactoryError(msg)
+
+ first_item = list_content.items[0]
+
+ # Check that items are StuffContent
+ if not isinstance(first_item, StuffContent): # pyright: ignore[reportUnnecessaryIsInstance]
+ msg = (
+ f"Trying to create a Stuff '{name}' from a ListContent but "
+ f"the items are not StuffContent. First item is of type {type(first_item).__name__}. "
+ "ListContent items must be subclasses of StuffContent."
+ )
+ raise StuffFactoryError(msg)
+
+ # Check all items are of the same type
+ for item in list_content.items:
+ if not isinstance(item, type(first_item)):
+ msg = (
+ f"Trying to create a Stuff '{name}' from a ListContent of '{type(first_item).__name__}' "
+ f"but the items are not of the same type. Especially, items {item} is of type {type(item).__name__}. "
+ "Every items of the list should be an identical type."
+ )
+ raise StuffFactoryError(msg)
+
+ # Get concept from first item's class name
+ content_class_name = type(first_item).__name__
+ else:
+ # Case 1.3a: Regular StuffContent → Get concept from the StuffContent class name itself
+ content_class_name = stuff_content_or_data.__class__.__name__
# Check if it's a native concept
if "Content" in content_class_name and NativeConceptCode.is_native_concept(concept_code=content_class_name.split("Content")[0]):
@@ -248,49 +282,6 @@ class StuffFactory:
msg = f"Cannot create Stuff from list of {type(first_item)}. Type should be {StuffContentOrData}."
raise StuffFactoryError(msg)
- # Case 1.5: ListContent[StuffContent] → Use the ListContent, infer concept from first item
- if isinstance(stuff_content_or_data, ListContent):
- list_content_15 = cast("ListContent[StuffContent]", stuff_content_or_data)
-
- if len(list_content_15.items) == 0:
- msg = f"Cannot create Stuff '{name}' from empty ListContent"
- raise StuffFactoryError(msg)
-
- first_item = list_content_15.items[0]
- content_class_name = type(first_item).__name__
-
- # Check all items are of the same type
- for item in list_content_15.items:
- if not isinstance(item, type(first_item)):
- msg = (
- f"Trying to create a Stuff '{name}' from a ListContent of '{type(first_item).__name__}' "
- f"but the items are not of the same type. Especially, items {item} is of type {type(item).__name__}. "
- "Every items of the list should be an identical type."
- )
- raise StuffFactoryError(msg)
-
- # Check if it's a native concept
- if "Content" in content_class_name and NativeConceptCode.is_native_concept(concept_code=content_class_name.split("Content")[0]):
- concept = get_native_concept(native_concept=NativeConceptCode(content_class_name.split("Content")[0]))
- else:
- try:
- concept = concept_library.get_required_concept_from_concept_string_or_code(
- concept_string_or_code=content_class_name, search_domains=search_domains
- )
- except ConceptLibraryConceptNotFoundError as exc:
- msg = (
- f"Trying to create a Stuff '{name}' from a ListContent but "
- f"the concept of name '{content_class_name}' is not found in the library"
- )
- raise StuffFactoryError(msg) from exc
-
- return cls.make_stuff(
- concept=concept,
- content=list_content_15,
- name=name,
- code=code,
- )
-
# ==================== CASE 2: Dict with 'concept' AND 'content' keys ====================
if not isinstance(stuff_content_or_data, dict):
msg = f"Unexpected type for stuff_content_or_data: {type(stuff_content_or_data)}.Type should be {StuffContentOrData}."
diff --git a/tests/unit/core/stuffs/data.py b/tests/unit/core/stuffs/data.py
index dccfede13..aaa438d89 100644
--- a/tests/unit/core/stuffs/data.py
+++ b/tests/unit/core/stuffs/data.py
@@ -52,6 +52,42 @@ TEST_CASES: list[tuple[str, StuffContentOrData, str, str, Stuff]] = [
),
),
),
+ # Case 1.2b: Content is a TextContent object (native concept)
+ (
+ "case-1.2b-text-content-object",
+ TextContent(text="Lorem Ipsum"),
+ "stuff_name",
+ "stuff_code",
+ Stuff(
+ stuff_name="stuff_name",
+ stuff_code="stuff_code",
+ concept=ConceptFactory.make_native_concept(native_concept_code=NativeConceptCode.TEXT),
+ content=TextContent(text="Lorem Ipsum"),
+ ),
+ ),
+ # Case 1.2c: Content is a list of TextContent objects (native concept)
+ (
+ "case-1.2c-list-of-text-content-objects",
+ [
+ TextContent(text="Lorem Ipsum 1"),
+ TextContent(text="Lorem Ipsum 2"),
+ TextContent(text="Lorem Ipsum 3"),
+ ],
+ "stuff_name",
+ "stuff_code",
+ Stuff(
+ stuff_name="stuff_name",
+ stuff_code="stuff_code",
+ concept=ConceptFactory.make_native_concept(native_concept_code=NativeConceptCode.TEXT),
+ content=ListContent(
+ items=[
+ TextContent(text="Lorem Ipsum 1"),
+ TextContent(text="Lorem Ipsum 2"),
+ TextContent(text="Lorem Ipsum 3"),
+ ]
+ ),
+ ),
+ ),
# Case 1.3: Content is a StuffContent object
(
"case-1.3-stuff-content-object",
diff --git a/tests/unit/core/stuffs/test_stuff_factory_implicit_memory.py b/tests/unit/core/stuffs/test_stuff_factory_implicit_memory.py
index 7f3e7219d..d36cee690 100644
--- a/tests/unit/core/stuffs/test_stuff_factory_implicit_memory.py
+++ b/tests/unit/core/stuffs/test_stuff_factory_implicit_memory.py
@@ -3,7 +3,7 @@ from typing import Any
import pytest
-from pipelex import log
+from pipelex import log, pretty_print
from pipelex.client.protocol import StuffContentOrData
from pipelex.core.concepts.concept_factory import ConceptFactory
from pipelex.core.stuffs.structured_content import StructuredContent
@@ -86,11 +86,14 @@ class TestStuffFactoryImplicitMemory:
log.info(f"Testing case: {test_name}")
log.debug(f"setup_test_concept: {setup_test_concept}")
+
result = StuffFactory.make_stuff_from_stuff_content_or_data(
name=stuff_name,
code=stuff_code,
stuff_content_or_data=stuff_content_or_data,
)
+ pretty_print(result, title=f"Result for test case: {test_name}")
+ pretty_print(expected_stuff, title=f"Expected stuff for test case: {test_name}")
assert result == expected_stuff, f"Failed for test case: {test_name}"