parallel-web 0.1.0__py3-none-any.whl
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.
Potentially problematic release.
This version of parallel-web might be problematic. Click here for more details.
- parallel/__init__.py +94 -0
- parallel/_base_client.py +1943 -0
- parallel/_client.py +403 -0
- parallel/_compat.py +230 -0
- parallel/_constants.py +16 -0
- parallel/_exceptions.py +108 -0
- parallel/_files.py +123 -0
- parallel/_models.py +803 -0
- parallel/_qs.py +150 -0
- parallel/_resource.py +43 -0
- parallel/_response.py +830 -0
- parallel/_streaming.py +333 -0
- parallel/_types.py +217 -0
- parallel/_utils/__init__.py +58 -0
- parallel/_utils/_logs.py +25 -0
- parallel/_utils/_proxy.py +62 -0
- parallel/_utils/_reflection.py +42 -0
- parallel/_utils/_streams.py +12 -0
- parallel/_utils/_sync.py +86 -0
- parallel/_utils/_transform.py +447 -0
- parallel/_utils/_typing.py +151 -0
- parallel/_utils/_utils.py +426 -0
- parallel/_version.py +4 -0
- parallel/lib/.keep +4 -0
- parallel/lib/__init__.py +0 -0
- parallel/lib/_parsing/__init__.py +2 -0
- parallel/lib/_parsing/_task_run_result.py +100 -0
- parallel/lib/_parsing/_task_spec.py +93 -0
- parallel/lib/_pydantic.py +29 -0
- parallel/lib/_time.py +57 -0
- parallel/py.typed +0 -0
- parallel/resources/__init__.py +19 -0
- parallel/resources/task_run.py +643 -0
- parallel/types/__init__.py +12 -0
- parallel/types/json_schema_param.py +15 -0
- parallel/types/parsed_task_run_result.py +29 -0
- parallel/types/task_run.py +54 -0
- parallel/types/task_run_create_params.py +32 -0
- parallel/types/task_run_result.py +122 -0
- parallel/types/task_run_result_params.py +13 -0
- parallel/types/task_spec_param.py +35 -0
- parallel/types/text_schema_param.py +15 -0
- parallel_web-0.1.0.dist-info/METADATA +544 -0
- parallel_web-0.1.0.dist-info/RECORD +46 -0
- parallel_web-0.1.0.dist-info/WHEEL +4 -0
- parallel_web-0.1.0.dist-info/licenses/LICENSE +7 -0
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
from typing import Dict, Type, Union, Optional, overload
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
|
|
10
|
+
from parallel.lib._time import prepare_timeout_float
|
|
11
|
+
|
|
12
|
+
from ..types import task_run_create_params, task_run_result_params
|
|
13
|
+
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
|
|
14
|
+
from .._utils import maybe_transform, async_maybe_transform
|
|
15
|
+
from .._compat import cached_property
|
|
16
|
+
from .._resource import SyncAPIResource, AsyncAPIResource
|
|
17
|
+
from .._response import (
|
|
18
|
+
to_raw_response_wrapper,
|
|
19
|
+
to_streamed_response_wrapper,
|
|
20
|
+
async_to_raw_response_wrapper,
|
|
21
|
+
async_to_streamed_response_wrapper,
|
|
22
|
+
)
|
|
23
|
+
from .._base_client import make_request_options
|
|
24
|
+
from ..types.task_run import TaskRun
|
|
25
|
+
from ..types.task_run_result import TaskRunResult
|
|
26
|
+
from ..types.task_spec_param import OutputT, OutputSchema, TaskSpecParam
|
|
27
|
+
from ..lib._parsing._task_spec import build_task_spec_param
|
|
28
|
+
from ..types.parsed_task_run_result import ParsedTaskRunResult
|
|
29
|
+
from ..lib._parsing._task_run_result import (
|
|
30
|
+
wait_for_result as _wait_for_result,
|
|
31
|
+
wait_for_result_async as _wait_for_result_async,
|
|
32
|
+
task_run_result_parser,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
__all__ = ["TaskRunResource", "AsyncTaskRunResource"]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class TaskRunResource(SyncAPIResource):
|
|
39
|
+
@cached_property
|
|
40
|
+
def with_raw_response(self) -> TaskRunResourceWithRawResponse:
|
|
41
|
+
"""
|
|
42
|
+
This property can be used as a prefix for any HTTP method call to return
|
|
43
|
+
the raw response object instead of the parsed content.
|
|
44
|
+
|
|
45
|
+
For more information, see https://www.github.com/shapleyai/parallel-sdk-python#accessing-raw-response-data-eg-headers
|
|
46
|
+
"""
|
|
47
|
+
return TaskRunResourceWithRawResponse(self)
|
|
48
|
+
|
|
49
|
+
@cached_property
|
|
50
|
+
def with_streaming_response(self) -> TaskRunResourceWithStreamingResponse:
|
|
51
|
+
"""
|
|
52
|
+
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
|
|
53
|
+
|
|
54
|
+
For more information, see https://www.github.com/shapleyai/parallel-sdk-python#with_streaming_response
|
|
55
|
+
"""
|
|
56
|
+
return TaskRunResourceWithStreamingResponse(self)
|
|
57
|
+
|
|
58
|
+
def create(
|
|
59
|
+
self,
|
|
60
|
+
*,
|
|
61
|
+
input: Union[str, object],
|
|
62
|
+
processor: str,
|
|
63
|
+
metadata: Optional[Dict[str, Union[str, float, bool]]] | NotGiven = NOT_GIVEN,
|
|
64
|
+
task_spec: Optional[TaskSpecParam] | NotGiven = NOT_GIVEN,
|
|
65
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
66
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
67
|
+
extra_headers: Headers | None = None,
|
|
68
|
+
extra_query: Query | None = None,
|
|
69
|
+
extra_body: Body | None = None,
|
|
70
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
71
|
+
) -> TaskRun:
|
|
72
|
+
"""
|
|
73
|
+
Initiates a single task run.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
input: Input to the task, either text or a JSON object.
|
|
77
|
+
|
|
78
|
+
processor: Processor to use for the task.
|
|
79
|
+
|
|
80
|
+
metadata: User-provided metadata stored with the run. Keys and values must be strings with
|
|
81
|
+
a maximum length of 16 and 512 characters respectively.
|
|
82
|
+
|
|
83
|
+
task_spec: Specification for a task.
|
|
84
|
+
|
|
85
|
+
For convenience we allow bare strings as input or output schemas, which is
|
|
86
|
+
equivalent to a text schema with the same description.
|
|
87
|
+
|
|
88
|
+
extra_headers: Send extra headers
|
|
89
|
+
|
|
90
|
+
extra_query: Add additional query parameters to the request
|
|
91
|
+
|
|
92
|
+
extra_body: Add additional JSON properties to the request
|
|
93
|
+
|
|
94
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
|
95
|
+
"""
|
|
96
|
+
return self._post(
|
|
97
|
+
"/v1/tasks/runs",
|
|
98
|
+
body=maybe_transform(
|
|
99
|
+
{
|
|
100
|
+
"input": input,
|
|
101
|
+
"processor": processor,
|
|
102
|
+
"metadata": metadata,
|
|
103
|
+
"task_spec": task_spec,
|
|
104
|
+
},
|
|
105
|
+
task_run_create_params.TaskRunCreateParams,
|
|
106
|
+
),
|
|
107
|
+
options=make_request_options(
|
|
108
|
+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
|
|
109
|
+
),
|
|
110
|
+
cast_to=TaskRun,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
def retrieve(
|
|
114
|
+
self,
|
|
115
|
+
run_id: str,
|
|
116
|
+
*,
|
|
117
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
118
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
119
|
+
extra_headers: Headers | None = None,
|
|
120
|
+
extra_query: Query | None = None,
|
|
121
|
+
extra_body: Body | None = None,
|
|
122
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
123
|
+
) -> TaskRun:
|
|
124
|
+
"""
|
|
125
|
+
Retrieves a run by run_id.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
extra_headers: Send extra headers
|
|
129
|
+
|
|
130
|
+
extra_query: Add additional query parameters to the request
|
|
131
|
+
|
|
132
|
+
extra_body: Add additional JSON properties to the request
|
|
133
|
+
|
|
134
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
|
135
|
+
"""
|
|
136
|
+
if not run_id:
|
|
137
|
+
raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
|
|
138
|
+
return self._get(
|
|
139
|
+
f"/v1/tasks/runs/{run_id}",
|
|
140
|
+
options=make_request_options(
|
|
141
|
+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
|
|
142
|
+
),
|
|
143
|
+
cast_to=TaskRun,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
def result(
|
|
147
|
+
self,
|
|
148
|
+
run_id: str,
|
|
149
|
+
*,
|
|
150
|
+
api_timeout: int | NotGiven = NOT_GIVEN,
|
|
151
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
152
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
153
|
+
extra_headers: Headers | None = None,
|
|
154
|
+
extra_query: Query | None = None,
|
|
155
|
+
extra_body: Body | None = None,
|
|
156
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
157
|
+
) -> TaskRunResult:
|
|
158
|
+
"""
|
|
159
|
+
Retrieves a run by run_id, blocking until the run is completed.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
extra_headers: Send extra headers
|
|
163
|
+
|
|
164
|
+
extra_query: Add additional query parameters to the request
|
|
165
|
+
|
|
166
|
+
extra_body: Add additional JSON properties to the request
|
|
167
|
+
|
|
168
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
|
169
|
+
"""
|
|
170
|
+
if not run_id:
|
|
171
|
+
raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
|
|
172
|
+
return self._get(
|
|
173
|
+
f"/v1/tasks/runs/{run_id}/result",
|
|
174
|
+
options=make_request_options(
|
|
175
|
+
extra_headers=extra_headers,
|
|
176
|
+
extra_query=extra_query,
|
|
177
|
+
extra_body=extra_body,
|
|
178
|
+
timeout=timeout,
|
|
179
|
+
query=maybe_transform({"api_timeout": api_timeout}, task_run_result_params.TaskRunResultParams),
|
|
180
|
+
),
|
|
181
|
+
cast_to=TaskRunResult,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
def _wait_for_result(
|
|
185
|
+
self,
|
|
186
|
+
*,
|
|
187
|
+
run_id: str,
|
|
188
|
+
deadline: float,
|
|
189
|
+
output: Optional[OutputSchema] | Type[OutputT] | NotGiven = NOT_GIVEN,
|
|
190
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
191
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
192
|
+
extra_headers: Headers | None = None,
|
|
193
|
+
extra_query: Query | None = None,
|
|
194
|
+
extra_body: Body | None = None,
|
|
195
|
+
) -> TaskRunResult | ParsedTaskRunResult[OutputT]:
|
|
196
|
+
"""Wait for a task run to complete within the given timeout."""
|
|
197
|
+
|
|
198
|
+
def _fetcher(run_id: str, deadline: float) -> TaskRunResult | ParsedTaskRunResult[OutputT]:
|
|
199
|
+
timeout = deadline - time.monotonic()
|
|
200
|
+
task_run_result = self.result(
|
|
201
|
+
run_id,
|
|
202
|
+
extra_headers=extra_headers,
|
|
203
|
+
extra_query=extra_query,
|
|
204
|
+
extra_body=extra_body,
|
|
205
|
+
timeout=timeout,
|
|
206
|
+
)
|
|
207
|
+
return task_run_result_parser(task_run_result, output)
|
|
208
|
+
|
|
209
|
+
return _wait_for_result(run_id=run_id, deadline=deadline, callable=_fetcher)
|
|
210
|
+
|
|
211
|
+
@overload
|
|
212
|
+
def execute(
|
|
213
|
+
self,
|
|
214
|
+
*,
|
|
215
|
+
input: Union[str, object],
|
|
216
|
+
processor: str,
|
|
217
|
+
metadata: Optional[Dict[str, Union[str, float, bool]]] | NotGiven = NOT_GIVEN,
|
|
218
|
+
output: Optional[OutputSchema] | NotGiven = NOT_GIVEN,
|
|
219
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
220
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
221
|
+
extra_headers: Headers | None = None,
|
|
222
|
+
extra_query: Query | None = None,
|
|
223
|
+
extra_body: Body | None = None,
|
|
224
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
225
|
+
) -> TaskRunResult: ...
|
|
226
|
+
@overload
|
|
227
|
+
def execute(
|
|
228
|
+
self,
|
|
229
|
+
*,
|
|
230
|
+
input: Union[str, object],
|
|
231
|
+
processor: str,
|
|
232
|
+
metadata: Optional[Dict[str, Union[str, float, bool]]] | NotGiven = NOT_GIVEN,
|
|
233
|
+
output: Type[OutputT],
|
|
234
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
235
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
236
|
+
extra_headers: Headers | None = None,
|
|
237
|
+
extra_query: Query | None = None,
|
|
238
|
+
extra_body: Body | None = None,
|
|
239
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
240
|
+
) -> ParsedTaskRunResult[OutputT]: ...
|
|
241
|
+
def execute(
|
|
242
|
+
self,
|
|
243
|
+
*,
|
|
244
|
+
input: Union[str, object],
|
|
245
|
+
processor: str,
|
|
246
|
+
metadata: Optional[Dict[str, Union[str, float, bool]]] | NotGiven = NOT_GIVEN,
|
|
247
|
+
output: Optional[OutputSchema] | Type[OutputT] | NotGiven = NOT_GIVEN,
|
|
248
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
249
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
250
|
+
extra_headers: Headers | None = None,
|
|
251
|
+
extra_query: Query | None = None,
|
|
252
|
+
extra_body: Body | None = None,
|
|
253
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
254
|
+
) -> TaskRunResult | ParsedTaskRunResult[OutputT]:
|
|
255
|
+
"""
|
|
256
|
+
Convenience method to create and execute a task run in a single call.
|
|
257
|
+
|
|
258
|
+
Awaits run completion. If the run is successful, a `ParsedTaskRunResult`
|
|
259
|
+
is returned when a pydantic was specified in `output`. Otherwise, a
|
|
260
|
+
`TaskRunResult` is returned.
|
|
261
|
+
|
|
262
|
+
Possible errors:
|
|
263
|
+
- `TimeoutError`: If the run does not finish within the specified timeout.
|
|
264
|
+
- `APIStatusError`: If the API returns a non-200-range status code.
|
|
265
|
+
- `APIConnectionError`: If the connection to the API fails.
|
|
266
|
+
|
|
267
|
+
Args:
|
|
268
|
+
input: Input to the task, either text or a JSON object.
|
|
269
|
+
|
|
270
|
+
processor: Processor to use for the task.
|
|
271
|
+
|
|
272
|
+
metadata: User-provided metadata stored with the run. Keys and values must be strings with
|
|
273
|
+
a maximum length of 16 and 512 characters respectively.
|
|
274
|
+
|
|
275
|
+
output: Optional output schema or pydantic type. If pydantic is provided,
|
|
276
|
+
the response will have a parsed field.
|
|
277
|
+
|
|
278
|
+
extra_headers: Send extra headers
|
|
279
|
+
|
|
280
|
+
extra_query: Add additional query parameters to the request
|
|
281
|
+
|
|
282
|
+
extra_body: Add additional JSON properties to the request
|
|
283
|
+
|
|
284
|
+
timeout: Override the client-level default timeout for this request, in seconds.
|
|
285
|
+
If the result is not available within the timeout, a `TimeoutError` is raised.
|
|
286
|
+
"""
|
|
287
|
+
extra_headers = {"X-Stainless-Poll-Helper": "true", **(extra_headers or {})}
|
|
288
|
+
|
|
289
|
+
timeout = prepare_timeout_float(timeout)
|
|
290
|
+
|
|
291
|
+
deadline = time.monotonic() + timeout
|
|
292
|
+
task_run = self.create(
|
|
293
|
+
input=input,
|
|
294
|
+
processor=processor,
|
|
295
|
+
metadata=metadata,
|
|
296
|
+
task_spec=build_task_spec_param(output, input),
|
|
297
|
+
extra_headers=extra_headers,
|
|
298
|
+
extra_query=extra_query,
|
|
299
|
+
extra_body=extra_body,
|
|
300
|
+
timeout=timeout,
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
return self._wait_for_result(
|
|
304
|
+
run_id=task_run.run_id,
|
|
305
|
+
deadline=deadline,
|
|
306
|
+
output=output,
|
|
307
|
+
extra_headers=extra_headers,
|
|
308
|
+
extra_query=extra_query,
|
|
309
|
+
extra_body=extra_body,
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
class AsyncTaskRunResource(AsyncAPIResource):
|
|
314
|
+
@cached_property
|
|
315
|
+
def with_raw_response(self) -> AsyncTaskRunResourceWithRawResponse:
|
|
316
|
+
"""
|
|
317
|
+
This property can be used as a prefix for any HTTP method call to return
|
|
318
|
+
the raw response object instead of the parsed content.
|
|
319
|
+
|
|
320
|
+
For more information, see https://www.github.com/shapleyai/parallel-sdk-python#accessing-raw-response-data-eg-headers
|
|
321
|
+
"""
|
|
322
|
+
return AsyncTaskRunResourceWithRawResponse(self)
|
|
323
|
+
|
|
324
|
+
@cached_property
|
|
325
|
+
def with_streaming_response(self) -> AsyncTaskRunResourceWithStreamingResponse:
|
|
326
|
+
"""
|
|
327
|
+
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
|
|
328
|
+
|
|
329
|
+
For more information, see https://www.github.com/shapleyai/parallel-sdk-python#with_streaming_response
|
|
330
|
+
"""
|
|
331
|
+
return AsyncTaskRunResourceWithStreamingResponse(self)
|
|
332
|
+
|
|
333
|
+
async def create(
|
|
334
|
+
self,
|
|
335
|
+
*,
|
|
336
|
+
input: Union[str, object],
|
|
337
|
+
processor: str,
|
|
338
|
+
metadata: Optional[Dict[str, Union[str, float, bool]]] | NotGiven = NOT_GIVEN,
|
|
339
|
+
task_spec: Optional[TaskSpecParam] | NotGiven = NOT_GIVEN,
|
|
340
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
341
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
342
|
+
extra_headers: Headers | None = None,
|
|
343
|
+
extra_query: Query | None = None,
|
|
344
|
+
extra_body: Body | None = None,
|
|
345
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
346
|
+
) -> TaskRun:
|
|
347
|
+
"""
|
|
348
|
+
Initiates a single task run.
|
|
349
|
+
|
|
350
|
+
Args:
|
|
351
|
+
input: Input to the task, either text or a JSON object.
|
|
352
|
+
|
|
353
|
+
processor: Processor to use for the task.
|
|
354
|
+
|
|
355
|
+
metadata: User-provided metadata stored with the run. Keys and values must be strings with
|
|
356
|
+
a maximum length of 16 and 512 characters respectively.
|
|
357
|
+
|
|
358
|
+
task_spec: Specification for a task.
|
|
359
|
+
|
|
360
|
+
For convenience we allow bare strings as input or output schemas, which is
|
|
361
|
+
equivalent to a text schema with the same description.
|
|
362
|
+
|
|
363
|
+
extra_headers: Send extra headers
|
|
364
|
+
|
|
365
|
+
extra_query: Add additional query parameters to the request
|
|
366
|
+
|
|
367
|
+
extra_body: Add additional JSON properties to the request
|
|
368
|
+
|
|
369
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
|
370
|
+
"""
|
|
371
|
+
return await self._post(
|
|
372
|
+
"/v1/tasks/runs",
|
|
373
|
+
body=await async_maybe_transform(
|
|
374
|
+
{
|
|
375
|
+
"input": input,
|
|
376
|
+
"processor": processor,
|
|
377
|
+
"metadata": metadata,
|
|
378
|
+
"task_spec": task_spec,
|
|
379
|
+
},
|
|
380
|
+
task_run_create_params.TaskRunCreateParams,
|
|
381
|
+
),
|
|
382
|
+
options=make_request_options(
|
|
383
|
+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
|
|
384
|
+
),
|
|
385
|
+
cast_to=TaskRun,
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
async def retrieve(
|
|
389
|
+
self,
|
|
390
|
+
run_id: str,
|
|
391
|
+
*,
|
|
392
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
393
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
394
|
+
extra_headers: Headers | None = None,
|
|
395
|
+
extra_query: Query | None = None,
|
|
396
|
+
extra_body: Body | None = None,
|
|
397
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
398
|
+
) -> TaskRun:
|
|
399
|
+
"""
|
|
400
|
+
Retrieves a run by run_id.
|
|
401
|
+
|
|
402
|
+
Args:
|
|
403
|
+
extra_headers: Send extra headers
|
|
404
|
+
|
|
405
|
+
extra_query: Add additional query parameters to the request
|
|
406
|
+
|
|
407
|
+
extra_body: Add additional JSON properties to the request
|
|
408
|
+
|
|
409
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
|
410
|
+
"""
|
|
411
|
+
if not run_id:
|
|
412
|
+
raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
|
|
413
|
+
return await self._get(
|
|
414
|
+
f"/v1/tasks/runs/{run_id}",
|
|
415
|
+
options=make_request_options(
|
|
416
|
+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
|
|
417
|
+
),
|
|
418
|
+
cast_to=TaskRun,
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
async def result(
|
|
422
|
+
self,
|
|
423
|
+
run_id: str,
|
|
424
|
+
*,
|
|
425
|
+
api_timeout: int | NotGiven = NOT_GIVEN,
|
|
426
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
427
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
428
|
+
extra_headers: Headers | None = None,
|
|
429
|
+
extra_query: Query | None = None,
|
|
430
|
+
extra_body: Body | None = None,
|
|
431
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
432
|
+
) -> TaskRunResult:
|
|
433
|
+
"""
|
|
434
|
+
Retrieves a run by run_id, blocking until the run is completed.
|
|
435
|
+
|
|
436
|
+
Args:
|
|
437
|
+
extra_headers: Send extra headers
|
|
438
|
+
|
|
439
|
+
extra_query: Add additional query parameters to the request
|
|
440
|
+
|
|
441
|
+
extra_body: Add additional JSON properties to the request
|
|
442
|
+
|
|
443
|
+
timeout: Override the client-level default timeout for this request, in seconds
|
|
444
|
+
"""
|
|
445
|
+
if not run_id:
|
|
446
|
+
raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
|
|
447
|
+
return await self._get(
|
|
448
|
+
f"/v1/tasks/runs/{run_id}/result",
|
|
449
|
+
options=make_request_options(
|
|
450
|
+
extra_headers=extra_headers,
|
|
451
|
+
extra_query=extra_query,
|
|
452
|
+
extra_body=extra_body,
|
|
453
|
+
timeout=timeout,
|
|
454
|
+
query=await async_maybe_transform(
|
|
455
|
+
{"api_timeout": api_timeout}, task_run_result_params.TaskRunResultParams
|
|
456
|
+
),
|
|
457
|
+
),
|
|
458
|
+
cast_to=TaskRunResult,
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
async def _wait_for_result(
|
|
462
|
+
self,
|
|
463
|
+
*,
|
|
464
|
+
run_id: str,
|
|
465
|
+
deadline: float,
|
|
466
|
+
output: Optional[OutputSchema] | Type[OutputT] | NotGiven = NOT_GIVEN,
|
|
467
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
468
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
469
|
+
extra_headers: Headers | None = None,
|
|
470
|
+
extra_query: Query | None = None,
|
|
471
|
+
extra_body: Body | None = None,
|
|
472
|
+
) -> TaskRunResult | ParsedTaskRunResult[OutputT]:
|
|
473
|
+
"""Wait for a task run to complete within the given timeout."""
|
|
474
|
+
|
|
475
|
+
async def _fetcher(run_id: str, deadline: float) -> TaskRunResult | ParsedTaskRunResult[OutputT]:
|
|
476
|
+
timeout = deadline - time.monotonic()
|
|
477
|
+
task_run_result = await self.result(
|
|
478
|
+
run_id,
|
|
479
|
+
extra_headers=extra_headers,
|
|
480
|
+
extra_query=extra_query,
|
|
481
|
+
extra_body=extra_body,
|
|
482
|
+
timeout=timeout,
|
|
483
|
+
)
|
|
484
|
+
return task_run_result_parser(task_run_result, output)
|
|
485
|
+
|
|
486
|
+
return await _wait_for_result_async(run_id=run_id, deadline=deadline, callable=_fetcher)
|
|
487
|
+
|
|
488
|
+
@overload
|
|
489
|
+
async def execute(
|
|
490
|
+
self,
|
|
491
|
+
*,
|
|
492
|
+
input: Union[str, object],
|
|
493
|
+
processor: str,
|
|
494
|
+
metadata: Optional[Dict[str, Union[str, float, bool]]] | NotGiven = NOT_GIVEN,
|
|
495
|
+
output: Optional[OutputSchema] | NotGiven = NOT_GIVEN,
|
|
496
|
+
extra_headers: Headers | None = None,
|
|
497
|
+
extra_query: Query | None = None,
|
|
498
|
+
extra_body: Body | None = None,
|
|
499
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
500
|
+
) -> TaskRunResult: ...
|
|
501
|
+
@overload
|
|
502
|
+
async def execute(
|
|
503
|
+
self,
|
|
504
|
+
*,
|
|
505
|
+
input: Union[str, object],
|
|
506
|
+
processor: str,
|
|
507
|
+
metadata: Optional[Dict[str, Union[str, float, bool]]] | NotGiven = NOT_GIVEN,
|
|
508
|
+
output: Type[OutputT],
|
|
509
|
+
extra_headers: Headers | None = None,
|
|
510
|
+
extra_query: Query | None = None,
|
|
511
|
+
extra_body: Body | None = None,
|
|
512
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
513
|
+
) -> ParsedTaskRunResult[OutputT]: ...
|
|
514
|
+
async def execute(
|
|
515
|
+
self,
|
|
516
|
+
*,
|
|
517
|
+
input: Union[str, object],
|
|
518
|
+
processor: str,
|
|
519
|
+
metadata: Optional[Dict[str, Union[str, float, bool]]] | NotGiven = NOT_GIVEN,
|
|
520
|
+
output: Optional[OutputSchema] | Type[OutputT] | NotGiven = NOT_GIVEN,
|
|
521
|
+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
|
522
|
+
# The extra values given here take precedence over values defined on the client or passed to this method.
|
|
523
|
+
extra_headers: Headers | None = None,
|
|
524
|
+
extra_query: Query | None = None,
|
|
525
|
+
extra_body: Body | None = None,
|
|
526
|
+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
|
527
|
+
) -> TaskRunResult | ParsedTaskRunResult[OutputT]:
|
|
528
|
+
"""
|
|
529
|
+
Convenience method to create and execute a task run in a single call.
|
|
530
|
+
|
|
531
|
+
Awaits run completion. If the run is successful, a `ParsedTaskRunResult`
|
|
532
|
+
is returned when a pydantic was specified in `output`. Otherwise, a
|
|
533
|
+
`TaskRunResult` is returned.
|
|
534
|
+
|
|
535
|
+
Possible errors:
|
|
536
|
+
- `TimeoutError`: If the run does not finish within the specified timeout.
|
|
537
|
+
- `APIStatusError`: If the API returns a non-200-range status code.
|
|
538
|
+
- `APIConnectionError`: If the connection to the API fails.
|
|
539
|
+
|
|
540
|
+
Args:
|
|
541
|
+
input: Input to the task, either text or a JSON object.
|
|
542
|
+
|
|
543
|
+
processor: Processor to use for the task.
|
|
544
|
+
|
|
545
|
+
metadata: User-provided metadata stored with the run. Keys and values must be strings with
|
|
546
|
+
a maximum length of 16 and 512 characters respectively.
|
|
547
|
+
|
|
548
|
+
output: Optional output schema or pydantic type. If pydantic is provided,
|
|
549
|
+
the response will have a parsed field.
|
|
550
|
+
|
|
551
|
+
extra_headers: Send extra headers
|
|
552
|
+
|
|
553
|
+
extra_query: Add additional query parameters to the request
|
|
554
|
+
|
|
555
|
+
extra_body: Add additional JSON properties to the request
|
|
556
|
+
|
|
557
|
+
timeout: Override the client-level default timeout for this request, in seconds.
|
|
558
|
+
If the result is not available within the timeout, a `TimeoutError` is raised.
|
|
559
|
+
"""
|
|
560
|
+
extra_headers = {"X-Stainless-Poll-Helper": "true", **(extra_headers or {})}
|
|
561
|
+
|
|
562
|
+
timeout = prepare_timeout_float(timeout)
|
|
563
|
+
deadline = time.monotonic() + timeout
|
|
564
|
+
|
|
565
|
+
task_run = await self.create(
|
|
566
|
+
input=input,
|
|
567
|
+
processor=processor,
|
|
568
|
+
metadata=metadata,
|
|
569
|
+
task_spec=build_task_spec_param(output, input),
|
|
570
|
+
extra_headers=extra_headers,
|
|
571
|
+
extra_query=extra_query,
|
|
572
|
+
extra_body=extra_body,
|
|
573
|
+
timeout=timeout,
|
|
574
|
+
)
|
|
575
|
+
|
|
576
|
+
return await self._wait_for_result(
|
|
577
|
+
run_id=task_run.run_id,
|
|
578
|
+
deadline=deadline,
|
|
579
|
+
output=output,
|
|
580
|
+
extra_headers=extra_headers,
|
|
581
|
+
extra_query=extra_query,
|
|
582
|
+
extra_body=extra_body,
|
|
583
|
+
)
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
class TaskRunResourceWithRawResponse:
|
|
587
|
+
def __init__(self, task_run: TaskRunResource) -> None:
|
|
588
|
+
self._task_run = task_run
|
|
589
|
+
|
|
590
|
+
self.create = to_raw_response_wrapper(
|
|
591
|
+
task_run.create,
|
|
592
|
+
)
|
|
593
|
+
self.retrieve = to_raw_response_wrapper(
|
|
594
|
+
task_run.retrieve,
|
|
595
|
+
)
|
|
596
|
+
self.result = to_raw_response_wrapper(
|
|
597
|
+
task_run.result,
|
|
598
|
+
)
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
class AsyncTaskRunResourceWithRawResponse:
|
|
602
|
+
def __init__(self, task_run: AsyncTaskRunResource) -> None:
|
|
603
|
+
self._task_run = task_run
|
|
604
|
+
|
|
605
|
+
self.create = async_to_raw_response_wrapper(
|
|
606
|
+
task_run.create,
|
|
607
|
+
)
|
|
608
|
+
self.retrieve = async_to_raw_response_wrapper(
|
|
609
|
+
task_run.retrieve,
|
|
610
|
+
)
|
|
611
|
+
self.result = async_to_raw_response_wrapper(
|
|
612
|
+
task_run.result,
|
|
613
|
+
)
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
class TaskRunResourceWithStreamingResponse:
|
|
617
|
+
def __init__(self, task_run: TaskRunResource) -> None:
|
|
618
|
+
self._task_run = task_run
|
|
619
|
+
|
|
620
|
+
self.create = to_streamed_response_wrapper(
|
|
621
|
+
task_run.create,
|
|
622
|
+
)
|
|
623
|
+
self.retrieve = to_streamed_response_wrapper(
|
|
624
|
+
task_run.retrieve,
|
|
625
|
+
)
|
|
626
|
+
self.result = to_streamed_response_wrapper(
|
|
627
|
+
task_run.result,
|
|
628
|
+
)
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
class AsyncTaskRunResourceWithStreamingResponse:
|
|
632
|
+
def __init__(self, task_run: AsyncTaskRunResource) -> None:
|
|
633
|
+
self._task_run = task_run
|
|
634
|
+
|
|
635
|
+
self.create = async_to_streamed_response_wrapper(
|
|
636
|
+
task_run.create,
|
|
637
|
+
)
|
|
638
|
+
self.retrieve = async_to_streamed_response_wrapper(
|
|
639
|
+
task_run.retrieve,
|
|
640
|
+
)
|
|
641
|
+
self.result = async_to_streamed_response_wrapper(
|
|
642
|
+
task_run.result,
|
|
643
|
+
)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .task_run import TaskRun as TaskRun
|
|
6
|
+
from .task_run_result import TaskRunResult as TaskRunResult
|
|
7
|
+
from .task_spec_param import TaskSpecParam as TaskSpecParam
|
|
8
|
+
from .json_schema_param import JsonSchemaParam as JsonSchemaParam
|
|
9
|
+
from .text_schema_param import TextSchemaParam as TextSchemaParam
|
|
10
|
+
from .parsed_task_run_result import ParsedTaskRunResult as ParsedTaskRunResult
|
|
11
|
+
from .task_run_create_params import TaskRunCreateParams as TaskRunCreateParams
|
|
12
|
+
from .task_run_result_params import TaskRunResultParams as TaskRunResultParams
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing_extensions import Literal, Required, TypedDict
|
|
6
|
+
|
|
7
|
+
__all__ = ["JsonSchemaParam"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class JsonSchemaParam(TypedDict, total=False):
|
|
11
|
+
json_schema: Required[object]
|
|
12
|
+
"""A JSON Schema object. Only a subset of JSON Schema is supported."""
|
|
13
|
+
|
|
14
|
+
type: Literal["json"]
|
|
15
|
+
"""The type of schema being defined. Always `json`."""
|