Skip to content

Commit 3c430ab

Browse files
authored
Merge pull request #23 from stainless-sdks/mh/regenerate-readme
docs: regenerate README from spec (match TS, rely on docs.parallel.ai)
2 parents de1c04c + c7ed169 commit 3c430ab

1 file changed

Lines changed: 68 additions & 89 deletions

File tree

README.md

Lines changed: 68 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
The Parallel Python library provides convenient access to the Parallel REST API from any Python 3.9+
77
application. The library includes type definitions for all request params and response fields,
88
and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx).
9-
It is strongly encouraged to use the asynchronous client for best performance.
109

1110
It is generated with [Stainless](https://www.stainless.com/).
1211

1312
## Documentation
1413

15-
The REST API documentation can be found in our [docs](https://docs.parallel.ai).
16-
The full API of this Python library can be found in [api.md](api.md).
14+
The REST API documentation can be found on [docs.parallel.ai](https://docs.parallel.ai). The full API of this library can be found in [api.md](api.md).
1715

1816
## Installation
1917

@@ -35,23 +33,17 @@ client = Parallel(
3533
)
3634

3735
task_run = client.task_run.create(
38-
input="France (2023)",
39-
processor="core",
36+
input="What was the GDP of France in 2023?",
37+
processor="base",
4038
)
41-
task_run_result = client.task_run.result(run_id=task_run.run_id)
42-
print(task_run_result.output)
39+
print(task_run.interaction_id)
4340
```
4441

4542
While you can provide an `api_key` keyword argument,
4643
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
4744
to add `PARALLEL_API_KEY="My API Key"` to your `.env` file
4845
so that your API Key is not stored in source control.
4946

50-
The API also supports typed inputs and outputs via Pydantic objects. See the relevant
51-
section on [convenience methods](#convenience-methods).
52-
53-
For information on what tasks are and how to specify them, see [our docs](https://docs.parallel.ai/task-api/core-concepts/specify-a-task).
54-
5547
## Async usage
5648

5749
Simply import `AsyncParallel` instead of `Parallel` and use `await` with each API call:
@@ -67,102 +59,79 @@ client = AsyncParallel(
6759

6860

6961
async def main() -> None:
70-
task_run = await client.task_run.create(input="France (2023)", processor="core")
71-
run_result = await client.task_run.result(run_id=task_run.run_id)
72-
print(run_result.output.content)
62+
task_run = await client.task_run.create(
63+
input="What was the GDP of France in 2023?",
64+
processor="base",
65+
)
66+
print(task_run.interaction_id)
7367

7468

75-
if __name__ == "__main__":
76-
asyncio.run(main())
69+
asyncio.run(main())
7770
```
7871

79-
To get the best performance out of Parallel's API, we recommend
80-
using the asynchronous client, especially for executing multiple Task Runs concurrently.
81-
Functionality between the synchronous and asynchronous clients is identical, including
82-
the convenience methods.
72+
Functionality between the synchronous and asynchronous clients is otherwise identical.
8373

84-
## Frequently Asked Questions
74+
### With aiohttp
8575

86-
**Does the Task API accept prompts or objectives?**
76+
By default, the async client uses `httpx` for HTTP requests. However, for improved concurrency performance you may also use `aiohttp` as the HTTP backend.
8777

88-
No, there are no `objective` or `prompt` parameters that can be specified for calls to
89-
the Task API. Instead, provide any directives or instructions via the schemas. For
90-
more information, check [our docs](https://docs.parallel.ai/task-api/core-concepts/specify-a-task).
78+
You can enable this by installing `aiohttp`:
9179

92-
**Can I access beta parameters or endpoints via the SDK?**
80+
```sh
81+
# install from PyPI
82+
pip install parallel-web[aiohttp]
83+
```
9384

94-
Yes, the SDK supports both beta endpoints and beta header parameters for the Task API.
95-
All beta parameters are accessible via the `client.beta` namespace in the SDK.
85+
Then you can enable it by instantiating the client with `http_client=DefaultAioHttpClient()`:
9686

97-
**Can I specify a timeout for API calls?**
87+
```python
88+
import os
89+
import asyncio
90+
from parallel import DefaultAioHttpClient
91+
from parallel import AsyncParallel
9892

99-
Yes, all methods support a timeout. For more information, see [Timeouts](#timeouts).
10093

94+
async def main() -> None:
95+
async with AsyncParallel(
96+
api_key=os.environ.get("PARALLEL_API_KEY"), # This is the default and can be omitted
97+
http_client=DefaultAioHttpClient(),
98+
) as client:
99+
task_run = await client.task_run.create(
100+
input="What was the GDP of France in 2023?",
101+
processor="base",
102+
)
103+
print(task_run.interaction_id)
104+
105+
106+
asyncio.run(main())
107+
```
108+
109+
## Using types
101110

102-
**Can I specify retries via the SDK?**
111+
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like:
103112

104-
Yes, errors can be retried via the SDK — the default retry count is 2. The maximum number
105-
of retries can be configured at the client level. For information on which errors
106-
are automatically retried and how to configure retry settings, see [Retries](#retries).
113+
- Serializing back into JSON, `model.to_json()`
114+
- Converting to a dictionary, `model.to_dict()`
107115

108-
## Low‑level API access
116+
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`.
109117

110-
The library also provides low‑level access to the Parallel API.
118+
## Nested params
119+
120+
Nested parameters are dictionaries, typed using `TypedDict`, for example:
111121

112122
```python
113123
from parallel import Parallel
114-
from parallel.types import TaskSpecParam
115124

116125
client = Parallel()
117126

118127
task_run = client.task_run.create(
119-
input={"country": "France", "year": 2023},
120-
processor="core",
121-
task_spec={
122-
"output_schema": {
123-
"json_schema": {
124-
"additionalProperties": False,
125-
"properties": {
126-
"gdp": {
127-
"description": "GDP in USD for the year",
128-
"type": "string",
129-
}
130-
},
131-
"required": ["gdp"],
132-
"type": "object",
133-
},
134-
"type": "json",
135-
},
136-
"input_schema": {
137-
"json_schema": {
138-
"additionalProperties": False,
139-
"properties": {
140-
"country": {
141-
"description": "Name of the country to research",
142-
"type": "string",
143-
},
144-
"year": {
145-
"description": "Year for which to retrieve information",
146-
"type": "integer",
147-
},
148-
},
149-
"required": ["country", "year"],
150-
"type": "object",
151-
},
152-
"type": "json",
153-
},
154-
},
128+
input="What was the GDP of France in 2023?",
129+
processor="base",
130+
advanced_settings={},
155131
)
156-
157-
run_result = client.task_run.result(task_run.run_id)
158-
print(run_result.output.content)
132+
print(task_run.advanced_settings)
159133
```
160134

161-
For more information, please check out the relevant section in our docs:
162-
163-
- [Task Spec](https://docs.parallel.ai/task-api/core-concepts/specify-a-task)
164-
- [Task Runs](https://docs.parallel.ai/task-api/core-concepts/execute-task-run)
165-
166135
## Handling errors
167136

168137
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `parallel.APIConnectionError` is raised.
@@ -179,7 +148,10 @@ from parallel import Parallel
179148
client = Parallel()
180149

181150
try:
182-
client.task_run.create(input="France (2023)", processor="core")
151+
client.task_run.create(
152+
input="What was the GDP of France in 2023?",
153+
processor="base",
154+
)
183155
except parallel.APIConnectionError as e:
184156
print("The server could not be reached")
185157
print(e.__cause__) # an underlying Exception, likely raised within httpx.
@@ -222,7 +194,10 @@ client = Parallel(
222194
)
223195

224196
# Or, configure per-request:
225-
client.with_options(max_retries=5).task_run.create(input="France (2023)", processor="core")
197+
client.with_options(max_retries=5).task_run.create(
198+
input="What was the GDP of France in 2023?",
199+
processor="base",
200+
)
226201
```
227202

228203
### Timeouts
@@ -245,7 +220,10 @@ client = Parallel(
245220
)
246221

247222
# Override per-request:
248-
client.with_options(timeout=5.0).task_run.create(input="France (2023)", processor="core")
223+
client.with_options(timeout=5.0).task_run.create(
224+
input="What was the GDP of France in 2023?",
225+
processor="base",
226+
)
249227
```
250228

251229
On timeout, an `APITimeoutError` is thrown.
@@ -287,12 +265,12 @@ from parallel import Parallel
287265

288266
client = Parallel()
289267
response = client.task_run.with_raw_response.create(
290-
input="France (2023)",
291-
processor="core",
268+
input="What was the GDP of France in 2023?",
269+
processor="base",
292270
)
293271
print(response.headers.get('X-My-Header'))
294272

295-
task_run = response.parse()
273+
task_run = response.parse() # get the object that `task_run.create()` would have returned
296274
print(task_run.interaction_id)
297275
```
298276

@@ -308,7 +286,8 @@ To stream the response body, use `.with_streaming_response` instead, which requi
308286

309287
```python
310288
with client.task_run.with_streaming_response.create(
311-
input="France (2023)", processor="core"
289+
input="What was the GDP of France in 2023?",
290+
processor="base",
312291
) as response:
313292
print(response.headers.get("X-My-Header"))
314293

0 commit comments

Comments
 (0)