@@ -34,8 +34,12 @@ client = Parallel(
3434 api_key = os.environ.get(" PARALLEL_API_KEY" ), # This is the default and can be omitted
3535)
3636
37- run_result = client.task_run.execute(input = " France (2023)" , processor = " core" , output = " GDP" )
38- print (run_result.output.parsed)
37+ task_run = client.task_run.create(
38+ input = " France (2023)" ,
39+ processor = " core" ,
40+ )
41+ task_run_result = client.task_run.result(run_id = task_run.run_id)
42+ print (task_run_result.output)
3943```
4044
4145While you can provide an ` api_key ` keyword argument,
@@ -63,10 +67,9 @@ client = AsyncParallel(
6367
6468
6569async def main () -> None :
66- run_result = await client.task_run.execute(
67- input = " France (2023)" , processor = " core" , output = " GDP"
68- )
69- print (run_result.output.parsed)
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)
7073
7174
7275if __name__ == " __main__" :
@@ -78,127 +81,6 @@ using the asynchronous client, especially for executing multiple Task Runs concu
7881Functionality between the synchronous and asynchronous clients is identical, including
7982the convenience methods.
8083
81- ## Convenience methods
82-
83- ### Execute
84-
85- The ` execute ` method provides a single call that combines creating a task run,
86- polling until completion, and parsing structured outputs (if specified).
87-
88- If an output type that inherits from ` BaseModel ` is
89- specified in the call to ` .execute() ` , the response content will be parsed into an
90- instance of the provided output type. The parsed output can be accessed via the
91- ` parsed ` property on the output field of the response.
92-
93- ``` python
94- import asyncio
95- from parallel import AsyncParallel
96- from pydantic import BaseModel
97-
98- client = AsyncParallel()
99-
100-
101- class SampleOutputStructure (BaseModel ):
102- output: str
103-
104-
105- async def main () -> None :
106- # with pydantic
107- run_result = await client.task_run.execute(
108- input = " France (2023)" ,
109- processor = " core" ,
110- output = SampleOutputStructure,
111- )
112- # parsed output of type SampleOutputStructure
113- print (run_result.output.parsed)
114- # without pydantic
115- run_result = await client.task_run.execute(
116- input = " France (2023)" , processor = " core" , output = " GDP"
117- )
118- print (run_result.output.parsed)
119-
120-
121- if __name__ == " __main__" :
122- asyncio.run(main())
123- ```
124-
125- The async client lets you create multiple task runs without blocking.
126- To submit several at once, call ` execute() ` and gather the results at the end.
127-
128- ``` python
129- import asyncio
130- import os
131-
132- from parallel import AsyncParallel
133- from pydantic import BaseModel, Field
134- from typing import List
135-
136-
137- class CountryInput (BaseModel ):
138- country: str = Field(
139- description = " Name of the country to research. Must be a recognized "
140- " sovereign nation (e.g., 'France', 'Japan')."
141- )
142- year: int = Field(
143- description = " Year for which to retrieve data. Must be 2000 or later. "
144- " Use most recent full-year estimates if year is current."
145- )
146-
147-
148- class CountryOutput (BaseModel ):
149- gdp: str = Field(description = " GDP in USD for the year, formatted like '$3.1 trillion (2023)'." )
150- top_exports: List[str ] = Field(
151- description = " Top 3 exported goods/services by value. Use credible sources."
152- )
153- top_imports: List[str ] = Field(
154- description = " Top 3 imported goods/services by value. Use credible sources."
155- )
156-
157-
158- async def main ():
159- # Initialize the Parallel client
160- client = AsyncParallel(api_key = os.environ.get(" PARALLEL_API_KEY" ))
161-
162- # Prepare structured input
163- input_data = [
164- CountryInput(country = " France" , year = 2023 ),
165- CountryInput(country = " Germany" , year = 2023 ),
166- CountryInput(country = " Italy" , year = 2023 ),
167- ]
168-
169- run_results = await asyncio.gather(
170- * [
171- client.task_run.execute(input = datum, output = CountryOutput, processor = " core" )
172- for datum in input_data
173- ]
174- )
175-
176- for run_input, run_result in zip (input_data, run_results):
177- print (f " Task run output for { run_input} : { run_result.output.parsed} " )
178-
179-
180- if __name__ == " __main__" :
181- asyncio.run(main())
182- ```
183-
184- #### ` execute() ` vs ` create() `
185-
186- The ` execute ` and ` create ` methods differ slightly in their signatures and
187- behavior — ` create ` requires a Task Spec object that contains the output schema,
188- while ` execute ` accepts an output schema as a top‑level parameter. ` execute ` is
189- also a one‑shot method that combines creation, polling, and parsing for you.
190-
191- Use ` create ` when you want a run ID immediately and prefer to control polling
192- yourself. ` execute ` is best for one‑shot task execution and for typed inputs and
193- outputs — note that no outputs are available until the call finishes. Finally, for
194- the output of ` execute ` , parsed content is available via ` run_result.output.parsed ` .
195-
196- Both ` execute ` and ` create ` validate inputs when appropriate input types are
197- provided. For ` execute ` , validation happens when a pydantic input is provided. For
198- ` create ` , validation occurs when the input schema is specified inside the task spec
199- parameter. Additionally, in both calls, the un-parsed result content is accessible via
200- the ` run_result.output.content ` .
201-
20284## Frequently Asked Questions
20385
20486** Does the Task API accept prompts or objectives?**
@@ -209,9 +91,8 @@ more information, check [our docs](https://docs.parallel.ai/task-api/core-concep
20991
21092** Can I access beta parameters or endpoints via the SDK?**
21193
212- The SDK currently does not support beta parameters in the Task API. You can consider
213- using [ custom requests] ( #making-customundocumented-requests ) in conjunction with
214- [ low level APIs] ( #lowlevel-api-access ) .
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.
21596
21697** Can I specify a timeout for API calls?**
21798
@@ -297,7 +178,7 @@ from parallel import Parallel
297178client = Parallel()
298179
299180try :
300- client.task_run.execute (input = " France (2023)" , processor = " core" , output = " GDP " )
181+ client.task_run.create (input = " France (2023)" , processor = " core" )
301182except parallel.APIConnectionError as e:
302183 print (" The server could not be reached" )
303184 print (e.__cause__) # an underlying Exception, likely raised within httpx.
@@ -340,9 +221,7 @@ client = Parallel(
340221)
341222
342223# Or, configure per-request:
343- client.with_options(max_retries = 5 ).task_run.execute(
344- input = " France (2023)" , processor = " core" , output = " GDP"
345- )
224+ client.with_options(max_retries = 5 ).task_run.create(input = " France (2023)" , processor = " core" )
346225```
347226
348227### Timeouts
@@ -365,9 +244,7 @@ client = Parallel(
365244)
366245
367246# Override per-request:
368- client.with_options(timeout = 5.0 ).task_run.execute(
369- input = " France (2023)" , processor = " core" , output = " GDP"
370- )
247+ client.with_options(timeout = 5.0 ).task_run.create(input = " France (2023)" , processor = " core" )
371248```
372249
373250On timeout, an ` APITimeoutError ` is thrown.
@@ -408,15 +285,14 @@ The "raw" Response object can be accessed by prefixing `.with_raw_response.` to
408285from parallel import Parallel
409286
410287client = Parallel()
411- response = client.task_run.with_raw_response.execute (
288+ response = client.task_run.with_raw_response.create (
412289 input = " France (2023)" ,
413290 processor = " core" ,
414- output = " GDP"
415291)
416292print (response.headers.get(' X-My-Header' ))
417293
418- task_run = response.parse() # get the object that `task_run.execute()` would have returned
419- print (task_run.output )
294+ task_run = response.parse()
295+ print (task_run.run_id )
420296```
421297
422298These methods return an [ ` APIResponse ` ] ( https://github.com/parallel-web/parallel-sdk-python/tree/main/src/parallel/_response.py ) object.
@@ -430,8 +306,8 @@ The above interface eagerly reads the full response body when you make the reque
430306To stream the response body, use ` .with_streaming_response ` instead, which requires a context manager and only reads the response body once you call ` .read() ` , ` .text() ` , ` .json() ` , ` .iter_bytes() ` , ` .iter_text() ` , ` .iter_lines() ` or ` .parse() ` . In the async client, these are async methods.
431307
432308``` python
433- with client.task_run.with_streaming_response.execute (
434- input = " France (2023)" , processor = " core" , output = " GDP "
309+ with client.task_run.with_streaming_response.create (
310+ input = " France (2023)" , processor = " core"
435311) as response:
436312 print (response.headers.get(" X-My-Header" ))
437313
0 commit comments