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.
ok add ListContent
thomashebrardbb6b1fd9110d
diff --git a/API_GUIDE.md b/API_GUIDE.mdindex 975d2bfc3..a89f70a77 100644--- a/API_GUIDE.md+++ b/API_GUIDE.md@@ -136,6 +136,7 @@ The `inputs` field uses **ImplicitMemory** format - a smart, flexible way to pro- 1.2: List of strings → `["text1", "text2"]`- 1.3: StuffContent object → `MyClass(arg1="value")`- 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"}`@@ -217,6 +218,34 @@ inputs = {- All items must be of the same type- Concept resolution follows the same rules as 1.3+### 1.5: ListContent of StuffContent Objects++Provide a `ListContent` object containing StuffContent items (Python clients):++```python+# Python client example+from pipelex.core.stuffs.list_content import ListContent++inputs = {+ "invoice_list": ListContent(items=[+ MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")),+ MyConcept(arg1="arg1_2", arg2=2, arg3=MySubClass(arg4="arg4_2"))+ ])+}+```++**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])`++**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)++**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.+---## Case 2: Explicit Format (Concept + Content)diff --git a/pipelex/client/implicit_memory.py b/pipelex/client/implicit_memory.pyindex 939d17940..2c37cf442 100644--- a/pipelex/client/implicit_memory.py+++ b/pipelex/client/implicit_memory.py@@ -23,6 +23,7 @@ class MyConcept(StructuredContent):# 1.2: stuff_data is a list of strings.# 1.3: stuff_data is a StuffContent object.# 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.@@ -79,6 +80,30 @@ stuff14 = Stuff(),)+# 1.5: content is a ListContent of StuffContent objects.+stuff_data15: 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 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.+# If no search domains are provided and we find 2 or more concepts with the same name, it should raise an error.+# 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(+ stuff_code="stuff_code",+ stuff_name="stuff_name",+ concept=ConceptFactory.make(concept_code="MyConcept", domain="domain", description="description", structure_class_name=MyConcept.__name__),+ content=ListContent(+ items=[MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")), MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4"))]+ ),+)+###################################################################################################### CASE 2: Here, we HAVE the key "concept_string" AND the key "content".# The value of the "content" key can Either be a:diff --git a/pipelex/client/protocol.py b/pipelex/client/protocol.pyindex c18571c17..bacc3c0f9 100644--- a/pipelex/client/protocol.py+++ b/pipelex/client/protocol.py@@ -17,6 +17,7 @@ from pipelex.types import StrEnum# - 1.2: Sequence[str] (list of strings)# - 1.3: StuffContent (a StuffContent object)# - 1.4: Sequence[StuffContent] (list of StuffContent objects, covariant)+# - 1.5: ListContent[StuffContent] (a ListContent object containing StuffContent items)# Case 2: Dict with 'concept' AND 'content' keys# - 2.1: {"concept": str, "content": str}# - 2.2: {"concept": str, "content": Sequence[str]}@@ -28,7 +29,7 @@ DictStuffContent = dict[str, Any]StuffContentOrData = (str # Case 1.1| Sequence[str] # Case 1.2- | StuffContent # Case 1.3+ | StuffContent # Case 1.3 (also covers Case 1.5 as ListContent is a StuffContent)| Sequence[StuffContent] # Case 1.4 (covariant - accepts list[TextContent], etc.)| DictStuffContent # Case 2.1, 2.2, 2.3, 2.4, 2.5, 2.6)diff --git a/pipelex/core/stuffs/stuff_factory.py b/pipelex/core/stuffs/stuff_factory.pyindex 048288e42..f7ae750d0 100644--- a/pipelex/core/stuffs/stuff_factory.py+++ b/pipelex/core/stuffs/stuff_factory.py@@ -134,6 +134,7 @@ class StuffFactory:1.2: list[str] → ListContent[TextContent] with Text concept1.3: StuffContent → Use the StuffContent, infer concept from class name1.4: list[StuffContent] → ListContent[StuffContent], infer concept from first item+ 1.5: ListContent[StuffContent] → Use the ListContent, infer concept from first itemCase 2: Dict with 'concept' AND 'content' keys2.1/2.1b: {"concept": "Text"/"native.Text", "content": str} → TextContent with Text concept@@ -247,6 +248,49 @@ 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}."@@ -335,16 +379,16 @@ class StuffFactory:# Case 2.2/2.2b/2.4/2.5/2.6: content is a listif isinstance(content, list):- list_content = cast("list[Any]", content)- if len(list_content) == 0:+ list_content_2 = cast("list[Any]", content)+ if len(list_content_2) == 0:msg = "Cannot create Stuff from empty list in content"raise StuffFactoryError(msg)- first_item = list_content[0]+ first_item = list_content_2[0]# Case 2.2/2.2b: list[str]if isinstance(first_item, str):- for item in list_content:+ for item in list_content_2:if not isinstance(item, str):msg = (f"Trying to create a Stuff '{name}' in the inputs of your pipe, from a list of strings but the item {item} "@@ -354,7 +398,7 @@ class StuffFactory:text_concept = get_native_concept(native_concept=NativeConceptCode.TEXT)if concept_library.is_compatible(tested_concept=concept, wanted_concept=text_concept, strict=True):- items = [TextContent(text=item) for item in list_content]+ items = [TextContent(text=item) for item in list_content_2]return cls.make_stuff(concept=concept,content=ListContent(items=items),@@ -367,7 +411,7 @@ class StuffFactory:# Case 2.4: list[StuffContent]if isinstance(first_item, StuffContent):- for item in list_content:+ for item in list_content_2:if not isinstance(item, type(first_item)):msg = (f"Trying to create a Stuff '{name}' in the inputs of your pipe, from a list of StuffContent "@@ -378,14 +422,14 @@ class StuffFactory:return cls.make_stuff(concept=concept,- content=ListContent(items=cast("list[StuffContent]", list_content)),+ content=ListContent(items=cast("list[StuffContent]", list_content_2)),name=name,code=code,)# Case 2.6: list[dict]if isinstance(first_item, dict):- for item_dict in list_content:+ for item_dict in list_content_2:if not isinstance(item_dict, dict):msg = (f"Trying to create a Stuff '{name}' in the inputs of your pipe, from a list of dicts but "@@ -396,7 +440,7 @@ class StuffFactory:# Create StuffContent objects from dictsstuff_items: list[StuffContent] = []- for item_dict in list_content:+ for item_dict in list_content_2:stuff_content = StuffContentFactory.make_stuff_content_from_concept_with_fallback(concept=concept,value=item_dict,diff --git a/tests/unit/core/stuffs/data.py b/tests/unit/core/stuffs/data.pyindex 62ab66c9a..dccfede13 100644--- a/tests/unit/core/stuffs/data.py+++ b/tests/unit/core/stuffs/data.py@@ -96,6 +96,33 @@ TEST_CASES: list[tuple[str, StuffContentOrData, str, str, Stuff]] = [),),),+ (+ "case-1.5-list-content-of-stuff-content-objects",+ ListContent(+ items=[+ MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")),+ MyConcept(arg1="arg1_2", arg2=2, arg3=MySubClass(arg4="arg4_2")),+ ]+ ),+ "stuff_name",+ "stuff_code",+ Stuff(+ stuff_name="stuff_name",+ stuff_code="stuff_code",+ concept=ConceptFactory.make(+ concept_code="MyConcept",+ domain="test_domain",+ description="Test concept for unit tests",+ structure_class_name="MyConcept",+ ),+ content=ListContent(+ items=[+ MyConcept(arg1="arg1", arg2=1, arg3=MySubClass(arg4="arg4")),+ MyConcept(arg1="arg1_2", arg2=2, arg3=MySubClass(arg4="arg4_2")),+ ]+ ),+ ),+ ),# Case 2.1: Content is a string("case-2.1-dict-with-string-content",diff --git a/tests/unit/pipelex/core/stuffs/test_stuff_factory_make_stuff_from_content.py b/tests/unit/pipelex/core/stuffs/test_stuff_factory_make_stuff_from_content.pyindex e007b5ed4..81135117a 100644--- a/tests/unit/pipelex/core/stuffs/test_stuff_factory_make_stuff_from_content.py+++ b/tests/unit/pipelex/core/stuffs/test_stuff_factory_make_stuff_from_content.py@@ -52,11 +52,10 @@ class TestData:class TestMakeStuffFromStuffContentUsingSearchDomains:def test_listcontent_with_valid_items(self, mocker: MockerFixture):- """Test ListContent with valid items."""list_content: ListContent[TextContent] = ListContent(items=TestData.TEXT_LIST_ITEMS)mock_stuff = mocker.Mock(spec=Stuff)- mock_make = mocker.patch.object(StuffFactory, "make_stuff_using_concept_name_and_search_domains", return_value=mock_stuff)+ mock_make = mocker.patch.object(StuffFactory, "make_stuff_from_stuff_content_or_data", return_value=mock_stuff)result = StuffFactory.make_stuff_from_stuff_content_or_data(name=TestData.STUFF_NAME,