docql 0.2.1__tar.gz → 0.3.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: docql
3
- Version: 0.2.1
3
+ Version: 0.3.0
4
4
  Summary: Python client for the DocQL document-extraction API
5
5
  Author: Finoniq
6
6
  License-Expression: Apache-2.0
@@ -133,6 +133,38 @@ binary file object:
133
133
  - There are no client-side type or size checks — the server is the authority.
134
134
  - A stream whose length cannot be measured is sent chunked; an oversized one
135
135
  may answer `503` instead of `413`. The API rejects files over 25 MiB.
136
+ - A CSV is recognised only by a file name that ends in `.csv`, in any letter
137
+ case (`sales.csv`, `SALES.CSV`). When you send `bytes` or a stream, pass the
138
+ name with `filename=`. The name is sent exactly as given and never trimmed,
139
+ so `"a.csv "` with a trailing space is refused with `unsupported_file_type`.
140
+
141
+ ## Spreadsheets
142
+
143
+ `.xlsx`, `.ods` and `.csv` files are sent like any other file. A refused
144
+ spreadsheet raises `docql.DocQLError`, whose `metadata` is the server's raw
145
+ refusal object. For `spreadsheet_too_large` it holds the measured value under
146
+ its axis key (`cells`, `runes`, `estimated_output_tokens` or `bytes`), plus
147
+ `limit`, `reason` and `request_id`. Find the axis by name, not by key order:
148
+
149
+ <!-- docql:spreadsheet:start -->
150
+ ```python
151
+ from pathlib import Path
152
+
153
+ import docql
154
+ from docql import DocQL
155
+
156
+ with DocQL() as client: # reads DOCQL_API_KEY and DOCQL_API_URL
157
+ data = Path("sales.csv").read_bytes()
158
+ try:
159
+ result = client.query_document(data, query="{ region total_sales }", filename="sales.csv")
160
+ print(result.page_count)
161
+ except docql.DocQLError as err:
162
+ if err.reason != docql.REASON_SPREADSHEET_TOO_LARGE:
163
+ raise
164
+ axis = next(k for k in err.metadata if k not in ("limit", "reason", "request_id"))
165
+ print(f"too large: {axis}={err.metadata[axis]} limit={err.metadata['limit']}")
166
+ ```
167
+ <!-- docql:spreadsheet:end -->
136
168
 
137
169
  ## Modes
138
170
 
@@ -208,7 +240,7 @@ two mean the request was not processed.
208
240
  ## Versioning
209
241
 
