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,93 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Type, Union
|
|
4
|
+
from typing_extensions import TypeGuard
|
|
5
|
+
|
|
6
|
+
import pydantic
|
|
7
|
+
|
|
8
|
+
from ..._types import NOT_GIVEN, NotGiven
|
|
9
|
+
from ..._utils import is_str, is_dict, is_given
|
|
10
|
+
from .._pydantic import to_json_schema, is_basemodel_type
|
|
11
|
+
from ...types.task_spec_param import (
|
|
12
|
+
OutputT,
|
|
13
|
+
InputSchema,
|
|
14
|
+
OutputSchema,
|
|
15
|
+
TaskSpecParam,
|
|
16
|
+
)
|
|
17
|
+
from ...types.json_schema_param import JsonSchemaParam
|
|
18
|
+
from ...types.text_schema_param import TextSchemaParam
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def is_json_schema_param(schema: object) -> TypeGuard[JsonSchemaParam]:
|
|
22
|
+
"""Check if a schema is a JsonSchemaParam."""
|
|
23
|
+
if not is_dict(schema):
|
|
24
|
+
return False
|
|
25
|
+
|
|
26
|
+
if not set(schema.keys()).issubset(set(JsonSchemaParam.__annotations__.keys())):
|
|
27
|
+
return False
|
|
28
|
+
|
|
29
|
+
if not schema.get("type") == "json":
|
|
30
|
+
return False
|
|
31
|
+
|
|
32
|
+
return True
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def is_text_schema_param(schema: object) -> TypeGuard[TextSchemaParam]:
|
|
36
|
+
"""Check if a schema is a TextSchemaParam."""
|
|
37
|
+
if not is_dict(schema):
|
|
38
|
+
return False
|
|
39
|
+
|
|
40
|
+
if not set(TextSchemaParam.__annotations__.keys()) == set(schema.keys()):
|
|
41
|
+
return False
|
|
42
|
+
|
|
43
|
+
if not schema.get("type") == "text":
|
|
44
|
+
return False
|
|
45
|
+
|
|
46
|
+
return is_str(schema.get("description"))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _is_schema_param(schema: object) -> TypeGuard[OutputSchema | InputSchema]:
|
|
50
|
+
"""Check if a schema is an OutputSchema or InputSchema."""
|
|
51
|
+
if is_str(schema):
|
|
52
|
+
return True
|
|
53
|
+
return is_json_schema_param(schema) or is_text_schema_param(schema)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def is_output_schema_param(output_schema: object) -> TypeGuard[OutputSchema]:
|
|
57
|
+
"""Check if a schema is an OutputSchema."""
|
|
58
|
+
return _is_schema_param(output_schema)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def is_input_schema_param(input_schema: object) -> TypeGuard[InputSchema]:
|
|
62
|
+
"""Check if a schema is an InputSchema."""
|
|
63
|
+
return _is_schema_param(input_schema)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _to_json_schema_param(output_format: Type[pydantic.BaseModel]) -> JsonSchemaParam:
|
|
67
|
+
"""Convert a pydantic basemodel to a JsonSchemaParam."""
|
|
68
|
+
return {"json_schema": to_json_schema(output_format), "type": "json"}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _generate_output_schema(output_format: OutputSchema | Type[OutputT]) -> OutputSchema:
|
|
72
|
+
"""Generate an OutputSchema from an OutputSchema or a generic output type."""
|
|
73
|
+
if is_output_schema_param(output_format):
|
|
74
|
+
return output_format
|
|
75
|
+
|
|
76
|
+
if is_basemodel_type(output_format):
|
|
77
|
+
json_schema_param = _to_json_schema_param(output_format)
|
|
78
|
+
return json_schema_param
|
|
79
|
+
|
|
80
|
+
raise TypeError(f"Invalid output_type. Type: {type(output_format)}")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def build_task_spec_param(
|
|
84
|
+
output_format: OutputSchema | Type[OutputT] | None | NotGiven,
|
|
85
|
+
_: Union[str, object], # placeholder for input
|
|
86
|
+
) -> TaskSpecParam | NotGiven:
|
|
87
|
+
"""Build a TaskSpecParam from an OutputSchema or Type[OutputT] if provided."""
|
|
88
|
+
if not is_given(output_format) or output_format is None:
|
|
89
|
+
return NOT_GIVEN
|
|
90
|
+
|
|
91
|
+
# output format has type OutputSchema | Type[OutputT] here
|
|
92
|
+
output_schema = _generate_output_schema(output_format) # type: ignore[arg-type]
|
|
93
|
+
return TaskSpecParam(output_schema=output_schema)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import inspect
|
|
4
|
+
from typing import Any
|
|
5
|
+
from typing_extensions import TypeGuard
|
|
6
|
+
|
|
7
|
+
import pydantic
|
|
8
|
+
|
|
9
|
+
from .._compat import PYDANTIC_V2, model_json_schema
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def to_json_schema(
|
|
13
|
+
model: type[pydantic.BaseModel] | pydantic.TypeAdapter[Any],
|
|
14
|
+
) -> dict[str, Any]:
|
|
15
|
+
"""Convert a Pydantic model/type adapter to a JSON schema."""
|
|
16
|
+
if inspect.isclass(model) and is_basemodel_type(model):
|
|
17
|
+
return model_json_schema(model)
|
|
18
|
+
|
|
19
|
+
if PYDANTIC_V2 and isinstance(model, pydantic.TypeAdapter):
|
|
20
|
+
return model.json_schema()
|
|
21
|
+
|
|
22
|
+
raise TypeError(f"Non BaseModel types are only supported with Pydantic v2 - {model}")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def is_basemodel_type(typ: type | object) -> TypeGuard[type[pydantic.BaseModel]]:
|
|
26
|
+
"""Check if a type is a Pydantic BaseModel."""
|
|
27
|
+
if not inspect.isclass(typ):
|
|
28
|
+
return False
|
|
29
|
+
return issubclass(typ, pydantic.BaseModel)
|
parallel/lib/_time.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import contextlib
|
|
3
|
+
from typing import Union, Iterator, NoReturn
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from .._types import NotGiven
|
|
8
|
+
from .._constants import DEFAULT_TIMEOUT_SECONDS
|
|
9
|
+
from .._exceptions import APIStatusError, APITimeoutError
|
|
10
|
+
from .._utils._utils import is_given
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def prepare_timeout_float(timeout: Union[float, httpx.Timeout, None, NotGiven]) -> float:
|
|
14
|
+
"""Create a simple float timeout for server responses from the provided timeout.
|
|
15
|
+
|
|
16
|
+
For httpx.Timeout, we only use the read timeout.
|
|
17
|
+
"""
|
|
18
|
+
if isinstance(timeout, httpx.Timeout):
|
|
19
|
+
timeout = timeout.read
|
|
20
|
+
|
|
21
|
+
if not is_given(timeout) or timeout is None:
|
|
22
|
+
return DEFAULT_TIMEOUT_SECONDS
|
|
23
|
+
|
|
24
|
+
return timeout
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _raise_timeout(run_id: str, exc: Union[Exception, None]) -> NoReturn:
|
|
28
|
+
raise TimeoutError(f"Fetching task run result for run id {run_id} timed out.") from exc
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@contextlib.contextmanager
|
|
32
|
+
def timeout_retry_context(run_id: str, deadline: float) -> Iterator[None]:
|
|
33
|
+
"""Context manager for handling timeouts and retries when fetching task run results.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
run_id: The ID of the task run
|
|
37
|
+
deadline: The absolute time (monotonic) by which the operation must complete
|
|
38
|
+
|
|
39
|
+
Raises:
|
|
40
|
+
TimeoutError: If the deadline is reached
|
|
41
|
+
APIStatusError: For non-timeout API errors
|
|
42
|
+
"""
|
|
43
|
+
exc: Union[Exception, None] = None
|
|
44
|
+
while time.monotonic() < deadline:
|
|
45
|
+
try:
|
|
46
|
+
yield
|
|
47
|
+
return
|
|
48
|
+
except APITimeoutError as e:
|
|
49
|
+
exc = e
|
|
50
|
+
continue
|
|
51
|
+
except APIStatusError as e:
|
|
52
|
+
# retry on timeouts from the API
|
|
53
|
+
if e.status_code == 408:
|
|
54
|
+
exc = e
|
|
55
|
+
continue
|
|
56
|
+
raise
|
|
57
|
+
_raise_timeout(run_id, exc)
|
parallel/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
from .task_run import (
|
|
4
|
+
TaskRunResource,
|
|
5
|
+
AsyncTaskRunResource,
|
|
6
|
+
TaskRunResourceWithRawResponse,
|
|
7
|
+
AsyncTaskRunResourceWithRawResponse,
|
|
8
|
+
TaskRunResourceWithStreamingResponse,
|
|
9
|
+
AsyncTaskRunResourceWithStreamingResponse,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"TaskRunResource",
|
|
14
|
+
"AsyncTaskRunResource",
|
|
15
|
+
"TaskRunResourceWithRawResponse",
|
|
16
|
+
"AsyncTaskRunResourceWithRawResponse",
|
|
17
|
+
"TaskRunResourceWithStreamingResponse",
|
|
18
|
+
"AsyncTaskRunResourceWithStreamingResponse",
|
|
19
|
+
]
|