The Python SDK allows for easy interaction with the Unstructured API using Python. Whether you’re using the free Unstructured API, the SaaS Unstructured API, Unstructured API on Azure/AWS or your local deployment of Unstructured API, you can access the API using the Python SDK.

Installation

Before using the Python SDK to interact with your Unstructured API, install the library:
pip install unstructured-client

Basics

Let’s start with a simple example in which you send a pdf document to partition via Unstructured API using the Python SDK:
from unstructured_client import UnstructuredClient
from unstructured_client.models import shared
from unstructured_client.models.errors import SDKError

client = UnstructuredClient(
    api_key_auth="YOUR_API_KEY",
    # you may need to provide your unique API URL
    # server_url="YOUR_API_URL",
)

filename = "sample-docs/layout-parser-paper.pdf"
file = open(filename, "rb")
req = shared.PartitionParameters(
    # Note that this currently only supports a single file
    files=shared.Files(
        content=file.read(),
        file_name=filename,
    ),
    # Other parameters
    strategy="fast",
)

try:
    res = client.general.partition(req)
    print(res.elements[0])
except SDKError as e:
    print(e)
In the example above we’re sending the request to the free Unstructured API, in which case the API URL is the same for all users, and you don’t need to provide it. Note, however, that you still need to authenticate yourself with your individual API Key. If you want to use the SaaS Unstructured API, you need to replace the URL in this example with the unique API URL that you have received in the same email as your API key. For Unstructured API on Azure/AWS, use the API URL that you configured through those services.

Parameters & examples

The API parameters are the same across all methods of accessing the Unstructured API.
  • Refer to the API parameters page for the full list of available parameters.
  • Refer to the Examples page for some inspiration on using the parameters.