UNSTRUCTURED_API_KEY
,
representing your Unstructured API key. To get your API key, do the following:
-
If you do not already have an Unstructured account, sign up for free.
After you sign up, you are automatically signed in to your new Unstructured Starter account, at https://platform.unstructured.io.
To sign up for a Team or Enterprise account instead, contact Unstructured Sales, or learn more.
-
If you have an Unstructured Starter or Team account and are not already signed in, sign in to your account at https://platform.unstructured.io.
For an Enterprise account, see your Unstructured account administrator for instructions, or email Unstructured Support at support@unstructured.io.
-
Get your Unstructured API key:
a. After you sign in to your Unstructured Starter account, click API Keys on the sidebar.
b. Click Generate API Key.For a Team or Enterprise account, before you click API Keys, make sure you have selected the organizational workspace you want to create an API key for. Each API key works with one and only one organizational workspace. Learn more.
c. Follow the on-screen instructions to finish generating the key.
d. Click the Copy icon next to your new key to add the key to your system’s clipboard. If you lose this key, simply return and click the Copy icon again.
Installation
Before using the SDK to interact with Unstructured, install the library:Python
The SDK uses semantic versioning and major bumps could bring breaking changes. It is advised to
pin your installed version. See the migration guide, later on this page, for breaking change announcements.
Basics
Let’s start with a simple example in which you send a PDF document to the Unstructured Partition Endpoint to be partitioned by Unstructured.Python
Async partitioning
The Python SDK also has apartition_async
. This call is equivalent to partition
except that it can be used in a non blocking context. For instance, asyncio.gather
can be used to concurrently process multiple files inside of a directory hierarchy, as demonstrated here:
Page splitting
In order to speed up processing of large PDF files, thesplit_pdf_page
* parameter is True
by default. This
causes the PDF to be split into small batches of pages before sending requests to the API. The client
awaits all parallel requests and combines the responses into a single response object. This is specific to PDF files and other
filetypes are ignored.
The number of parallel requests is controlled by split_pdf_concurrency_level
*.
The default is 8 and the max is set to 15 to avoid high resource usage and costs.
If at least one request is successful, the responses are combined into a single response object. An
error is returned only if all requests failed or there was an error during splitting.
This feature may lead to unexpected results when chunking because the server does not see the entire
document context at once. If you’d like to chunk across the whole document and still get the speedup from
parallel processing, you can:
- Partition the PDF with
split_pdf_page
set toTrue
, without any chunking parameters. - Store the returned elements in
results.json
. - Partition this JSON file with the desired chunking parameters.
Python
Customizing the client
Retries
You can also change the defaults for retries through theretry_config
*
parameter when initializing the client. If a request to the API fails, the client will retry the
request with an exponential backoff strategy up to a maximum interval of one minute. The
function keeps retrying until the total elapsed time exceeds max_elapsed_time
*,
which defaults to one hour. However, you can override these defaults, for example as follows:
Python
RetryConfig
settings include:
Setting | Description |
---|---|
strategy | The strategy to use for retries. The only supported value is backoff . |
retry_connection_errors | True to retry on connection errors. |
BackoffStrategy.initial_interval | After the first error, wait the specified number of milliseconds before retrying. |
BackoffStrategy.max_interval | The maximum wait time, specified in milliseconds between retries. |
BackoffStrategy.exponent | After each retry, increase the wait time exponentially by the specified factor. |
BackoffStrategy.max_elapsed_time | Stop retrying after the specified number of milliseconds. |
Disabling SSL validation
If you disable SSL validation, requests will accept any TLS certificate presented by the server and ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Only set this toFalse
for testing.
Python
Handling the response
The partition response defaults to a dict format that can be converted to Unstructured elements with theelements_from_dicts
utility function as seen below. Otherwise, the API response can be sent directly
to your vector store or another destination.
Python
Parameters & examples
The parameter names used in this document are for the Python SDK, which follow snake_case convention. The JavaScript/TypeScript SDK follows camelCase convention. Other than this difference in naming convention, the names used in the SDKs are the same across all methods.- 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.
Migration guide
There are breaking changes beginning with Python SDK version 0.26.0. If you encounter any errors when upgrading, please find the solution below. If you see the error:AttributeError: 'PartitionParameters' object has no attribute 'partition_parameters'
Before 0.26.0, the SDK accepted a PartitionParameters
object as input to the sdk.general.partition
function. Beginning with 0.26.0, this object must be wrapped in a PartitionRequest
object. The old behavior was deprecated in 0.23.0 and removed in 0.26.0.
TypeError: BaseModel.__init__() takes 1 positional argument but 2 were given
Beginning with 0.26.0, the PartitionRequest
constructor no longer allows for positional arguments. You must specify partition_parameters
by name.
TypeError: General.partition() takes 1 positional argument but 2 were given
Beginning with 0.26.0, the partition
function no longer allows for positional arguments. You must specify request
by name.