210
242
  The SDK follows [Semantic Versioning](https://semver.org/). While the major
211
- version is 0, a minor release may change the API; pin `docql>=0.2,<0.3` until
243
+ version is 0, a minor release may change the API; pin `docql>=0.3,<0.4` until
212
244
  1.0.
213
245
 
214
246
  ## Support
@@ -111,6 +111,38 @@ binary file object:
111
111
  - There are no client-side type or size checks — the server is the authority.
112
112
  - A stream whose length cannot be measured is sent chunked; an oversized one
113
113
  may answer `503` instead of `413`. The API rejects files over 25 MiB.
114
+ - A CSV is recognised only by a file name that ends in `.csv`, in any letter
115
+ case (`sales.csv`, `SALES.CSV`). When you send `bytes` or a stream, pass the
116
+ name with `filename=`. The name is sent exactly as given and never trimmed,
117
+ so `"a.csv "` with a trailing space is refused with `unsupported_file_type`.
118
+
119
+ ## Spreadsheets
120
+
121
+ `.xlsx`, `.ods` and `.csv` files are sent like any other file. A refused
122
+ spreadsheet raises `docql.DocQLError`, whose `metadata` is the server's raw
123
+ refusal object. For `spreadsheet_too_large` it holds the measured value under
124
+ its axis key (`cells`, `runes`, `estimated_output_tokens` or `bytes`), plus
125
+ `limit`, `reason` and `request_id`. Find the axis by name, not by key order:
126
+
127
+ <!-- docql:spreadsheet:start -->
128
+ ```python
129
+ from pathlib import Path
130
+
131
+ import docql
132
+ from docql import DocQL
133
+
134
+ with DocQL() as client: # reads DOCQL_API_KEY and DOCQL_API_URL
135
+ data = Path("sales.csv").read_bytes()
136
+ try:
137
+ result = client.query_document(data, query="{ region total_sales }", filename="sales.csv")
138
+ print(result.page_count)
139
+ except docql.DocQLError as err:
140
+ if err.reason != docql.REASON_SPREADSHEET_TOO_LARGE:
141
+ raise
142
+ axis = next(k for k in err.metadata if k not in ("limit", "reason", "request_id"))
143
+ print(f"too large: {axis}={err.metadata[axis]} limit={err.metadata['limit']}")
144
+ ```
145
+ <!-- docql:spreadsheet:end -->
114
146
 
115
147
  ## Modes
116
148
 
@@ -186,7 +218,7 @@ two mean the request was not processed.
186
218
  ## Versioning
187
219
 
188
220
  The SDK follows [Semantic Versioning](https://semver.org/). While the major
189
- version is 0, a minor release may change the API; pin `docql>=0.2,<0.3` until
221
+ version is 0, a minor release may change the API; pin `docql>=0.3,<0.4` until
190
222
  1.0.
191
223
 
192
224
  ## Support
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "docql"
3
- version = "0.2.1"
3
+ version = "0.3.0"
4
4
  description = "Python client for the DocQL document-extraction API"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "docql"
3
- version = "0.2.1"
3
+ version = "0.3.0"
4
4
  description = "Python client for the DocQL document-extraction API"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -13,6 +13,7 @@ from docql._constants import (
13
13
  REASON_CONCURRENCY_LIMIT_EXCEEDED,
14
14
  REASON_CORRUPTED_FILE,
15
15
  REASON_DOCUMENT_EXCEEDS_TIME_BUDGET,
16
+ REASON_DOCUMENT_TOO_COMPLEX,
16
17
  REASON_IDEMPOTENCY_IN_PROGRESS,
17
18
  REASON_IDEMPOTENCY_KEY_CONFLICT,
18
19
  REASON_INVALID_API_KEY,
@@ -26,6 +27,8 @@ from docql._constants import (
26
27
  REASON_QUEUE_FULL,
27
28
  REASON_RATE_LIMIT_EXCEEDED,
28
29
  REASON_SERVICE_OVERLOADED,
30
+ REASON_SPREADSHEET_TOO_LARGE,
31
+ REASON_TOO_MANY_PAGES,
29
32
  REASON_UNSUPPORTED_FILE_TYPE,
30
33
  REASON_UNSUPPORTED_MEDIA_TYPE,
31
34
  REASON_UPLOAD_TOO_LARGE,
@@ -65,6 +68,7 @@ __all__ = [
65
68
  "REASON_CONCURRENCY_LIMIT_EXCEEDED",
66
69
  "REASON_CORRUPTED_FILE",
67
70
  "REASON_DOCUMENT_EXCEEDS_TIME_BUDGET",
71
+ "REASON_DOCUMENT_TOO_COMPLEX",
68
72
  "REASON_IDEMPOTENCY_IN_PROGRESS",
69
73
  "REASON_IDEMPOTENCY_KEY_CONFLICT",
70
74
  "REASON_INVALID_API_KEY",
@@ -78,6 +82,8 @@ __all__ = [
78
82
  "REASON_QUEUE_FULL",
79
83
  "REASON_RATE_LIMIT_EXCEEDED",
80
84
  "REASON_SERVICE_OVERLOADED",
85
+ "REASON_SPREADSHEET_TOO_LARGE",
86
+ "REASON_TOO_MANY_PAGES",
81
87
  "REASON_UNSUPPORTED_FILE_TYPE",
82
88
  "REASON_UNSUPPORTED_MEDIA_TYPE",
83
89
  "REASON_UPLOAD_TOO_LARGE",
@@ -35,9 +35,10 @@ ERROR_BODY_READ_LIMIT: Final[int] = 65536
35
35
  # because an early RST may have discarded the 413 (research Pitfall 4).
36
36
  MAX_UPLOAD_BYTES: Final[int] = 26_214_400
37
37
 
38
- # Reason constants (D-05, D-09): exactly the 22 non-null reasons of the vendored
39
- # contract kit — contract/sdk-cases.json (the sync stub and gateway errors) plus
40
- # contract/job-cases.json (the job errors). The engine's wider class table stays
38
+ # Reason constants (D-05, D-09, D-12): exactly the 25 non-null reasons of the
39
+ # vendored contract kit — contract/sdk-cases.json (the sync stub and gateway
40
+ # errors), contract/job-cases.json (the job errors) and contract/sheet-cases.json
41
+ # (the kit v5 spreadsheet refusals). The engine's wider class table stays
41
42
  # reachable through the open ``reason: str``; adding constants later is
42
43
  # backward compatible, removing them is not.
43
44
  REASON_INVALID_API_KEY: Final[str] = "invalid_api_key"
@@ -62,6 +63,9 @@ REASON_QUEUE_FULL: Final[str] = "queue_full"
62
63
  REASON_IDEMPOTENCY_KEY_CONFLICT: Final[str] = "idempotency_key_conflict"
63
64
  REASON_IDEMPOTENCY_IN_PROGRESS: Final[str] = "idempotency_in_progress"
64
65
  REASON_JOB_INTERRUPTED: Final[str] = "job_interrupted"
66
+ REASON_DOCUMENT_TOO_COMPLEX: Final[str] = "document_too_complex"
67
+ REASON_SPREADSHEET_TOO_LARGE: Final[str] = "spreadsheet_too_large"
68
+ REASON_TOO_MANY_PAGES: Final[str] = "too_many_pages"
65
69
 
66
70
  # Async jobs (JSDK-01). Status values are open strings (D-09): an unknown status
67
71
  # from a future DocQL release is kept verbatim on Job.status, never an error.
@@ -35,6 +35,15 @@ class DocQLError(DocQLException):
35
35
  cancel error). ``idempotency_key`` is set on errors from
36
36
  ``submit_document`` so the caller can retry with the same key; it is a
37
37
  field only and never appears in :func:`str` or :func:`repr` (D-08).
38
+
39
+ ``metadata`` is the server's raw refusal metadata object, as a dict (D-06).
40
+ For ``spreadsheet_too_large`` it holds the measured value under its axis
41
+ key (``cells``, ``runes``, ``estimated_output_tokens`` or ``bytes``),
42
+ ``limit``, ``reason`` and ``request_id``; for ``too_many_pages`` it holds
43
+ ``pages`` and ``limit``. Every occurrence of the API key in its keys and
44
+ string values is replaced with ``***``. It is ``{}`` when the answer
45
+ carried no metadata object, and it never appears in :func:`str` or
46
+ :func:`repr`.
38
47
  """
39
48
 
40
49
  def __init__(
@@ -49,6 +58,7 @@ class DocQLError(DocQLException):
49
58
  body: str,
50
59
  job_id: str | None = None,
51
60
  idempotency_key: str | None = None,
61
+ metadata: dict[str, Any] | None = None,
52
62
  ) -> None:
53
63
  super().__init__()
54
64
  self.status = status
@@ -60,6 +70,7 @@ class DocQLError(DocQLException):
60
70
  self.body = body
61
71
  self.job_id = job_id
62
72
  self.idempotency_key = idempotency_key
73
+ self.metadata: dict[str, Any] = {} if metadata is None else metadata
63
74
 
64
75
  def __str__(self) -> str:
65
76
  if self.reason is not None:
@@ -106,6 +117,7 @@ class DocQLError(DocQLException):
106
117
  "body": self.body,
107
118
  "job_id": self.job_id,
108
119
  "idempotency_key": self.idempotency_key,
120
+ "metadata": self.metadata,
109
121
  },
110
122
  )
111
123
 
@@ -50,6 +50,22 @@ def _scrub_and_truncate(text: str, api_key: str) -> str:
50
50
  return text[:ERROR_BODY_KEEP_CHARS]
51
51
 
52
52
 
53
+ def _scrub_deep(value: Any, api_key: str) -> Any:
54
+ """Replace the key in every string inside ``value``, dict keys included (D-06).
55
+
56
+ Used for the error metadata, which a developer may log or store: a
57
+ reflecting server must never get the key into it. Numbers, booleans and
58
+ None pass through unchanged.
59
+ """
60
+ if isinstance(value, str):
61
+ return value.replace(api_key, "***") if api_key else value
62
+ if isinstance(value, dict):
63
+ return {_scrub_deep(k, api_key): _scrub_deep(v, api_key) for k, v in value.items()}
64
+ if isinstance(value, list):
65
+ return [_scrub_deep(item, api_key) for item in value]
66
+ return value
67
+
68
+
53
69
  def parse_retry_after(value: str | None, now: datetime | None = None) -> int | None:
54
70
  """``Retry-After`` as whole seconds, or None when absent/unparsable.
55
71
 
@@ -146,18 +162,25 @@ def decode_error(
146
162
 
147
163
  Classification is by status, never by body shape. An envelope is present
148
164
  only when the JSON body is a dict whose ``metadata`` is a dict with a str
149
- ``reason``. Neither the stored ``body`` nor ``message`` ever contains the
150
- API key, even when the server echoes it back.
165
+ ``reason``. Neither the stored ``body``, ``message`` nor ``metadata`` ever
166
+ contains the API key, even when the server echoes it back.
167
+
168
+ ``metadata`` is parsed from the whole bounded body, before the stored body
169
+ is cut, and is filled whenever the body carries a metadata object, for
170
+ every reason (D-06, D-12); otherwise the error keeps the default ``{}``.
151
171
  """
152
172
  reason: str | None = None
153
173
  message: str | None = None
154
174
  request_id: str | None = None
175
+ metadata_out: dict[str, Any] | None = None
155
176
  try:
156
177
  doc: Any = json.loads(body)
157
178
  except ValueError:
158
179
  doc = None
159
180
  if isinstance(doc, dict):
160
181
  metadata = doc.get("metadata")
182
+ if isinstance(metadata, dict):
183
+ metadata_out = _scrub_deep(metadata, api_key)
161
184
  if isinstance(metadata, dict) and isinstance(metadata.get("reason"), str):
162
185
  reason = metadata["reason"]
163
186
  error_info = doc.get("error_info")
@@ -182,4 +205,5 @@ def decode_error(
182
205
  retryable=status in (429, 503),
183
206
  message=message,
184
207
  body=_scrub_and_truncate(body.decode("utf-8", "replace"), api_key),
208
+ metadata=metadata_out,
185
209
  )
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes