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.
feature/Storage-key-extension · L-260928-59eb7b — jpg fallback in storage keys (#1295)
Louis Choquel8c93fe31f178
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 738e744d4..fe3578177 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1,5 +1,11 @@# Changelog+## [Unreleased]++### Fixed++- **A generated object's storage key names its own format**: the extension of a key under `generated/` follows the whole MIME type the object is stored under, so a fetched `image/svg+xml` is stored as `.svg`, and a type with no extension of its own gets `.bin`, where both used to get `.jpg`. The MIME type's parameters and case no longer change the extension.+## [v0.69.0] - 2026-09-28### Highlightsdiff --git a/docs/under-the-hood/distributed-content-generation.md b/docs/under-the-hood/distributed-content-generation.mdindex 9841cf304..179f022d7 100644--- a/docs/under-the-hood/distributed-content-generation.md+++ b/docs/under-the-hood/distributed-content-generation.md@@ -150,6 +150,8 @@ async def generate_and_store_images(img_gen_assignment):)```+Each object is stored under `{storage_scope}/generated/<filename>`, the filename rendered from the storage method's `uri_format` (`{hash}.{extension}` by default). The extension follows the MIME type the object is stored under, so the key and the stored content type always agree: `jpg` for `image/jpeg`, `svg` for `image/svg+xml`, and `bin` for a type with no extension of its own.+!!! info "What crosses the boundary"`ImageContent` carries `url` (storage URI), `public_url`, `mime_type`, paired `width`/`height`, and `caption` — but never raw bytes. The `url` can be an S3 URI, HTTP URL, or local file path depending on storage configuration.diff --git a/pipelex/cogt/content_generation/generated_content_factory.py b/pipelex/cogt/content_generation/generated_content_factory.pyindex 146c46f94..7c55916e5 100644--- a/pipelex/cogt/content_generation/generated_content_factory.py+++ b/pipelex/cogt/content_generation/generated_content_factory.py@@ -14,6 +14,7 @@ from pipelex.system.storage_scope import GENERATED_CONTENT_LEAFfrom pipelex.tools.misc.base64_utils import extract_base64_str_from_base64_url_if_possiblefrom pipelex.tools.misc.exceptions import RemoteFileFetchErrorfrom pipelex.tools.misc.file_fetch_utils import fetch_file_and_content_type_from_url_httpx+from pipelex.tools.misc.filetype_utils import UNKNOWN_FILE_TYPE, mime_type_to_extensionfrom pipelex.tools.misc.image_utils import ImageFormatfrom pipelex.tools.storage.storage_provider_abstract import StorageProviderAbstract@@ -44,6 +45,9 @@ class GeneratedContentFactory:resulting content reports. It alone decides the file extension,so the key can never disagree with the content's `mime_type`(a provider may return a different format than requested).+ Its parameters and case do not change the extension, and a type+ that yields no extension of its own gets `bin`, never another+ format's.Returns:`{storage_scope}/generated/<filename>`, where the filename is@@ -51,8 +55,9 @@ class GeneratedContentFactory:"""hash_digest = hashlib.sha256(data).hexdigest()[:16]- match mime_type:- case "image/jpeg":+ media_type = mime_type.split(";", 1)[0].strip().lower()+ match media_type:+ case "image/jpeg" | "image/jpg":extension = "jpg"case "image/png":extension = "png"@@ -60,12 +65,18 @@ class GeneratedContentFactory:extension = "webp"case _:# A media type this runtime does not itself produce can still arrive,- # because a remote URL may serve one. The extension follows the subtype- # so that the key keeps agreeing with the content type the object is- # stored under; a subtype that is not a plain token (`svg+xml`) would- # not make a filename, and falls back with the key alone imprecise.- subtype = mime_type.removeprefix("image/")- extension = subtype if subtype.isalnum() else "jpg"+ # because a remote URL may serve one (`image/gif`, `image/svg+xml`).+ # The extension is looked up from the whole media type, so the key+ # keeps agreeing with the content type the object is stored under:+ # `image/svg+xml` makes `svg`. A subtype the MIME table does not know+ # still names itself when it is a plain token (`image/jxl` makes+ # `jxl`); anything else gets `bin`, which claims no format at all.+ # It used to get `jpg`, which put an SVG under a JPEG's name.+ extension = mime_type_to_extension(media_type)+ if extension == UNKNOWN_FILE_TYPE:+ subtype = media_type.removeprefix("image/")+ is_plain_image_subtype = media_type.startswith("image/") and subtype.isascii() and subtype.isalnum()+ extension = subtype if is_plain_image_subtype else "bin"# The `generated/` leaf is HARDCODED, and `uri_format` renders only the# filename under it. It used to render the whole key, `{storage_scope}/…`,# which put generated bytes loose at the root of the scope while thediff --git a/tests/unit/pipelex/cogt/content_generation/test_generated_content_factory.py b/tests/unit/pipelex/cogt/content_generation/test_generated_content_factory.pyindex 598ce6690..18cd05592 100644--- a/tests/unit/pipelex/cogt/content_generation/test_generated_content_factory.py+++ b/tests/unit/pipelex/cogt/content_generation/test_generated_content_factory.py@@ -63,6 +63,46 @@ class TestGeneratedContentFactoryStoredObject:assert image_content.url == "pipelex-storage://stored-uri"+class TestGeneratedContentFactoryStorageKey:+ """The key builder on its own, whatever its caller passes.++ `make_image_content` only ever hands it an image type today, because+ `GeneratedImageRawDetails` refuses a declared type outside the formats the runtime+ produces and a fetched type that is not `image/*` is dropped. The builder's promise+ does not lean on that: the extension follows the whole media type for any caller,+ and a type with no extension of its own gets `bin` rather than another format's.+ """++ @pytest.mark.parametrize(+ ("mime_type", "expected_extension"),+ [+ pytest.param("image/png", "png", id="produced-format"),+ pytest.param("image/png; charset=binary", "png", id="parameters-do-not-change-the-extension"),+ pytest.param("IMAGE/PNG", "png", id="case-does-not-change-the-extension"),+ pytest.param("application/pdf", "pdf", id="non-image-type-takes-its-own-extension"),+ pytest.param("text/plain", "txt", id="text-type-takes-its-own-extension"),+ pytest.param("application/octet-stream", "bin", id="opaque-bytes-are-bin"),+ pytest.param("application/x-unheard-of", "bin", id="unknown-non-image-type-claims-no-format"),+ ],+ )+ def test_the_extension_follows_the_whole_media_type(+ self,+ mocker: MockerFixture,+ mime_type: str,+ expected_extension: str,+ ):+ factory = GeneratedContentFactory(storage_provider=mocker.MagicMock(spec=StorageProviderAbstract))++ storage_key = factory._build_storage_key( # ruff: ignore[private-member-access] # pyright: ignore[reportPrivateUsage]+ storage_scope="test/scope",+ data=FAKE_IMAGE_BYTES,+ mime_type=mime_type,+ )++ assert storage_key.startswith("test/scope/generated/")+ assert storage_key.endswith(f".{expected_extension}")++@pytest.mark.asyncio(loop_scope="class")class TestGeneratedContentFactoryFetchedRemoteImage:"""The fetched-remote path, where nothing has declared a media type yet.@@ -84,6 +124,10 @@ class TestGeneratedContentFactoryFetchedRemoteImage:pytest.param(None, None, "image/png", "png", "image/png", id="served-type-settles-an-undeclared-image"),pytest.param(None, "jpeg", "image/png", "png", "image/png", id="served-type-beats-requested-format"),pytest.param(None, None, "image/gif", "gif", "image/gif", id="served-type-we-do-not-generate-is-still-stored"),+ pytest.param(None, None, "image/svg+xml", "svg", "image/svg+xml", id="non-token-subtype-takes-its-registered-extension"),+ pytest.param(None, None, "image/jxl", "jxl", "image/jxl", id="unregistered-token-subtype-names-itself"),+ pytest.param(None, None, "image/x-icon", "bin", "image/x-icon", id="unregistered-non-token-subtype-claims-no-format"),+ pytest.param(None, None, "image/jpg", "jpg", "image/jpg", id="nonstandard-jpg-alias-keeps-the-jpg-extension"),pytest.param(None, None, "application/octet-stream", "jpg", "image/jpeg", id="non-image-served-type-is-dropped"),pytest.param(None, None, "text/html", "jpg", "image/jpeg", id="error-page-dressed-as-a-200-is-dropped"),pytest.param(None, None, None, "jpg", "image/jpeg", id="silent-server-falls-back-to-default"),