datalab-python-sdk 0.15.0__tar.gz → 0.16.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/CHANGELOG.md +55 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/PKG-INFO +11 -2
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/README.md +10 -1
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/__init__.py +8 -0
- datalab_python_sdk-0.16.0/datalab_sdk/_status.py +119 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/client.py +709 -163
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/exceptions.py +64 -1
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/models.py +55 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/pyproject.toml +1 -1
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_streaming_retries.py +14 -2
- datalab_python_sdk-0.16.0/tests/test_submit_and_check.py +800 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/uv.lock +1 -1
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/.github/workflows/ci.yml +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/.gitignore +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/.pre-commit-config.yaml +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/.python-version +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/LICENSE +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/08-Lambda-Calculus.pptx +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/adversarial.pdf +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/bid_evaluation.docx +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/book_review.ppt +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/book_store.xls +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/chi_hind.png +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/how_to_read.doc +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/normandy.epub +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/sample-1-sheet.xlsx +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/thinkpython.pdf +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/data/vibe.html +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/_output_layout.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/_output_names.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/auth.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/cli.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/mimetypes.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/datalab_sdk/settings.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/integration/README.md +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/integration/__init__.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/integration/test_live_api.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/integration/test_readme_examples.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/pytest.ini +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/__init__.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/conftest.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_api_consistency.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_api_key_resolution.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_cli_exit_codes.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_cli_options.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_cli_output_layout.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_cli_simple.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_client_methods.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_comparisons.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_device_login.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_errors.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_import_warnings.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_new_endpoints.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_progress.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_results.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_sync_client_errors.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_timeout_recovery.py +0 -0
- {datalab_python_sdk-0.15.0 → datalab_python_sdk-0.16.0}/tests/test_transport_audit.py +0 -0
|
@@ -3,6 +3,61 @@
|
|
|
3
3
|
The newest release is at the top. Each export to this repository uses the
|
|
4
4
|
top section as its commit message.
|
|
5
5
|
|
|
6
|
+
## 0.16.0
|
|
7
|
+
|
|
8
|
+
Added:
|
|
9
|
+
|
|
10
|
+
- Submit a document request without waiting for it, then check on it
|
|
11
|
+
later. `submit_convert()`, `submit_extract()`, `submit_segment()`,
|
|
12
|
+
`submit_agent()`, `submit_track_changes()`, `submit_create_document()`,
|
|
13
|
+
`submit_fill()`, `submit_form_fields()` and `submit_redact()` take the
|
|
14
|
+
same arguments as the blocking methods, check them the same way, and
|
|
15
|
+
return a `SubmittedRequest` with `request_id`, `check_url` and
|
|
16
|
+
`checkpoint_reused`. They do not poll.
|
|
17
|
+
- `check_status()` checks once whether a submitted request has finished
|
|
18
|
+
and returns a `RequestStatus` with `done`, `success`, `error` and
|
|
19
|
+
`progress_percent`; `error` is None for a success. It never returns the
|
|
20
|
+
result: call `get_result()` once `done` is True. It asks the API to leave the result out and reads
|
|
21
|
+
at most about 1 MB of a response. When an API version sends a larger
|
|
22
|
+
finished result anyway, `success` and `error` are None; check
|
|
23
|
+
`result.success` after `get_result()`. Check `success` first, because most
|
|
24
|
+
processing failures finish with `success=False`. It makes one status
|
|
25
|
+
request and does not retry it; an error it raises has `request_id` and
|
|
26
|
+
`check_url`.
|
|
27
|
+
- Use these calls when something else waits between checks, such as a
|
|
28
|
+
scheduler or a job queue. They work like `run_pipeline()` and
|
|
29
|
+
`get_pipeline_execution()` do for pipelines. `submit_agent()` is open
|
|
30
|
+
beta and `submit_redact()` is beta, like `run_agent()` and `redact()`.
|
|
31
|
+
- `DatalabProcessingError` and `DatalabResponseError`, both subclasses of
|
|
32
|
+
`DatalabAPIError`. The first means the API reported that the request
|
|
33
|
+
failed before it completed, for example an expired on-prem request; the
|
|
34
|
+
second means `check_status()` could not read the status response. From
|
|
35
|
+
`check_status()` both carry the response's HTTP `status_code`, so they
|
|
36
|
+
are not mistaken for connection errors.
|
|
37
|
+
- `DatalabError.retryable`: True when making the same call again later may
|
|
38
|
+
succeed and is safe. For `check_status()` and `get_result()`, that is a
|
|
39
|
+
timeout, a dropped connection, HTTP 408 or 429, or a 5xx; the SDK's own
|
|
40
|
+
poll retries use the same rule. For a submission, it is only HTTP 429 or
|
|
41
|
+
503: after any other error the API may have accepted the request, and
|
|
42
|
+
sending it again could charge for it twice. `check_status()` does not
|
|
43
|
+
retry, so check again when its error's `retryable` is True.
|
|
44
|
+
|
|
45
|
+
Changed:
|
|
46
|
+
|
|
47
|
+
- When a poll reports `success=False` before the request is complete, the
|
|
48
|
+
processing methods raise `DatalabProcessingError` instead of a plain
|
|
49
|
+
`DatalabAPIError`. Code that catches `DatalabAPIError` still catches it.
|
|
50
|
+
- A streamed poll (`stream_response_to`) whose response has no `status`
|
|
51
|
+
raises `DatalabResponseError` with the HTTP status, at once. It raised a
|
|
52
|
+
`DatalabAPIError` with no `status_code`, which the SDK retried as a
|
|
53
|
+
connection error.
|
|
54
|
+
- A poll of a request that the team's page quota rejected (HTTP 429)
|
|
55
|
+
raises `DatalabProcessingError` at once. The SDK retried it as a rate
|
|
56
|
+
limit, though the request had already failed.
|
|
57
|
+
- Polls no longer retry a transport error that would happen again, such
|
|
58
|
+
as an invalid URL, a redirect to another scheme, or a certificate
|
|
59
|
+
failure. Dropped connections and cut-off bodies are still retried.
|
|
60
|
+
|
|
6
61
|
## 0.15.0
|
|
7
62
|
|
|
8
63
|
Removed:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: datalab-python-sdk
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.16.0
|
|
4
4
|
Summary: SDK for the Datalab document intelligence API
|
|
5
5
|
Project-URL: Homepage, https://www.datalab.to
|
|
6
6
|
Project-URL: Documentation, https://documentation.datalab.to
|
|
@@ -195,7 +195,16 @@ Published-agent execution is in open beta; the API may change. Processor design
|
|
|
195
195
|
except DatalabPollingTimeoutError as e:
|
|
196
196
|
result = client.get_result(e.check_url, max_polls=600)
|
|
197
197
|
```
|
|
198
|
-
-
|
|
198
|
+
- To wait somewhere else, for example in a scheduler, submit with `submit_convert()`, `submit_extract()`, `submit_segment()` or another `submit_*` method (SDK 0.16.0+). It returns a `SubmittedRequest` with `request_id` and `check_url` and does not poll. `check_status(check_url)` checks once whether the request has finished, without returning the result, and does not retry. When `done` is True, `get_result(check_url)` downloads the result; check `result.success`.
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
submitted = client.submit_convert("big.pdf")
|
|
202
|
+
# ... later, from any process
|
|
203
|
+
status = client.check_status(submitted.check_url)
|
|
204
|
+
if status.done:
|
|
205
|
+
result = client.get_result(submitted.check_url)
|
|
206
|
+
```
|
|
207
|
+
- API errors raise `DatalabAPIError` (with `status_code` and `response_data`) or one of its subclasses: `DatalabAuthenticationError` (401, 403), `DatalabPaymentRequiredError` (402), `DatalabNotFoundError` (404), `DatalabRateLimitError` (429) and `DatalabServerError` (5xx). `DatalabProcessingError` means the API reported that the request failed before it completed, and `DatalabResponseError` means `check_status()` could not read the response. Every `DatalabError` has `retryable`, which says whether making the same call again later may succeed and is safe: a submission is retryable only after HTTP 429 or 503, because after other errors the API may already have accepted it. Invalid arguments raise `DatalabValidationError`, which is also a `ValueError`.
|
|
199
208
|
|
|
200
209
|
With SDK 0.9.0, `upload_files(paths, max_concurrency=5)` streams
|
|
201
210
|
files from disk and limits the number of active uploads. Results retain the
|
|
@@ -174,7 +174,16 @@ Published-agent execution is in open beta; the API may change. Processor design
|
|
|
174
174
|
except DatalabPollingTimeoutError as e:
|
|
175
175
|
result = client.get_result(e.check_url, max_polls=600)
|
|
176
176
|
```
|
|
177
|
-
-
|
|
177
|
+
- To wait somewhere else, for example in a scheduler, submit with `submit_convert()`, `submit_extract()`, `submit_segment()` or another `submit_*` method (SDK 0.16.0+). It returns a `SubmittedRequest` with `request_id` and `check_url` and does not poll. `check_status(check_url)` checks once whether the request has finished, without returning the result, and does not retry. When `done` is True, `get_result(check_url)` downloads the result; check `result.success`.
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
submitted = client.submit_convert("big.pdf")
|
|
181
|
+
# ... later, from any process
|
|
182
|
+
status = client.check_status(submitted.check_url)
|
|
183
|
+
if status.done:
|
|
184
|
+
result = client.get_result(submitted.check_url)
|
|
185
|
+
```
|
|
186
|
+
- API errors raise `DatalabAPIError` (with `status_code` and `response_data`) or one of its subclasses: `DatalabAuthenticationError` (401, 403), `DatalabPaymentRequiredError` (402), `DatalabNotFoundError` (404), `DatalabRateLimitError` (429) and `DatalabServerError` (5xx). `DatalabProcessingError` means the API reported that the request failed before it completed, and `DatalabResponseError` means `check_status()` could not read the response. Every `DatalabError` has `retryable`, which says whether making the same call again later may succeed and is safe: a submission is retryable only after HTTP 429 or 503, because after other errors the API may already have accepted it. Invalid arguments raise `DatalabValidationError`, which is also a `ValueError`.
|
|
178
187
|
|
|
179
188
|
With SDK 0.9.0, `upload_files(paths, max_concurrency=5)` streams
|
|
180
189
|
files from disk and limits the number of active uploads. Results retain the
|
|
@@ -14,6 +14,8 @@ from .exceptions import (
|
|
|
14
14
|
DatalabNotFoundError,
|
|
15
15
|
DatalabRateLimitError,
|
|
16
16
|
DatalabServerError,
|
|
17
|
+
DatalabProcessingError,
|
|
18
|
+
DatalabResponseError,
|
|
17
19
|
DatalabTimeoutError,
|
|
18
20
|
DatalabTransferTimeoutError,
|
|
19
21
|
DatalabPollingTimeoutError,
|
|
@@ -25,6 +27,8 @@ from .models import (
|
|
|
25
27
|
CreateDocumentResult,
|
|
26
28
|
FileResult,
|
|
27
29
|
OCRResult,
|
|
30
|
+
RequestStatus,
|
|
31
|
+
SubmittedRequest,
|
|
28
32
|
ConvertOptions,
|
|
29
33
|
ExtractOptions,
|
|
30
34
|
SegmentOptions,
|
|
@@ -83,6 +87,8 @@ __all__ = [
|
|
|
83
87
|
"DatalabNotFoundError",
|
|
84
88
|
"DatalabRateLimitError",
|
|
85
89
|
"DatalabServerError",
|
|
90
|
+
"DatalabProcessingError",
|
|
91
|
+
"DatalabResponseError",
|
|
86
92
|
"DatalabTimeoutError",
|
|
87
93
|
"DatalabTransferTimeoutError",
|
|
88
94
|
"DatalabPollingTimeoutError",
|
|
@@ -92,6 +98,8 @@ __all__ = [
|
|
|
92
98
|
"CreateDocumentResult",
|
|
93
99
|
"FileResult",
|
|
94
100
|
"OCRResult",
|
|
101
|
+
"RequestStatus",
|
|
102
|
+
"SubmittedRequest",
|
|
95
103
|
"ConvertOptions",
|
|
96
104
|
"ExtractOptions",
|
|
97
105
|
"SegmentOptions",
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""The bounded reader behind check_status().
|
|
2
|
+
|
|
3
|
+
check_status() needs four top-level fields of a status response. An API
|
|
4
|
+
that does not support include_result=false sends the whole result, which can
|
|
5
|
+
be gigabytes, before success and error. This reader never reads more than
|
|
6
|
+
READ_LIMIT bytes of a body, and the caller turns off automatic decompression,
|
|
7
|
+
so the limit counts the bytes received.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import Optional
|
|
12
|
+
|
|
13
|
+
import aiohttp
|
|
14
|
+
import ijson
|
|
15
|
+
|
|
16
|
+
READ_LIMIT = 1024 * 1024
|
|
17
|
+
_CHUNK = 64 * 1024
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class StatusBodyError(Exception):
|
|
21
|
+
"""The status body was compressed, invalid JSON, had no status, or was
|
|
22
|
+
too large without a finished status."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ProcessingFailed(Exception):
|
|
26
|
+
"""The API reported success=False for a request that is not complete."""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class StatusFields:
|
|
31
|
+
"""A status snapshot. read_status() returns it normalized: success and
|
|
32
|
+
error are None while the request runs, and None for a finished request
|
|
33
|
+
whose body reached READ_LIMIT before they arrived (outcome unknown)."""
|
|
34
|
+
|
|
35
|
+
status: Optional[str] = None
|
|
36
|
+
success: Optional[bool] = None
|
|
37
|
+
error: Optional[str] = None
|
|
38
|
+
progress_percent: Optional[int] = None
|
|
39
|
+
_error_seen: bool = False
|
|
40
|
+
|
|
41
|
+
def _feed(self, events) -> None:
|
|
42
|
+
for prefix, event, value in events:
|
|
43
|
+
if prefix == "status" and event == "string":
|
|
44
|
+
self.status = value
|
|
45
|
+
elif prefix == "success" and event == "boolean":
|
|
46
|
+
self.success = value
|
|
47
|
+
elif prefix == "error" and event in ("string", "null"):
|
|
48
|
+
self.error = value
|
|
49
|
+
self._error_seen = True
|
|
50
|
+
elif prefix == "progress_percent" and event == "number":
|
|
51
|
+
self.progress_percent = value if type(value) is int else None
|
|
52
|
+
|
|
53
|
+
def _normalized(self, outcome_known: bool) -> "StatusFields":
|
|
54
|
+
if self.status is None:
|
|
55
|
+
raise StatusBodyError("the response has no status")
|
|
56
|
+
if self.status != "complete" and self.success is False:
|
|
57
|
+
raise ProcessingFailed(self.error or "Unknown error")
|
|
58
|
+
if self.status != "complete" or not outcome_known:
|
|
59
|
+
self.success = self.error = None
|
|
60
|
+
elif self.success is None:
|
|
61
|
+
self.success = True
|
|
62
|
+
# Some routes send error="" for a success: report no error as None.
|
|
63
|
+
self.error = self.error or None
|
|
64
|
+
return self
|
|
65
|
+
|
|
66
|
+
def _finished(self) -> bool:
|
|
67
|
+
return (
|
|
68
|
+
self.status == "complete"
|
|
69
|
+
and self.success is not None
|
|
70
|
+
and (self.success or self._error_seen)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
async def read_error_body(resp: aiohttp.ClientResponse) -> bytes:
|
|
75
|
+
"""At most READ_LIMIT bytes of an error body. A cut-off body still lets
|
|
76
|
+
the caller raise the error for the status code."""
|
|
77
|
+
try:
|
|
78
|
+
return await resp.content.read(READ_LIMIT)
|
|
79
|
+
except aiohttp.ClientError:
|
|
80
|
+
return b""
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
async def read_status(resp: aiohttp.ClientResponse) -> StatusFields:
|
|
84
|
+
"""Read and normalize the status fields of a 2xx status response,
|
|
85
|
+
reading at most READ_LIMIT bytes."""
|
|
86
|
+
encoding = resp.headers.get("Content-Encoding", "identity").lower()
|
|
87
|
+
if encoding != "identity":
|
|
88
|
+
raise StatusBodyError(f"status response is {encoding}-encoded")
|
|
89
|
+
|
|
90
|
+
fields = StatusFields()
|
|
91
|
+
events = ijson.sendable_list()
|
|
92
|
+
parser = ijson.parse_coro(events)
|
|
93
|
+
remaining = READ_LIMIT
|
|
94
|
+
try:
|
|
95
|
+
while remaining > 0:
|
|
96
|
+
chunk = await resp.content.read(min(_CHUNK, remaining))
|
|
97
|
+
if not chunk:
|
|
98
|
+
parser.close()
|
|
99
|
+
fields._feed(events)
|
|
100
|
+
return fields._normalized(outcome_known=True)
|
|
101
|
+
remaining -= len(chunk)
|
|
102
|
+
parser.send(chunk)
|
|
103
|
+
fields._feed(events)
|
|
104
|
+
del events[:]
|
|
105
|
+
if fields._finished():
|
|
106
|
+
return fields._normalized(outcome_known=True)
|
|
107
|
+
# The budget is spent. A body of exactly READ_LIMIT bytes has ended.
|
|
108
|
+
if await resp.content.read(1) == b"":
|
|
109
|
+
parser.close()
|
|
110
|
+
fields._feed(events)
|
|
111
|
+
return fields._normalized(outcome_known=True)
|
|
112
|
+
except ijson.JSONError as e:
|
|
113
|
+
# ijson's message adds lines that point at the bad character.
|
|
114
|
+
reason = str(e).splitlines()[0] if str(e) else type(e).__name__
|
|
115
|
+
raise StatusBodyError(f"invalid JSON: {reason}") from e
|
|
116
|
+
|
|
117
|
+
if fields.status != "complete":
|
|
118
|
+
raise StatusBodyError(f"no finished status in the first {READ_LIMIT} bytes")
|
|
119
|
+
return fields._normalized(outcome_known=False)
|