Skip to content

Commit b03ae9b

Browse files
feat(api): api update
1 parent 5d5fe8c commit b03ae9b

38 files changed

Lines changed: 2728 additions & 127 deletions

.stats.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
configured_endpoints: 2
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/nanonets%2Fdocstrange-75dd621006675868b0701738226a045f2e9d7211242fece922f06478bef66738.yml
3-
openapi_spec_hash: 47cb78aa2caa018fe58742fc19b0a49c
4-
config_hash: aed0d6cc8b4cffa1f02021baec0a6da3
1+
configured_endpoints: 9
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/nanonets%2Fdocstrange-a418fe45369669cc2d14b549ee010f3a7ee52f6e47fea3489e560d33064be099.yml
3+
openapi_spec_hash: 02f7f52faae1eb42188c290c32c25f10
4+
config_hash: 1804754b97b5ccf95ad31183c74527bc

README.md

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ client = Docstrange(
3333
)
3434

3535
extract_response = client.extract.sync(
36-
file=b"raw file contents",
3736
output_format="markdown",
3837
)
3938
print(extract_response.record_id)
@@ -60,7 +59,6 @@ client = AsyncDocstrange(
6059

6160
async def main() -> None:
6261
extract_response = await client.extract.sync(
63-
file=b"raw file contents",
6462
output_format="markdown",
6563
)
6664
print(extract_response.record_id)
@@ -97,7 +95,6 @@ async def main() -> None:
9795
http_client=DefaultAioHttpClient(),
9896
) as client:
9997
extract_response = await client.extract.sync(
100-
file=b"raw file contents",
10198
output_format="markdown",
10299
)
103100
print(extract_response.record_id)
@@ -115,6 +112,67 @@ Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typ
115112

116113
Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`.
117114

115+
## Pagination
116+
117+
List methods in the Docstrange API are paginated.
118+
119+
This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:
120+
121+
```python
122+
from docstrange import Docstrange
123+
124+
client = Docstrange()
125+
126+
all_results = []
127+
# Automatically fetches more pages as needed.
128+
for result in client.extract.results.list():
129+
# Do something with result here
130+
all_results.append(result)
131+
print(all_results)
132+
```
133+
134+
Or, asynchronously:
135+
136+
```python
137+
import asyncio
138+
from docstrange import AsyncDocstrange
139+
140+
client = AsyncDocstrange()
141+
142+
143+
async def main() -> None:
144+
all_results = []
145+
# Iterate through items across all pages, issuing requests as needed.
146+
async for result in client.extract.results.list():
147+
all_results.append(result)
148+
print(all_results)
149+
150+
151+
asyncio.run(main())
152+
```
153+
154+
Alternatively, you can use the `.has_next_page()`, `.next_page_info()`, or `.get_next_page()` methods for more granular control working with pages:
155+
156+
```python
157+
first_page = await client.extract.results.list()
158+
if first_page.has_next_page():
159+
print(f"will fetch next page using these details: {first_page.next_page_info()}")
160+
next_page = await first_page.get_next_page()
161+
print(f"number of items we just fetched: {len(next_page.results)}")
162+
163+
# Remove `await` for non-async usage.
164+
```
165+
166+
Or just work directly with the returned data:
167+
168+
```python
169+
first_page = await client.extract.results.list()
170+
for result in first_page.results:
171+
print(result.record_id)
172+
173+
# Remove `await` for non-async usage.
174+
```
175+
118176
## File uploads
119177

120178
Request parameters that correspond to file uploads can be passed as `bytes`, or a [`PathLike`](https://docs.python.org/3/library/os.html#os.PathLike) instance or a tuple of `(filename, contents, media type)`.
@@ -126,8 +184,8 @@ from docstrange import Docstrange
126184
client = Docstrange()
127185

128186
client.extract.sync(
129-
file=Path("/path/to/file"),
130187
output_format="markdown",
188+
file=Path("/path/to/file"),
131189
)
132190
```
133191

@@ -150,7 +208,6 @@ client = Docstrange()
150208

