Microsoft Kiota Abstractions (Python)
The `microsoft-kiota-abstractions` library provides core interfaces and base classes for Python SDKs generated by Microsoft Kiota. These abstractions define how HTTP requests are built, how data is serialized and deserialized, and the fundamental structure for models used in API clients. It is currently at version 1.10.1 and follows a frequent release cadence, often synchronized with other Kiota Python packages.
Warnings
- breaking Starting with version 1.10.0, support for Python 3.9 has been officially dropped. The library now requires Python 3.10 or higher.
- gotcha `microsoft-kiota-abstractions` alone does not provide a functional API client. It defines interfaces and base classes. To make actual API calls, you need additional Kiota packages like `microsoft-kiota-http` (for HTTP client implementations), `microsoft-kiota-serialization-json` (for JSON serialization), and an SDK generated by the Kiota tool.
- gotcha Kiota is primarily a code generation tool. The `abstractions` library is consumed by the *generated* SDKs. Direct, manual implementation of interfaces like `Parsable` is usually only necessary when extending or customizing specific Kiota functionalities (e.g., custom serialization) rather than for general API client usage.
- gotcha When implementing `Parsable`, pay close attention to the exact field names (case sensitivity) and types expected by the deserializers. Mismatches in `get_field_deserializers` can lead to runtime errors or incorrect data parsing.
Install
-
pip install microsoft-kiota-abstractions
Imports
- RequestInformation
from kiota_abstractions.request_information import RequestInformation
- RequestAdapter
from kiota_abstractions.request_adapter import RequestAdapter
- Parsable
from kiota_abstractions.serialization import Parsable
- ParseNode
from kiota_abstractions.serialization import ParseNode
- SerializationWriter
from kiota_abstractions.serialization import SerializationWriter
- HttpMethod
from kiota_abstractions.http.method import HttpMethod
- BaseRequestBuilder
from kiota_abstractions.base_request_builder import BaseRequestBuilder
Quickstart
import datetime
from typing import Dict, Optional
from kiota_abstractions.serialization import Parsable, ParseNode, SerializationWriter
class MySimpleModel(Parsable):
"""Represents a simple data model implementing Parsable."""
def __init__(self) -> None:
self._id: Optional[str] = None
self._name: Optional[str] = None
self._created_at: Optional[datetime.datetime] = None
@property
def id(self) -> Optional[str]:
return self._id
@id.setter
def id(self, value: Optional[str]) -> None:
self._id = value
@property
def name(self) -> Optional[str]:
return self._name
@name.setter
def name(self, value: Optional[str]) -> None:
self._name = value
@property
def created_at(self) -> Optional[datetime.datetime]:
return self._created_at
@created_at.setter
def created_at(self, value: Optional[datetime.datetime]) -> None:
self._created_at = value
def get_field_deserializers(self) -> Dict[str, callable]:
"""The deserialization information for the current model"""
return {
"id": lambda n: setattr(self, 'id', n.get_str_value()),
"name": lambda n: setattr(self, 'name', n.get_str_value()),
"createdAt": lambda n: setattr(self, 'created_at', n.get_datetime_value())
}
def serialize(self, writer: SerializationWriter) -> None:
"""Serializes information the current object"""
if not writer:
raise TypeError("writer cannot be None")
writer.write_str_value("id", self.id)
writer.write_str_value("name", self.name)
writer.write_datetime_value("createdAt", self.created_at)
# Example usage (typically handled by Kiota-generated code)
model = MySimpleModel()
model.id = "123"
model.name = "Test Model"
model.created_at = datetime.datetime.now(datetime.timezone.utc)
print(f"Model ID: {model.id}, Name: {model.name}, Created At: {model.created_at}")