Microsoft Kiota Multipart Serialization
This library provides core abstractions and implementation for serializing and deserializing multipart content within Microsoft Kiota generated Python clients. It's part of the broader Kiota ecosystem for building API clients and is currently at version 1.10.1, with a regular release cadence synchronized with other Kiota Python packages. It supports Python versions >=3.10 and <4.0.
Common errors
-
ModuleNotFoundError: No module named 'microsoft_kiota_serialization_multipart'
cause The `microsoft-kiota-serialization-multipart` package has not been installed in the Python environment, or the environment where it's installed is not active.fixInstall the package using pip: `pip install microsoft-kiota-serialization-multipart` -
Error: unsupported content type for multipart body: object
cause This error occurs when attempting to add an object or metadata reference to a multipart body part instead of the actual content (e.g., bytes, stream, or correctly serialized string) of the file or data. The serialization library expects the content itself, not a reference to it.fixEnsure that the actual content of the file or data (e.g., `file.read()`, `file.bytes()`, or a correctly encoded string) is passed when creating or adding a part to the `MultipartBody`, rather than the file object itself or an object placeholder. -
ModuleNotFoundError: No module named 'microsoft.kiota.serialization.multipart'
cause Developers often mistakenly assume a hierarchical package structure (e.g., `microsoft.kiota`) when the top-level package name in Python is actually `microsoft_kiota_serialization_multipart`. This is a common wrong import pattern.fixCorrect the import statement to use the exact installed package name, typically: `from microsoft_kiota_serialization_multipart.<submodule> import <Class>`
Warnings
- breaking Version 1.10.0 and later dropped official support for Python 3.9. Users attempting to install or run with Python 3.9 will encounter `Requires-Python` metadata errors or unexpected runtime behavior.
- gotcha This package relies heavily on core interfaces defined in `microsoft-kiota-abstractions`. If you manually manage your dependencies, ensure `microsoft-kiota-abstractions` is explicitly installed alongside `microsoft-kiota-serialization-multipart` to avoid `ModuleNotFoundError` or `TypeError`.
- gotcha While `MultipartSerializationWriter` supports `write_object_value`, multipart content is fundamentally different from single-body formats like JSON or XML. It's designed for sending data as distinct parts (e.g., form fields, files). Direct serialization of a complex `Parsable` object might not produce the desired single-body multipart output as expected in other formats.
Install
-
pip install microsoft-kiota-serialization-multipart
Imports
- MultipartSerializationWriterFactory
from microsoft_kiota_serialization_multipart import MultipartSerializationWriterFactory
- MultipartParseNodeFactory
from microsoft_kiota_serialization_multipart import MultipartParseNodeFactory
Quickstart
from microsoft_kiota_abstractions.serialization import (
register_default_deserializer,
register_default_serializer,
SerializationWriterFactoryRegistry,
ParseNodeFactoryRegistry
)
from microsoft_kiota_serialization_multipart import (
MultipartSerializationWriterFactory,
MultipartParseNodeFactory
)
# Initialize and register the multipart serialization and deserialization factories.
# This makes them available to Kiota's internal machinery when
# 'multipart/form-data' content type is encountered.
mulipart_serializer_factory = MultipartSerializationWriterFactory()
mulipart_parser_factory = MultipartParseNodeFactory()
register_default_serializer(mulipart_serializer_factory)
register_default_deserializer(mulipart_parser_factory)
print("Microsoft Kiota Multipart Serialization and Deserialization factories registered.")
# You can now retrieve them from the global registry (e.g., for custom HTTP client setups)
# The Kiota generated clients typically handle this registration automatically.
writer_factory = SerializationWriterFactoryRegistry.get_default_instance().get_serialization_writer_factory("multipart/form-data")
parser_factory = ParseNodeFactoryRegistry.get_default_instance().get_parse_node_factory("multipart/form-data")
if writer_factory and parser_factory:
print("\nSuccessfully retrieved Multipart factories from the global registry:")
print(f"- Serialization Writer Factory: {writer_factory}")
print(f"- Parse Node Factory: {parser_factory}")
print("\nThese factories enable Kiota clients to handle 'multipart/form-data' requests and responses.")
else:
print("\nError: Could not retrieve Multipart factories from the global registry.")
# To perform actual serialization/deserialization, you would typically use a
# Kiota-generated client or directly interact with the writer/parser with file streams
# or form data.