151209
try:
152210
client.extract.sync(
153-
file=b"raw file contents",
154211
output_format="markdown",
155212
)
156213
except docstrange.APIConnectionError as e:
@@ -196,7 +253,6 @@ client = Docstrange(
196253

197254
# Or, configure per-request:
198255
client.with_options(max_retries=5).extract.sync(
199-
file=b"raw file contents",
200256
output_format="markdown",
201257
)
202258
```
@@ -222,7 +278,6 @@ client = Docstrange(
222278

223279
# Override per-request:
224280
client.with_options(timeout=5.0).extract.sync(
225-
file=b"raw file contents",
226281
output_format="markdown",
227282
)
228283
```
@@ -266,7 +321,6 @@ from docstrange import Docstrange
266321

267322
client = Docstrange()
268323
response = client.extract.with_raw_response.sync(
269-
file=b"raw file contents",
270324
output_format="markdown",
271325
)
272326
print(response.headers.get('X-My-Header'))
@@ -287,7 +341,6 @@ To stream the response body, use `.with_streaming_response` instead, which requi
287341

288342
```python
289343
with client.extract.with_streaming_response.sync(
290-
file=b"raw file contents",
291344
output_format="markdown",
292345
) as response:
293346
print(response.headers.get("X-My-Header"))

api.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,62 @@
33
Types:
44

55
```python
6-
from docstrange.types import ExtractRequestBody, ExtractResponse
6+
from docstrange.types import (
7+
BatchExtractResponse,
8+
ExtractResponse,
9+
ExtractionFormatResult,
10+
ExtractionMetadata,
11+
ExtractionResult,
12+
ExtractStreamResponse,
13+
)
714
```
815

916
Methods:
1017

18+
- <code title="post /api/v1/extract/async">client.extract.<a href="./src/docstrange/resources/extract/extract.py">async\_</a>(\*\*<a href="src/docstrange/types/extract_async_params.py">params</a>) -> <a href="./src/docstrange/types/extract_response.py">ExtractResponse</a></code>
19+
- <code title="post /api/v1/extract/batch">client.extract.<a href="./src/docstrange/resources/extract/extract.py">batch</a>(\*\*<a href="src/docstrange/types/extract_batch_params.py">params</a>) -> <a href="./src/docstrange/types/batch_extract_response.py">BatchExtractResponse</a></code>
20+
- <code title="post /api/v1/extract/stream">client.extract.<a href="./src/docstrange/resources/extract/extract.py">stream</a>(\*\*<a href="src/docstrange/types/extract_stream_params.py">params</a>) -> str</code>
1121
- <code title="post /api/v1/extract/sync">client.extract.<a href="./src/docstrange/resources/extract/extract.py">sync</a>(\*\*<a href="src/docstrange/types/extract_sync_params.py">params</a>) -> <a href="./src/docstrange/types/extract_response.py">ExtractResponse</a></code>
1222

23+
## Results
24+
25+
Types:
26+
27+
```python
28+
from docstrange.types.extract import ExtractionListResponse, PaginationInfo
29+
```
30+
31+
Methods:
32+
33+
- <code title="get /api/v1/extract/results/{record_id}">client.extract.results.<a href="./src/docstrange/resources/extract/results.py">retrieve</a>(record_id, \*\*<a href="src/docstrange/types/extract/result_retrieve_params.py">params</a>) -> <a href="./src/docstrange/types/extract_response.py">ExtractResponse</a></code>
34+
- <code title="get /api/v1/extract/results">client.extract.results.<a href="./src/docstrange/resources/extract/results.py">list</a>(\*\*<a href="src/docstrange/types/extract/result_list_params.py">params</a>) -> <a href="./src/docstrange/types/extract_response.py">SyncPageNumberPagination[ExtractResponse]</a></code>
35+
1336
# Classify
1437

1538
Types:
1639

1740
```python
18-
from docstrange.types import ClassifyRequestBody, ClassifyResponse
41+
from docstrange.types import (
42+
BatchClassifyResponse,
43+
ClassifyResponse,
44+
FileClassificationResult,
45+
PageClassification,
46+
)
1947
```
2048

2149
Methods:
2250

51+
- <code title="post /api/v1/classify/batch">client.classify.<a href="./src/docstrange/resources/classify.py">batch</a>(\*\*<a href="src/docstrange/types/classify_batch_params.py">params</a>) -> <a href="./src/docstrange/types/batch_classify_response.py">BatchClassifyResponse</a></code>
2352
- <code title="post /api/v1/classify/sync">client.classify.<a href="./src/docstrange/resources/classify.py">sync</a>(\*\*<a href="src/docstrange/types/classify_sync_params.py">params</a>) -> <a href="./src/docstrange/types/classify_response.py">ClassifyResponse</a></code>
53+
54+
# Chat
55+
56+
Types:
57+
58+
```python
59+
from docstrange.types import ChatCompletionsRequest
60+
```
61+
62+
Methods:
63+
64+
- <code title="post /v1/chat/completions">client.chat.<a href="./src/docstrange/resources/chat.py">create_completion</a>(\*\*<a href="src/docstrange/types/chat_create_completion_params.py">params</a>) -> object</code>

src/docstrange/_client.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
)
3232

3333
if TYPE_CHECKING:
34-
from .resources import extract, classify
34+
from .resources import chat, extract, classify
35+
from .resources.chat import ChatResource, AsyncChatResource
3536
from .resources.classify import ClassifyResource, AsyncClassifyResource
3637
from .resources.extract.extract import ExtractResource, AsyncExtractResource
3738

@@ -114,6 +115,12 @@ def classify(self) -> ClassifyResource:
114115

115116
return ClassifyResource(self)
116117

118+
@cached_property
119+
def chat(self) -> ChatResource:
120+
from .resources.chat import ChatResource
121+
122+
return ChatResource(self)
123+
117124
@cached_property
118125
def with_raw_response(self) -> DocstrangeWithRawResponse:
119126
return DocstrangeWithRawResponse(self)
@@ -294,6 +301,12 @@ def classify(self) -> AsyncClassifyResource:
294301

295302
return AsyncClassifyResource(self)
296303

304+
@cached_property
305+
def chat(self) -> AsyncChatResource:
306+
from .resources.chat import AsyncChatResource
307+
308+
return AsyncChatResource(self)
309+
297310
@cached_property
298311
def with_raw_response(self) -> AsyncDocstrangeWithRawResponse:
299312
return AsyncDocstrangeWithRawResponse(self)
@@ -425,6 +438,12 @@ def classify(self) -> classify.ClassifyResourceWithRawResponse:
425438

426439
return ClassifyResourceWithRawResponse(self._client.classify)
427440

441+
@cached_property
442+
def chat(self) -> chat.ChatResourceWithRawResponse:
443+
from .resources.chat import ChatResourceWithRawResponse
444+
445+
return ChatResourceWithRawResponse(self._client.chat)
446+
428447

429448
class AsyncDocstrangeWithRawResponse:
430449
_client: AsyncDocstrange
@@ -444,6 +463,12 @@ def classify(self) -> classify.AsyncClassifyResourceWithRawResponse:
444463

445464
return AsyncClassifyResourceWithRawResponse(self._client.classify)
446465

466+
@cached_property
467+
def chat(self) -> chat.AsyncChatResourceWithRawResponse:
468+
from .resources.chat import AsyncChatResourceWithRawResponse
469+
470+
return AsyncChatResourceWithRawResponse(self._client.chat)
471+
447472

448473
class DocstrangeWithStreamedResponse:
449474
_client: Docstrange
@@ -463,6 +488,12 @@ def classify(self) -> classify.ClassifyResourceWithStreamingResponse:
463488

464489
return ClassifyResourceWithStreamingResponse(self._client.classify)
465490

491+
@cached_property
492+
def chat(self) -> chat.ChatResourceWithStreamingResponse:
493+
from .resources.chat import ChatResourceWithStreamingResponse
494+
495+
return ChatResourceWithStreamingResponse(self._client.chat)
496+
466497

467498
class AsyncDocstrangeWithStreamedResponse:
468499
_client: AsyncDocstrange
@@ -482,6 +513,12 @@ def classify(self) -> classify.AsyncClassifyResourceWithStreamingResponse:
482513

483514
return AsyncClassifyResourceWithStreamingResponse(self._client.classify)
484515

516+
@cached_property
517+
def chat(self) -> chat.AsyncChatResourceWithStreamingResponse:
518+
from .resources.chat import AsyncChatResourceWithStreamingResponse
519+
520+
return AsyncChatResourceWithStreamingResponse(self._client.chat)
521+
485522

486523
Client = Docstrange
487524

src/docstrange/resources/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
from .chat import (
4+
ChatResource,
5+
AsyncChatResource,
6+
ChatResourceWithRawResponse,
7+
AsyncChatResourceWithRawResponse,
8+
ChatResourceWithStreamingResponse,
9+
AsyncChatResourceWithStreamingResponse,
10+
)
311
from .extract import (
412
ExtractResource,
513
AsyncExtractResource,
@@ -30,4 +38,10 @@
3038
"AsyncClassifyResourceWithRawResponse",
3139
"ClassifyResourceWithStreamingResponse",
3240
"AsyncClassifyResourceWithStreamingResponse",
41+
"ChatResource",
42+
"AsyncChatResource",
43+
"ChatResourceWithRawResponse",
44+
"AsyncChatResourceWithRawResponse",
45+
"ChatResourceWithStreamingResponse",
46+
"AsyncChatResourceWithStreamingResponse",
3347
]

0 commit comments

Comments
 (0)